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));
}
}
});