【发布时间】:2016-04-14 15:54:33
【问题描述】:
所以我有一个带有自定义 UUID 的 BLE 外围设备,我可以很好地连接到它。我可以使用以下方式读取自定义特征:
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
}
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
}
else {
if(characteristic.getValue()!=null){
final int data2 = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
intent.putExtra("Right",String.valueOf(data2));}
}
sendBroadcast(intent);
}
但如果我想指定如何处理特定特征:
public final static UUID chara = UUID.fromString("custom 128bit UUID");
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
if(chara.equals(characteristic.getUuid())){
if(characteristic.getValue()!=null){
final int data2 = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
intent.putExtra("Right",String.valueOf(data2));}
}
sendBroadcast(intent);
}
我收到错误接收广播意图.....原因:java.lang.NumberFormatException: Invalid int: "null"
谁能告诉我我做错了什么?
【问题讨论】:
-
我也面临同样的问题。你找到解决办法了吗?
标签: android bluetooth bluetooth-lowenergy