Android BLE device disconnects after short time, error code 8

Viewed 798

I have an Android app which can connect to a BLE device (using BMD-350), receive data from it via notifications and transmit data on a characteristic. I don't know why, but after a while the BLE device disconnects on its own, reporting error code 8 in the BondStateChanged callback. I can see data coming in to the phone right up until it disconnects. The BLE device sends data to the phone around 550 bytes/sec. The phone does not send any data to the BLE device in this time. Why is it disconnecting?

*note: This code is written in C# (Xamarin), but it's 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 Receive Notifications:

_bleGatt.SetCharacteristicNotification(_rx, true);

Reading Characteristic:

public override void OnCharacteristicChanged(BluetoothGatt gatt, 
BluetoothGattCharacteristic characteristic)
{
    base.OnCharacteristicChanged(gatt, characteristic);
    PublishNewData(characteristic.GetValue());
}

UUID's/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");
0 Answers
Related