无法从 BLE 设备读取,写入正在运行 (Android)

编程入门 行业动态 更新时间:2024-10-25 10:27:40
本文介绍了无法从 BLE 设备读取,写入正在运行 (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试从 BLE 设备读取值.我遵循的步骤:

I am trying to read values from BLE device. Steps I followed:

我能够发现 BLE 设备并连接到它.我能够通过解析来找到所需的特征服务并获取 GattCharacteristic.我能够为 BLE 设备特性写入一个值,那就是已验证.我试图读取的那个特征的属性值来自:读取、写入和指示/通知.

我用于读写的函数如下:

The functions I am using for read and write are as follows:

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) 写函数

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");

    }
}

在获得所需特征后(通过匹配 UUID),我从不同的活动中调用上述两个.

I am calling the above two from a different activity, after I get the required characteristic(by matching the UUIDs).

//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);

结果: 读取函数返回 false,写入函数返回 true,因此已成功写入所需特征的值.(已验证)

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)

请问,谁能帮忙告诉为什么读取没有被执行?为什么当它定义了 Read as 属性和正确的值时它仍然返回 false?

推荐答案

问题就在这几行.

holder1.reqService.writeCharacteristic(holder1.reqChar);

//Call for read

holder1.reqService.readCharacteristic(holder1.reqChar);

在调用 writeCharacteristic 后,如果您执行任何额外的读/写/等操作.,调用不会被执行并返回false.您必须等待 BluetoothGattCallback.onCharacteristicWrite 才能进行进一步操作.

After calling writeCharacteristic, if you do any extra read/write/etc. , the call will not be executed and return false. You have to wait for BluetoothGattCallback.onCharacteristicWrite before doing further operation.

这篇关于无法从 BLE 设备读取,写入正在运行 (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:42:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正在运行   设备   BLE   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!