gpt4 book ai didi

java - 低功耗蓝牙 Android Studio

转载 作者:行者123 更新时间:2023-12-01 09:56:47 26 4
gpt4 key购买 nike

为什么不起作用?“#”正确发送但未读取(对方设备读取该字符并发送)。我认为我无法同时执行以下两个函数,但为什么?

    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 堆栈只允许同时执行一个任务,因此基本上您是在中止写入任务和读取任务。您应该等待下一个任务,直到之前的任务完成,在您的情况下,您应该等到读取属性,然后再开始写入。

希望这有帮助。

关于java - 低功耗蓝牙 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37159426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com