Which works except calling _initialize() again gets me to _waitForNetworkConnection() where it blocks waiting for a semaphore that never comes. It seems the network state change callback isn’t triggered on a second connect.
Am I doing something wrong? Is there a recommended process to shut BLE down and bring it back up? Manually calling AwsIotNetworkManager_DisableNetwork(AWSIOT_NETWORK_TYPE_BLE); and re-enable causes errors where BLE is hard-down, no advertisiment, etc.
To help us troubleshoot further, could you mention which board are you using ?
Please note, the callback will only be triggered only upon a successful BLE connection , that is a BLE central has connected to the device. Are you seeing BLE advertisement from the peripheral after disabling and enabling BLE ?
And yes. If I just call cleanup to deinit what it can, it does come back to advertising. It does connect on BLE. However… see log:
24 787 [xx] [INFO ][MQTT][7870] (MQTT connection 2001c1c8, SUBSCRIBE operation 2001c470) Wait complete with result SUCCESS.
29 1216 [BLE] [INFO ][DEMO][12160] BLE disconnected with remote device, connId = 0
30 6781 [xx] [ERROR][BLE_TXRX][67810] TX Failed, channel closed.
31 6781 [xx] [ERROR][MQTT][67810] (MQTT connection 2001c1c8) Failed to send PINGREQ.
32 6781 [xx] [INFO ][MQTT][67810] (MQTT connection 2001c1c8) Network connection closed.
35 6807 [xx] [INFO ][MQTT][68070] MQTT library cleanup done.
36 6807 [xx] [INFO ][INIT][68070] SDK cleanup done.
39 6847 [xx] [INFO ][INIT][68470] SDK successfully initialized.
40 6847 [xx] [INFO ][DEMO][68470] No networks connected for the demo. Waiting for a network connection.
41 8137 [BLE] [ERROR][BLE_HAL][81370] PM_UNKNOWN, 0.
42 8137 [BLE] [ERROR][BLE_HAL][81370] sd_ble_gattc_exchange_mtu_request() on connection 0x0 returned busy, will retry.
43 8137 [BLE] [INFO ][DEMO][81370] BLE Connected to remote device, connId = 0
44 8166 [BLE] [INFO ][BLE_TXRX][81660] MTU changed, changing transmit length for data transfer service from 244 to 182
45 8207 [BLE] [ERROR][BLE_HAL][82070] UNKNOWN BLE EVENT 34.
46 8211 [BLE] [ERROR][BLE_HAL][82110] UNKNOWN BLE EVENT 35.
And that’s it. Hangs there at EVENT34/35 (fix btw?)… Never gets the callback and thus semaphore that connection was re-established.
I pulled some lines of that log out, lines my program put in for debug.
24 - normal process up to this point just subbed to a topic
29 - I disconnect phone by shutting app down
30 - Takes 60 seconds for program to timeout, haven’t messed with this yet
35 - loss is detected in my program, starts cleanups
39 - cleaned up, now starts reinit
40 - back to waiting for BLE connection
41 - I connect with app, goes through the process and connects actually, no callback triggered
Now, if I call AwsIotNetworkManager_DisableNetwork(AWSIOT_NETWORK_TYPE_BLE); then cleanup, then re-enable, no the advertising doesn’t come back and the log has errors to the same effect.
I am able to reproduce this after modifying the demo runner code as you mentioned above. It seems the events are received, but the state variable in the demorunner is not in sync. There seems to be 2 modifications you want to do for this:
When BLE is disabled by calling AwsIotNetworkManager_DisableNetwork() , it transitions to network state eNetworkStateUnknown but the demorunner is not expecting that. So you need to modify the _onNetworkStateChangeCallback function here as follows:
else if( ( state == eNetworkStateDisabled || state == eNetworkStateUnknown )
&& ( demoConnectedNetwork == network ) )
{
Cleanup function performs AwsIotNetworkManager_RemoveSubscription() and IotSdk_Cleanup() immediately after disabling network. But the network state change events are scheduled asynchronously using taskpool, so you should perform this only once after the whole application is cleaned up.
Hope this helps. Please feel free to get back if you have questions.
Interesting to me, if I leave out the disable, I get the same log. If I don’t disable or enable, I get the same lack of state semaphore - despite adding the lines you suggested. I put a breakpoint of that line just to see, and I’m never getting to _onNetworkStateChangeCallback() at all. I’m looking at the ERROR 34 / 35 lines, but never got to where state is checked at all.