Mag*_*gic 6 android characteristics bluetooth-lowenergy android-bluetooth android-4.3-jelly-bean
我想将我的远程BLE设备的特定数据的数据读取到我的Android平板电脑Nexus 7.
问题是,即使没有打电话,我也可以通过启用该特性的通知来接收数据readCharacteristic
.但是,readCharacteristic
如果不启用通知,我无法通过调用成功读取特征.
mBluetoothGatt.readCharacteristic(characteristic)
返回false.因此,该功能onCharacteristicRead
从未被触发过.我还检查了属性值BluetoothGattCharacteristic.PROPERTY_READ
,它是30.
有没有人对这里发生的事情有所了解?我真的需要分别阅读这个特性.因为如果我只根据通知分析数据,我无法弄清楚数据的开始位置.这是因为我的设备每次都会发送12bytes.它会不断发送字节数组.但是,通知会一次一个字节地给我带来数据.所以我不知道哪个是字节数组的起始字节.
我正在使用Android提供的示例代码.
这是片段:
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean status = mBluetoothGatt.readCharacteristic(characteristic);
System.out.println("Initialize reading process status:" + status);
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
Run Code Online (Sandbox Code Playgroud)
回调中的代码是:
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
System.out.println("In onCharacteristicRead!!!!!!!");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
System.out.println("Received Data Success!!!!!!");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了阅读特征的文件,但没有任何帮助.谁能帮我?非常感谢你!
归档时间: |
|
查看次数: |
10897 次 |
最近记录: |