System details:
- Board: Custom
- Chip: nRF52832
- PCA Number: PCA10040
- SoftDevice: S132
- SDK: Version 16.0.0
I have been building a custom nRF52832 device for a custom application that incorporates a handful of common services from the nRF SDK examples. This project first began by incorporating the TWI peripheral example with the BLE UART example, and at this point the application supported the UART and DIS services (both of these services were advertised). Next, I incorporated the DFU service (it was never an advertised service) and once I got this service to work quite well, I came to depend on the fact that I could connect to my device via the iOS nRF Connect app and perform an OTA DFU. For added context, my custom device is a fully-encapsulated, wirelessly-charged device, which means that I have no access to debug or perform DFU via the SeggerES once I encapsulate the hardware...so this use of the nRF Connect iOS app is very helpful.
Now, I just got around to adding the HIDS mouse service (which also requires the BAS and DIS services), and although I am able to successfully control my computer's cursor when using my device (sometimes), I can no longer perform my OTA DFU (nRF Toolbox and Connect for both iOS and Mac OS). I can connect to the device with a separate, custom CLI application I have built that I was using previously for BLE UART communication, but I CAN NOT make a connection to the device when trying to perform OTA DFU (I have tried nRF Toolbox, and nRF Connect for iOS and for Mac Desktop with a nRF52DK as a dongle). To add a little more color, when I select "Connect" to my custom device in the nRF Connect app, it times out and shows a log/pop-up error that connection cant be established. I have also gotten a pop-up that says "Peer removed pairing information". When trying the same thing via the Desktop nRF Connect app, the HCI Error 0x3E is thrown. On the other hand, my custom CLI application scans for UART-enabled devices of a particular name, and connects via the Bleak BLE library. This essentially works as before, only now it connects to the UART service AND to the HID mouse service (I can see it pop up in my computer's bluetooth preferences as a connected HID device).
Another observation related to connection...I was going to make a new ticket about this, but felt it is probably related: It might even be an intended feature of the HID mouse code, but I notice that sometimes when the device is turned on & advertising, and my computer bluetooth is on, a connection will rapidly be made and then disconnected over and over again between the device and my computer. I can see this happen in the Debug Console, and this behavior has only occurred after installing HIDS. I tried erasing bonds, but it doesnt appear to prevent that repeated connection attempt behavior. This behavior doesnt seem to affect the performance of the UART service, which can still be connected to after this back and forth connection/disconnection and it sends/receives messages just fine. However, it does often affect the performance of the HID mouse code, which sometimes wont work at all when the BLE connection is made after the series of repeated connections/disconnections. When viewing the Debug Console, I notice that whenever the HIDS mouse fails to get set up properly (and thus fails to control my computer mouse), it is due to a "Connection security failed: Bonding error 133". As I mentioned, I have erase_bonds = true and this parameter is passed in to advertising_start(erase_bonds) upon device initialization. I also call delete_bonds() in the ble_evt_handler on BLE_GAP_EVT_DISCONNECTED, but this event never occurs because the HIDS mouse code just stays connected. Lastly, I have looked into the way that I am disconnecting from my device, and realize that it is likely problematic. Previously, I just had to unsubscribe from the UART TX characteristic and disconnect. Now I am also unsubscribing from the Input Report Characteristic, but I believe I am doing this wrong, and also I worry that I am forgetting to unsubscribe from some other HID characteristics that I dont know enough about. Any guidance/advice/resources on disconnecting from HID properly is much appreciated!
Finally, Id like to share the edits/tweaks I have made along the way that differ from the basic example code:
- sdk_config.h: Attached in .zip below for general reference. My concern is I have somehow mismatched some configs that are problematic without me knowing...
- mouth_mouse.c: Attached in .zip below. The file containing all HIDS-specific code.
- NOTE that at the end of the function mouse_movement_send(), I have added the if check (err_code != NRF_ERROR_FORBIDDEN) along with the others to handle this error that would keep getting thrown because I have set the mouse movement period to be every 50ms
- This was suggested in this post. Is there a better more official method I should use?
- ble_stack.c: Attached in .zip below. The main file for all BLE-related code (advertising, connections, most services, NUS event handler)
- NOTE that instead of advertising UART and DIS as I previously did, now we are advertising UART and HID, no DIS. Would this change anything?
- data_sampling.c: Attached in .zip below. This is the main code that reads all sensor data and streams it via BLE UART.
- main.c: Attached in .zip below for reference.