2

I'm trying to read battery level of my bluetooth headphones(Skullcandy Method Wireless). So far, i'm successful in receiving connectivity broadcast when headphones are connected, connect to GATT via bluetoothDevice.connectGatt().

//getting device
  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

On BluetoothGattCallback.onConnectionChanged, i'm calling discoverServices() to discover bluetooth Battery_Service. But the list of services is always zero.

BluetoothGatt bluetoothGatt = device.connectGatt(context, false, new BluetoothGattCallback() {

            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                Logger.log(A2dpConnectionStateReceiver.class, "onConnectionStateChange "+newState);
                if(newState == BluetoothProfile.STATE_CONNECTED) {
                    Logger.log(A2dpConnectionStateReceiver.class, "onConnectionStateChange state connected, triggering discover services " + gatt.discoverServices());
                }
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                Logger.log(A2dpConnectionStateReceiver.class, "onServicesDiscovered count = "+gatt.getServices().size()+" status="+ (status == BluetoothGatt.GATT_SUCCESS? "success" : "failed") );

            }

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                Logger.log(A2dpConnectionStateReceiver.class, "onCharacteristicChanged "+characteristic.getUuid());
                if(characteristic.getUuid().equals(Battery_Level_UUID)) {
                    Logger.log(A2dpConnectionStateReceiver.class, "onCharacteristicChanged battery level at "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
                }
            }
        });
Siva
  • 355
  • 1
  • 6
  • 19
  • BLE is DIFFERENT from BLUETOOTH, so have u checked if ur headphones have his feature or not? – Anjani Mittal Aug 17 '18 at 06:48
  • @AnjaniMittal my device does not support BLE. on reading through the specs here https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.battery_service.xml ..battery service supports classic transport apart from Low energy. Am i misinterpreting this? – Siva Aug 17 '18 at 07:02
  • 1
    According to what I read -> " GATT is an acronym for the Generic Attribute Profile, and it defines the way that two Bluetooth Low Energy devices transfer data back and forth using concepts called Services and Characteristics. " as u can see BLE Devices & in the link I saw GATT Requirements, so the device has to have BLE (BLE being an upgraded version of Bluetooth)...... – Anjani Mittal Aug 17 '18 at 09:15
  • check this link also https://www.plugintoiot.com/bluetoothclassic-vs-bluetoothlowenergy/ – Anjani Mittal Aug 17 '18 at 09:40
  • Thanks for the links. Well, it seems there is no standard way do achieve this. https://github.com/wangrunz/BluetoothMonitor and https://github.com/aosp-mirror/platform_frameworks_base/commit/96f98f255ba794441883377d81d7a9d4c11967c0#diff-abc02213082938a215d4eb5c60add4af – Siva Aug 17 '18 at 11:20

0 Answers0