[英]Bluetooth Low Energy Android Studio
为什么它不起作用? “#”已正确发送但未被读取(另一台设备已读取并发送了字符)。 我想我无法同时执行以下两个功能,但为什么呢?
Read_Data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Write
Data_BLE_Write("#");
//Read
Data_BLE_Read();
Toast.makeText(getApplicationContext(), "Data !!", Toast.LENGTH_SHORT).show();
}
});
但是,如果我用两个按钮分隔Data_Ble_Read和Data_Ble_Write,它会运行,所以我不明白为什么? 我的职能:
private void Data_BLE_Write(String Caract){
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic_W =
mGattCharacteristics.get(3).get(1);
final int charaProp = characteristic_W.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic_W;
mBluetoothLeService.setCharacteristicNotification(
characteristic_W, true);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
String str = Caract;
byte[] strBytes = str.getBytes();
characteristic_W.setValue(strBytes);
mBluetoothLeService.writeCaracteristic(characteristic_W);
}
}
}
private String Data_BLE_Read(){
Data_Read_Ble = "";
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(2).get(6);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
Data_BLE_Write("&");
}
return Data_Read_Ble;
}
ble堆栈只能同时允许一个任务,因此,基本上,您将中止写入任务和读取任务。 您应该等待下一个任务,直到之前的任务完成为止;在这种情况下,您应该等到读取属性后,再开始写入。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.