使用蓝牙 SIG 应用加速器代码,它很好地演示了蓝牙低功耗的不同概念。然而,它没有提到与通知相反的指示。我知道与通知不同,需要确认指示,并且在代码中我会这样做byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_INDICATION_VALUE;
代替byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
。我还有什么需要做的吗?我到底如何让服务器知道我收到了所需的指示?有什么我需要添加的吗?
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
{
notification_id++;
Log.d("BleWrapper","notification count = " + notification_id);
// characteristic's value was updated due to enabled notification, lets get this value
// the value itself will be reported to the UI inside getCharacteristicValue
getCharacteristicValue(characteristic);
// also, notify UI that notification are enabled for particular characteristic
mUiCallback.uiGotNotification(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic);
}
你的描述已经足够了,但是有一个小错误。
事实上,BLE 指示需要客户端确认,而通知则不需要。然而,这完全是由 Android 在幕后处理的。系统会将指示确认为您的onCharacteristicChanged
回调被调用。
The only difference, which you already found out about, is that you need to enable the right flag in the Client Characteristic Configuration descriptor on the BLE server. For regular notifications, use ENABLE_NOTIFICATION_VALUE
. For indications, use ENABLE_INDICATION_VALUE
. Note that you disable both by writing DISABLE_NOTIFICATION_VALUE
. The DISABLE_INDICATION_VALUE
that you mentioned does not exist, as per the documentation!
在Android端,使用就足够了BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic,%20boolean) where enable = true
。这适用于通知和指示。在这两种情况下,您的onCharacteristicChanged
将使用回调。
(你现在可能已经明白了这一点,但无论如何都要发帖,以防有人通过谷歌来到这里。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)