我正在尝试读取蓝牙耳机的电池电量(Skullcandy方法无线)。到目前为止,连接耳机时,我已成功接收连接广播,并通过bluetoothDevice.connectGatt()连接到GATT。
//getting device
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
在BluetoothGattCallback.onConnectionChanged上,我正在调用discoverServices()以发现蓝牙Battery_Service。但是服务列表始终为零。
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));
}
}
});