Android 14 Service Discovery Issue

272 Views Asked by At

I have a .jar library that I am using in another project.

Something has changed between Android 13 and Android 14 that is causing this issue.

In my library I scan for and connect to a BLE device.

During the callback to the library from the Android OS I have the following code:

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        androidBluetoothLog("onConnectionStateChange");

        String deviceAddress = gatt.getDevice().getAddress();

        if (newState == BluetoothProfile.STATE_CONNECTED) {

            removeConnectingDevice(deviceAddress, false);

            // start discovering services
            gatt.discoverServices();

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

            removeConnectingDevice(deviceAddress, false);

            gatt.close();
        }
    }

As you can see I call the gatt.discoverServices(); This should enumerate the services and characteristics and then call the onServicesDiscovered callback.

My onServicesDiscovered looks like this:

    @Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        if (status == BluetoothGatt.GATT_SUCCESS) {

            androidBluetoothLog("Services Discovered");

            List<BluetoothGattService> services = gatt.getServices();
            if (services != null) {
                androidBluetoothLog("Service Count: " + services.size());

                String deviceAddress = gatt.getDevice().getAddress();
                for (BluetoothGattService service : services) {

                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    for (BluetoothGattCharacteristic characteristic : characteristics) {
                        androidBluetoothLog("Characteristic found");
                    }
                }
            }
        } else {

            androidBluetoothLog("Error~Service Discovery " + status);
        }
    }

When run on Android 13 this all works correctly. Service count is > 0 and I can enumerate all services and characteristics.

When I run the exact same code on Android 14, the service count is always 0.

I have tried adding delays here and there using sleep, but nothing seems to work.

What changed between Android 13 and Android 14 that could cause this?

0

There are 0 best solutions below