2

We are using RxAndroidBle 1.3.1 to connect to BLE devices from Android. On certain phones (currently I'm seeing this on an LG V20 [LG-H918], Android 7.0), after our app disconnects, the phone will continue to connect without any user input.

When we close our app, the device disconnects. Then we can kill our app process. Twenty seconds or so later, the phone reconnects to the device for about 2 seconds then disconnects. This behavior repeats every 10-20 seconds indefinitely, until we reboot the phone.

Actually, we even get the recurring connections if we hard-kill the app while the connection still exists.

These are the logs when we disconnect:

06-21 10:51:07.464 26640-26640/com.hatchbaby.rest.qa I/Nightlight: [main] Disconnecting: Nightlight.disconnect()
06-21 10:51:07.528 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: setCharacteristicNotification() - uuid: 02240003-5efd-47eb-9c1a-de53f7a2b232 enable: false
06-21 10:51:07.538 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.539 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.543 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: setCharacteristicNotification() - uuid: 02260002-5efd-47eb-9c1a-de53f7a2b232 enable: false
06-21 10:51:07.547 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.548 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDisconnect(160892126)
06-21 10:51:07.562 26640-26640/com.hatchbaby.rest.qa I/Nightlight: [main] Nightlight state: RxBleConnectionState{DISCONNECTED}
06-21 10:51:07.686 26640-26747/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
06-21 10:51:07.688 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.689 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.783 26640-26746/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
06-21 10:51:07.786 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.787 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDisconnect(160892126)
06-21 10:51:07.787 26640-26640/com.hatchbaby.rest.qa D/BluetoothManager: getConnectionState()
06-21 10:51:07.787 26640-26640/com.hatchbaby.rest.qa D/BluetoothManager: getConnectedDevices
06-21 10:51:07.793 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: cancelOpen() - device: E5:D6:FC:68:FB:55
06-21 10:51:07.797 26640-26653/com.hatchbaby.rest.qa D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=E5:D6:FC:68:FB:55
06-21 10:51:07.797 26640-26653/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=0
06-21 10:51:07.803 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: close()
06-21 10:51:07.803 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: unregisterApp() - mClientIf=6
06-21 10:51:07.807 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDisconnect(160892126)

I am baffled; any help is appreciated!

Ken DeLong
  • 929
  • 2
  • 8
  • 27

3 Answers3

1

Make sure you called close method.

close method

Tony Lin
  • 765
  • 3
  • 15
  • 35
  • I am using RxAndroidBle library; you don't actually call `close()`, you unsubscribe from all Observables that have to do with that connection. In fact, I see the disconnect radio operation in the logs, so it appears to be disconnecting correctly. – Ken DeLong Jun 20 '17 at 21:35
1

The culprit is Spotify Connect. It watches for any disconnecting bluetooth device and immediately tries to connect to it. Sometimes it goes into an infinite loop, sometimes the connection cycle eventually stops. But we were able to rid ourselves of this devastating behavior by stopping SC, and we were able to make it happen on other phones by installing it.

See also Android BLE unexpectedly and repeatedly reconnects to peripheral

Ken DeLong
  • 929
  • 2
  • 8
  • 27
0

Make sure when you connect the Gatt Profile from your app you have auto connect flag set to false From Android documentation for BLE:-

Connecting to a GATT Server The first step in interacting with a BLE device is connecting to it— more specifically, connecting to the GATT server on the device. To connect to a GATT server on a BLE device, you use the connectGatt() method. This method takes three parameters: a Context object, autoConnect (boolean indicating whether to automatically connect to the BLE device as soon as it becomes available), and a reference to a BluetoothGattCallback:

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

Zakir
  • 2,222
  • 21
  • 31
  • 1
    If you properly call disconnect() on the BluetoothGatt object then it has no effect whether you set autoConnect to true or false. – Emil Jun 20 '17 at 19:58
  • @Emil: I was hinting at a potential bug on the remote side stack - caused by auto-connect in OP's App - so wanted to rule that out – Zakir Jun 20 '17 at 20:00
  • Autoconnect is false. As noted above, in RxAndroidBle, you don't call `disconnect()` directly; instead you unsubscribe from the Rx Observables that registered interest in that connection. – Ken DeLong Jun 20 '17 at 21:36