[英]Android BLE onCharacteristicChanged not triggering
I have an medical device(named as SnoreCoach), I have created an android app and did Bluetooth Low Energy connectivity with this device. 我有一个医疗设备(名为SnoreCoach),我创建了一个android应用,并与此设备进行了蓝牙低功耗连接。 Everything (connecting to device, writing data to characteristics etc.) is working fine except OnCharacteristicChanged is not getting triggered.
除了未触发OnCharacteristicChanged之外,其他所有东西(连接到设备,将数据写入特征等)都工作正常。
Following is my BluetoothGattCallback: 以下是我的BluetoothGattCallback:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
broadcastUpdate(ACTION_GATT_CONNECTED);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
broadcastUpdate(ACTION_GATT_DISCONNECTED);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onServicesDiscovered - success");
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onCharacteristicRead - success");
broadcastUpdate(ACTION_DATA_AVAILABLE);
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onCharacteristicWrite - success");
if (writeType.equals("unPair")) {
mBluetoothGatt.close();
mBluetoothGatt = null;
currentlyConnectedPeripheral = null;
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.d(TAG, "onCharacteristicChanged");
broadcastUpdate(ACTION_DATA_AVAILABLE);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.d(TAG, "onReliableWriteCompleted");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.d(TAG, "onReadRemoteRssi");
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.d(TAG, "onMtuChanged");
}
};
Following is the "broadcastUpdate" function called from above BluetoothGattCallback: 以下是从上述BluetoothGattCallback调用的“ broadcastUpdate”函数:
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
In my calling activity (SettingsActivity) I have following broadcast receiver: 在我的通话活动(SettingsActivity)中,我具有以下广播接收器:
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case BLEService.ACTION_GATT_CONNECTED:
displayToast("Connected");
Log.d(TAG, "Connected");
mBleService.discoverServices();
break;
case BLEService.ACTION_GATT_DISCONNECTED:
displayToast("Disconnected");
break;
case BLEService.ACTION_GATT_SERVICES_DISCOVERED:
Log.d(TAG, "Services Discovered");
displayToast("Services Discovered");
mBleService.enableNotification();
break;
case BLEService.ACTION_DATA_AVAILABLE:
displayToast("Data Received");
break;
}
}
};
And below is my "enableNotification" functions: 下面是我的“ enableNotification”函数:
public void enableNotification() {
BluetoothGattService snorecoachService = mBluetoothGatt.getService(SNORECOACH_SERVICE_UUID);
if (snorecoachService == null) {
Log.d(TAG, "snorecoach Service not found!");
return;
}
BluetoothGattCharacteristic bodyMovementChar = snorecoachService.getCharacteristic(BODY_MOVEMENT_UUID);
if (bodyMovementChar == null) {
Log.d(TAG, "charateristic not found!");
return;
}
mBluetoothGatt.setCharacteristicNotification(bodyMovementChar, true);
BluetoothGattDescriptor desc = bodyMovementChar.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(desc);
}
Following is the flow : 以下是流程:
1) On connection, I send an broadcast as "connected" to mGattUpdateReceive, here I then start service discovery. 1)在连接上,我将广播作为“已连接”发送到mGattUpdateReceive,然后在此处开始服务发现。
2) OnServiceDiscovery, I send an broadcast as "Services Discovered" to mGattUpdateReceive, here I call "enable notofication" function to set "setCharacteristicNotification", which writes the required descriptor too. 2)OnServiceDiscovery,我将广播作为“发现的服务”发送到mGattUpdateReceive,在这里我称为“启用通知”功能以设置“ setCharacteristicNotification”,该函数也写入所需的描述符。
I have tried all possible options I found from other stackOverflow questions, But I don't know what I am doing wrong that the onCharacteristic event is not getting triggered. 我已经尝试了从其他stackOverflow问题中发现的所有可能选项,但是我不知道onCharacteristic事件没有被触发而做错了什么。
I have spent more than 2 days for solving this but no luck, so any help would be greatly appreciated. 我花了超过2天的时间解决了这个问题,但没有运气,因此,我们将不胜感激。
If it is notifications you want rather than indications, try to change 如果是您想要的通知而不是指示,请尝试更改
desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
to 至
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.