简体   繁体   English

在Android中覆盖GATT后的BLE答案

[英]BLE Answer after writing over GATT in Android

Is there a way to come an answer after writing a hex command via Bluetooth Low Energy in android? 在Android中通过Bluetooth Low Energy编写十六进制命令后,有没有办法得到答案? I write the hex command over gatt, this is my write function: 我通过gatt编写了十六进制命令,这是我的写函数:

/* set new value for particular characteristic */
public void writeDataToCharacteristic(final BluetoothGattCharacteristic ch, final byte[] dataToWrite) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null || ch == null) return;

    // first set it locally....
    ch.setValue(dataToWrite);
    // ... and then "commit" changes to the peripheral
    mBluetoothGatt.writeCharacteristic(ch);
}

After the write is completed the Callback tells me only it's succesfull or it's failed, but the receiver sends an answer back. 写入完成后,回叫只会告诉我成功或失败,但是接收方会回送答复。 In moment there is only the check it's succesfull or not, but I wan't to display the answer from receiver. 暂时只有支票是否成功,但我不想显示接收者的答案。 Is there a way to show the answer? 有没有办法显示答案?

    /*The callback function*/
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        String deviceName = gatt.getDevice().getName();
        String serviceName = BleNamesResolver.resolveServiceName(characteristic.getService().getUuid().toString().toLowerCase(Locale.getDefault()));
        String charName = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString().toLowerCase(Locale.getDefault()));
        String description = "Device: " + deviceName + " Service: " + serviceName + " Characteristic: " + charName;

        // we got response regarding our request to write new value to the characteristic
        // let see if it failed or not
        if(status == BluetoothGatt.GATT_SUCCESS) {
             mUiCallback.uiSuccessfulWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description);
        }
        else {
             mUiCallback.uiFailedWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description + " STATUS = " + status);
        }
    };

In your callback have you tried 在回调中您是否尝试过

characteristic.getValue() ?

If this value is different from the one you set and sent across this could be the response you're looking for. 如果此值与您设置并发送的值不同,则可能是您正在寻找的响应。

Also, make sure that the Characteristic you're writing to has read or notifiable properties. 另外,请确保您要写入的特征具有已读取或可通知的属性。 You can do this as follows: 您可以按照以下步骤进行操作:

int props = characteristic.getProperties();
String propertiesString = String.format("0x%04X ", props);
if((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) propertiesString += "read ";
if((props & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) propertiesString += "write ";
if((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) propertiesString += "notify ";
if((props & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) propertiesString += "indicate ";

It's possible there are two Characteristics for a service - one that's writeable (for sending data) and one that's Notifiable for receiving data. 服务可能有两个特征-一个可写(用于发送数据)和一个可通知(Notifiable)用于接收数据。 That makes it possible for the receiver to do asynchronous processing. 这使得接收器可以执行异步处理。 Make sure that there isn't another Characteristic in the Service that's Notifiable or Readable 确保服务中没有其他可通知或可读的特征

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM