我正在尝试连接蓝牙健康温度计。我可以找到设备并连接到该设备,但是当我想读取特征时,readCharacteristics调用将返回false。
我的代码是:
private void connectDevice(BluetoothDevice bluetoothDevice) {
mBluetoothGatt = bluetoothDevice.connectGatt(getContext(), true, mGattCallback);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
if(deviceName != null) {
Device btDevice = new Device(device);
if(mDevices.contains(btDevice)) {
mDevices.remove(btDevice);
}
mDevices.add(btDevice);
mDeviceAdapter.notifyDataSetChanged();
}
Log.i("Device Name: " , "device " + deviceName);
Log.i("deviceHardwareAddress " , "hard" + deviceHardwareAddress);
}
}
};
BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Timber.d("Connected to GATT client. Attempting to start service discovery");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Timber.d("Disconnected from GATT client");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status != GATT_SUCCESS) {
// Handle the error
return;
}
// Get the counter characteristic
BluetoothGattCharacteristic characteristic = gatt
.getService(HEALTH_THERMOMETER_SERVICE_UUID)
.getCharacteristic(HEALTH_THERMOMETER_CHARACTERISTIC_UUID);
// Enable notifications for this characteristic locally
gatt.setCharacteristicNotification(characteristic, true);
// Write on the config descriptor to be notified when the value changes
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(DESCRIPTOR_CONFIG_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
readCounterCharacteristic(characteristic);
}
private void readCounterCharacteristic(BluetoothGattCharacteristic
characteristic) {
if (HEALTH_THERMOMETER_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
byte[] data = characteristic.getValue();
Timber.d(data.toString());
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
if (DESCRIPTOR_CONFIG_UUID.equals(descriptor.getUuid())) {
BluetoothGattCharacteristic characteristic = gatt
.getService(HEALTH_THERMOMETER_SERVICE_UUID)
.getCharacteristic(HEALTH_THERMOMETER_CHARACTERISTIC_UUID);
boolean read = gatt.readCharacteristic(characteristic);
Timber.d(String.valueOf(read));
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Timber.d("onCharacteristicChanged");
}
};