简体   繁体   English

无法从BLE设备读取,写入正常(Android)

[英]Unable to Read from BLE Device, Write is working (Android)

I am trying to read values from BLE device. 我正在尝试从BLE设备读取值。 Steps I followed: 我遵循的步骤:

  1. I am able to discover the BLE device and connect to it. 我能够发现BLE设备并连接到它。
  2. I am able to find the required characteristic by parsing through the services and get the GattCharacteristic. 我可以通过解析服务找到所需的特征并获取GattCharacteristic。
  3. I am able to write a value to the BLE device characteristic and that's verified. 我能够将值写入BLE设备特性,并且已经过验证。
  4. The properties for that characteristic that I am trying to read the value from are: READ, WRITE and INDICATE/NOTIFY. 我尝试从中读取值的该特性的属性为:READ,WRITE和INDICATE / NOTIFY。

The functions I am using for read and write are as follows: 我用于读写的功能如下:

a) Read function: a)读取功能:

 public void readCharacteristic(BluetoothGattCharacteristic characteristic) 
{
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    boolean status;
Log.w(TAG, "SCBABLe: Writing BLuetooth");
status=mBluetoothGatt.readCharacteristic(characteristic);

if(status) {

    Log.w(TAG, "Read Successfully");
}

else
{
    Log.w(TAG, "Read Unsuccessfully");

}

   }

b) Write function b)写功能

public void writeCharacteristic(BluetoothGattCharacteristic characteristic)
{
    if (mBluetoothAdapter == null || mBluetoothGatt == null)
    {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    boolean status;
    Log.w(TAG, "SCBABLe: Writing BLuetooth");
    status=mBluetoothGatt.writeCharacteristic(characteristic);

    if(status) {

        Log.w(TAG, "Write Successfully");
    }

    else
    {
        Log.w(TAG, "Write Unuccessfully");

    }
}

I am calling the above two from a different activity, after I get the required characteristic(by matching the UUIDs). 在获得所需的特征后(通过匹配UUID),我从不同的活动中调用上述两者。

//Current Activity Name: DeviceControlActivity
//BluetoothLeService is the other activity where Read and write are defined


private static final DeviceControlActivity holder1 = new DeviceControlActivity();

public BluetoothGattCharacteristic reqChar;
public BluetoothLeService reqService;
private BluetoothLeService mBluetoothLeService;

private void displayGattServices(List<BluetoothGattService> gattServices) 
{
  if (gattServices == null) return;
    String uuid = null;
  for (BluetoothGattService gattService : gattServices) 
    {
        uuid = gattService.getUuid().toString();
        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) 
        {

                uuid = gattCharacteristic.getUuid().toString();
        //SampleGattAttributes.UNKNOWN_CHARACTERISTIC is the hardcoded uuid against which i am checking     
       if((uuid.equals((SampleGattAttributes.UNKNOWN_CHARACTERISTIC))))
       {
          holder1.reqChar = gattCharacteristic;
          holder1.reqService = mBluetoothLeService;

          //Call for write
          byte [] byte1= {0x01, 0x10};

          holder1.reqChar.setValue(byte1);
          holder1.reqService.writeCharacteristic(holder1.reqChar);

          //Call for read

          holder1.reqService.readCharacteristic(holder1.reqChar);

Result: Read function is returning false and write function is returning true so the value is getting written successfully for the required characteristic.(verified it) 结果:读取函数返回false,写入函数返回true,因此已成功写入所需特性的值。(已验证)

Please, could anyone help and tell why the read is not getting executed? 请,任何人都可以帮助并告知为什么读取未执行的原因吗? Why is it still returning false when it has Read as property and proper value defined? 当它具有Read as属性并定义了正确的值时,为什么仍返回false?

The problem is in these few lines. 问题出在这几行。

holder1.reqService.writeCharacteristic(holder1.reqChar);

//Call for read

holder1.reqService.readCharacteristic(holder1.reqChar);

After calling writeCharacteristic, if you do any extra read/write/etc. 调用writeCharacteristic之后,是否还要进行其他读/写/其他操作。 , the call will not be executed and return false. ,该调用将不会执行并返回false。 You have to wait for BluetoothGattCallback.onCharacteristicWrite before doing further operation. 您必须等待BluetoothGattCallback.onCharacteristicWrite才能执行进一步的操作。

In the Android BLE implementation, the gatt operation calls need to be queued so that only one operation (read, write, etc.) is in effect at a time. 在Android BLE实现中,需要对gatt操作调用进行排队,以便一次仅执行一个操作(读,写等)。 So for example, after gatt.readCharacteristic(characteristicX) is called, you need to wait for the gatt callback BluetoothGattCallback.onCharacteristicRead() to indicate the read is finished. 因此,例如,在gatt.readCharacteristic(characteristicX)之后,您需要等待gatt回调BluetoothGattCallback.onCharacteristicRead()指示读取已完成。 If you initiate a second gatt.readCharacteristic() operation before the previous one completes, the second one will fail (by returning false) This goes for all of the gatt.XXX() operations. 如果在上一个操作完成之前启动第二个gatt.readCharacteristic()操作,则第二个操作将失败(返回false)。这适用于所有gatt.XXX()操作。

Its a little work, but I think the best solution is to create a command queue for all the gatt operations and run them one at a time. 它的工作量很小,但是我认为最好的解决方案是为所有gatt操作创建一个命令队列,并一次运行一次。 You can use the command pattern to accomplish this. 您可以使用命令模式来完成此操作。

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

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