I have an Android application that can connect to a BLE device (using the BMD-350), receive data from it through notifications and transmit data by characteristic. I donβt know why, but after a while, the BLE device shuts down on its own, reporting error code 8 in the BondStateChanged callback. I can see the data arriving on the phone, right up to the disconnection. The BLE device sends data to the phone at about 550 bytes per second. Currently, the phone does not send data to the BLE device. Why is it disconnecting?
* note: This code is written in C # (Xamarin), but it is basically the same as its Java counterpart.
Connection Code:
_bleGatt = device.ConnectGatt(context, false, this);
Connection callback:
if (newState == ProfileState.Connected) { if (status == GattStatus.Success) { gatt.DiscoverServices(); } }
Service Discovery:
public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status) { base.OnServicesDiscovered(gatt, status); BluetoothGattService mService; try { mService = gatt.GetService(UART_UUID); } catch { BluetoothConnectionError("UART service not found", gatt.Device); return; } _tx = mService.GetCharacteristic(TX_UUID); _rx = mService.GetCharacteristic(RX_UUID); if (_tx == null) { BluetoothConnectionError("Tx characteristic not found", gatt.Device); return; } if ((_tx.Properties | GattProperty.WriteNoResponse) == 0 && (_tx.Properties | GattProperty.Write) == 0) { BluetoothConnectionError("Tx characteristic not not in 'write' or 'write - no response' mode", gatt.Device); return; } if (_rx == null) { BluetoothConnectionError("Rx characteristic not found", gatt.Device); return; } if ((_rx.Properties | GattProperty.Notify) == 0) { BluetoothConnectionError("Rx characteristic not in 'notify' mode", gatt.Device); return; } BluetoothGattDescriptor mDescriptor = _rx.GetDescriptor(CLIENT_UUID); if (mDescriptor == null) { BluetoothConnectionError("Could not get descriptor", gatt.Device); return; } mDescriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray()); try { gatt.WriteDescriptor(mDescriptor); } catch { BluetoothConnectionError("Could not write descriptor", gatt.Device); return; } ConnectionStatusChanged(ProfileState.Connected, gatt.Device); }
Enabling receipt notifications:
_bleGatt.SetCharacteristicNotification(_rx, true);
Reading rate:
public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { base.OnCharacteristicChanged(gatt, characteristic); PublishNewData(characteristic.GetValue()); }
UUID in / CCCD:
private readonly UUID UART_UUID = UUID.FromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); private readonly UUID RX_UUID = UUID.FromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); private readonly UUID TX_UUID = UUID.FromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"); private readonly UUID CLIENT_UUID = UUID.FromString("00002902-0000-1000-8000-00805f9b34fb");