为什么它不起作用? “#”正确发送但未读取(其他设备读取字符并发送)。我想我不能在同一时间执行以下两个功能,但为什么呢?
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;
}
答案 0 :(得分:1)
ble堆栈只允许同时执行一个任务,因此基本上您将使用读取任务中止写入任务。您应该等待下一个任务,直到前面的任务完成,在您的情况下,您应该等到读取属性,然后再开始写入。
希望这有帮助。