【发布时间】:2017-04-02 03:53:40
【问题描述】:
我正在尝试在我的应用中接收来自 BLE 设备的数据。我可以成功地将 BLE 设备与我的应用程序连接起来,并且我能够找到 BLE 设备提供的服务。
我可以在特征中写入数据,并能够成功启用特定服务的通知及其返回TRUE。问题是在 gatt.writeCharacteristic(characteristic) 成功后它应该调用 onCharacteristicChanged 覆盖方法。但它没有调用该方法。 只有通过这种方法,我才能从 BLE 设备接收数据。
我关注了以下网址
注意: 通过使用服务,我正在调用建立 GATT 连接。
private class BleGattCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.d(TAG, "BluetoothProfile.STATE_DISCONNECTED");
gatt.close();
break;
default:
break;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "onServicesDiscovered");
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(READING_UUID);
if (characteristic != null) {
Log.d(TAG, " Subscribe Characteristic Notification UUID : "+characteristic.getUuid());
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
boolean success = gatt.writeDescriptor(descriptor);
Log.d(TAG, "writeDescriptor Status : " + success);
}
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Toast.makeText(mContext,"onCharacteristicChanged",Toast.LENGTH_LONG).show();
// read Value
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(INDEX_UUID);
Log.d(TAG, "onDescriptorWrite success UUID for "+characteristic.getUuid());
if (characteristic != null) {
characteristic.setValue(new byte[] {0x03, 0x00});
gatt.writeCharacteristic(characteristic);
}
}
}
}
}
【问题讨论】:
标签: android bluetooth-lowenergy android-bluetooth gatt android-service-binding