Handling Bluetooth Disabling Gracefully

Is there a way to gracefully handle the toggling of Bluetooth from the phone while a connection is already established? I am able to register a broadcast receiver to handle the events STATE_TURNING_OFF and STATE_OFF. The Android API docs recommend gracefully disconnecting from any Bluetooth devices when the message STATE_TURNING_OFF is received, since the STATE_OFF message is not received until about 1-2 seconds (at least on my device) afterward. However, I'm unable to disconnect properly from the device in that time it seems. And I found that if I do not properly disconnect from the Metawear, then I cannot re-establish a connection without removing the battery. Any thoughts?

Comments

  • edited June 2016
    Disabling the BT adapter should trigger the "disconnected" callback function.  Does your Android device behave differently?
    06-26 22:42:14.597 17269-17281/? I/BluetoothAdapterState: Bluetooth adapter state changed: 15-> 16
    06-26 22:42:14.598 17269-17281/? D/BluetoothAdapterProperties: onBleDisable
    06-26 22:42:14.598 17269-17281/? I/BluetoothAdapterState: Entering PendingCommandState
    06-26 22:42:14.598 17269-17282/? D/bt_stack_manager: event_shut_down_stack is bringing down the stack.
    06-26 22:42:14.599 17269-17285/? D/BluetoothAdapterProperties: Scan Mode:20
    06-26 22:42:14.602 17128-17128/? D/BluetoothMap: Proxy object disconnected
    06-26 22:42:14.602 17128-17128/? D/MapProfile: Bluetooth service disconnected
    06-26 22:42:14.602 28625-28625/? I/TrustAgent.Tracker: [BluetoothConnectionTracker] Bluetooth adapter off, revoking trust
    06-26 22:42:14.610 17269-17292/? W/bt_btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0016
    06-26 22:42:14.610 17128-17128/? D/DockEventReceiver: finishStartingService: stopping service
    06-26 22:42:14.609 28625-28625/? I/TrustAgent.Tracker: [BluetoothConnectionTracker] Bluetooth adapter off, revoking trust
    06-26 22:42:14.611 17269-17285/? D/BtGatt.GattService: onDisconnected() - clientIf=5, connId=5, address=D5:7B:B9:7D:CE:0E
    06-26 22:42:14.612 17255-17268/com.mbientlab.metawear.example D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=D5:7B:B9:7D:CE:0E
    06-26 22:42:14.612 17255-17268/com.mbientlab.metawear.example D/BluetoothGatt: refresh() - device: D5:7B:B9:7D:CE:0E
    06-26 22:42:14.612 17269-17279/? D/BtGatt.GattService: refreshDevice() - address=D5:7B:B9:7D:CE:0E
    06-26 22:42:14.613 17255-17268/com.mbientlab.metawear.example D/BluetoothGatt: close()
    06-26 22:42:14.613 17255-17268/com.mbientlab.metawear.example D/BluetoothGatt: unregisterApp() - mClientIf=5
    06-26 22:42:14.613 17269-17338/? D/BtGatt.GattService: unregisterClient() - clientIf=5
    Other people have mentioned the same limbo state but I cannot reproduce it on our Nexus devices.  If I connect to a MetaWear then kill the app by swiping it from the recent apps screen, I see the Android GattService closing the connection:

    06-26 22:31:31.089 938-1948/? I/WindowState: WIN DEATH: Window{6e4cc82 u0 com.mbientlab.metawear.example/com.mbientlab.metawear.example.MainActivity}
    06-26 22:31:31.090 1100-1156/? D/BtGatt.GattService: Binder is dead - unregistering client (5)!
    06-26 22:31:31.090 1100-1122/? D/BtGatt.AdvertiseManager: message : 1
    06-26 22:31:31.091 1100-1122/? D/BtGatt.AdvertiseManager: stop advertise for client 5
    06-26 22:31:31.092 1100-1122/? D/BtGatt.AdvertiseManager: app died - unregistering client : 5
    06-26 22:31:31.092 1100-1118/? E/bt_btif: btgattc_handle_event invalid instance ID in BTIF_GATTC_DISABLE_ADV
    06-26 22:31:31.092 1100-1122/? D/BtGatt.GattService: unregisterClient() - clientIf=5
    06-26 22:31:31.093 1100-1145/? W/bt_btif: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
    06-26 22:31:31.093 1100-1145/? W/bt_btif: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0016
    06-26 22:31:31.093 1100-1145/? W/bt_btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0016
    06-26 22:31:31.120 1100-1118/? D/BtGatt.GattService: onDisconnected() - clientIf=5, connId=5, address=D5:7B:B9:7D:CE:0E
    06-26 22:31:31.120 1100-1118/? E/BtGatt.ContextMap: Context not found for ID 5
    06-26 22:31:31.139 1100-1145/? I/bt_btm_sec: btm_sec_disconnected clearing pending flag handle:64 reason:22
    06-26 22:31:31.140 1100-1118/? E/BluetoothRemoteDevices: state12newState1
    06-26 22:31:31.145 28625-28625/? I/TrustAgent.Tracker: [BluetoothConnectionTracker] Bluetooth disconnect broadast for MetaWear D5:7B:B9:7D:CE:0E
    06-26 22:31:31.146 1100-1100/? D/BluetoothMapService: onReceive
    06-26 22:31:31.146 1100-1100/? D/BluetoothMapService: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
    06-26 22:31:31.154 28625-28625/? I/TrustAgent.Tracker: [BluetoothConnectionTracker] Bluetooth disconnect broadast for MetaWear D5:7B:B9:7D:CE:0E
    Again, I am assuming your Android device behaves differently wrt to Bluetooth connections?
  • From the sample application, I get the following exception when I turn off Bluetooth while connected to the Metawear C.

    06-27 14:57:37.651  20246-20260/? E/Flow﹕ IOException occurred when accepting connections
        java.io.IOException: read failed, socket might closed or timeout, read ret: -1
                at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
                at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:859)
                at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:674)
                at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:158)
                at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:144)
                at com.google.android.clockwork.companion.flow.FlowService$AcceptThread.run(FlowService.java:240)

    Then after reenabling Bluetooth, I'm unable to find the device until I remove and reinsert the battery. Swiping the app away doesn't cause any issues for me and I also see the GattService closing the connection. I have a Galaxy S5 running the latest version of Android.
  • Sorry for not updating this thread in a while.  I haven't had a chance to further look into this issue.
  • Thanks, it's really not a critical component of my application. Also, I was actually using an S6, not an S5 in case you are looking into it further.
This discussion has been closed.