Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GATT Services returns Null after many successful connection

I am developing an Android app which connects to the BLE devices, Device does connect most of the time and i am able to capture the readings. But sometime after many connects disconnects, Bluetooth On/Off, my BluetoothGattCallback class method

onServicesDiscovered(BluetoothGatt gatt, int status)

with status 0 [which means GATT_SUCCESS].

now when i try to get the BluetoothGattService like:

BluetoothGattService service = gatt.getService(getServiceUUID());

it return null, so that i am unable to perform next steps. Please help me to find the problem.

like image 271
jitain sharma Avatar asked Sep 30 '22 21:09

jitain sharma


1 Answers

You have to first discover all services for the given device, otherwise when you run BluetoothGattService service = gatt.getService(getServiceUUID()); it will return null.

I would recommend you add the onServicesDiscovered function and use gatt.discoverServices(); instead.

@Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            for (BluetoothGattService gattService : gattServices) {
                Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
            }
        }
    }
like image 107
Omar Avatar answered Oct 04 '22 08:10

Omar