gpt4 book ai didi

java - Android BLE onCharacteristicChanged 未触发

转载 作者:行者123 更新时间:2023-12-01 09:11:11 26 4
gpt4 key购买 nike

我有一个医疗设备(名为 SnoreCoach),我创建了一个 Android 应用程序并与该设备进行低功耗蓝牙连接。一切(连接到设备、将数据写入特征等)都工作正常,除了 OnCharacteristicChanged 没有被触发。

以下是我的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");
}
};

以下是从上面的BluetoothGattCallback调用的“broadcastUpdate”函数:

private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}

在我的通话 Activity (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;
}
}
};

下面是我的“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);
}

以下是流程:

1)连接时,我将广播作为“已连接”发送到 mGattUpdateReceive,然后在这里开始服务发现。

2)OnServiceDiscovery,我将广播作为“Services Discovered”发送到mGattUpdateReceive,这里我调用“enable notofication”函数来设置“setCharacteristicNotification”,它也会写入所需的描述符。

我已经尝试了从其他 stackOverflow 问题中找到的所有可能选项,但我不知道我做错了什么,导致 onCharacteristic 事件没有被触发。

我花了 2 天多的时间来解决这个问题,但没有成功,因此我们将不胜感激。

最佳答案

如果您想要的是通知而不是指示,请尝试更改

desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

关于java - Android BLE onCharacteristicChanged 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40913751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com