[英]onCharacteristicChanged is not called in Android BLE GATT servcies
I am trying to receive the data from BLE device in my app. 我正在尝试从我的应用程序中的BLE设备接收数据。 i could able to connect the BLE device successfully with my app and i am able to find the services provided by the BLE devices. 我能够将BLE设备成功连接到我的应用程序,并且能够找到BLE设备提供的服务。
I can able to write data in characteristics and able to enable notification for particular service successfully and its returning TRUE . 我能够在特征中写入数据,并且能够成功地启用特定服务的通知及其返回的TRUE 。 Issue is after gatt.writeCharacteristic(characteristic) successfully it should call onCharacteristicChanged override method. 问题是gatt.writeCharacteristic(characteristic)成功执行后,应调用onCharacteristicChanged重写方法。 But its not calling that method. 但是它没有调用该方法。 from that method only i can able to receive the data from BLE device. 从这种方法中,我只能从BLE设备接收数据。
I Followed below url 我跟随以下网址
Note: By using service , i am invoking establishing GATT connection. 注意:通过使用service,我正在调用建立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);
}
}
}
}
}
No. onCharacteristicChanged is not called after you write a value to a characteristic. 否。将值写入特征后,不会调用onCharacteristicChanged。 onCharacteristicWrite is called. 调用onCharacteristicWrite。
onCharacteristicChanged is called when a notification or indication has been received. 当收到通知或指示时,将调用onCharacteristicChanged。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.