Android如何使用PriorityQueue读取多个BLE特征

2023-11-26

有点卡在这里,可能需要你的帮助。我想一次读取多个 BLE 特性,有些人建议使用 PriorityQueue。我已经知道所有的 uuid 等,只需要一种方法可以一次读取多个。 谁能解释一下它究竟应该是什么样子?或者也许还有另一个更简单的解决方案?

预先感谢,这是我的代码:

public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    PriorityQueue<BluetoothGattCharacteristic> queue = new PriorityQueue<BluetoothGattCharacteristic>();

    // When connection state changes
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        List<BluetoothGattService> services = gatt.getServices();
        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        List<UUID> uuidsList;

        UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid();
        UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid();
        UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid();
        UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid();
        //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid();

        uuidsList = new ArrayList<UUID>();

        uuidsList.add(TRANSMISSION_POWER);
        uuidsList.add(BROADCASTING_INTERVAL);
        uuidsList.add(BEACON_NAME);
        uuidsList.add(CONNECTION_MODE);
        //uuidsList.add(SOFT_REBOOT);

        queue.add(rightService.getCharacteristic(uuidsList.get(0)));
        queue.add(rightService.getCharacteristic(uuidsList.get(1)));
        queue.add(rightService.getCharacteristic(uuidsList.get(2)));

    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

        Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
        onServicesDiscovered(gatt, 0);

    }

};

UPDATE:

即使将它们放在不同的线程上,它仍然只对一个 gatt.readCharacteristic(...) 做出反应。像下面这样:

// Gatt Callback
public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    // When connection state changes
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {


        List<BluetoothGattService> services = gatt.getServices();

        /*
        DISPLAY ALL SERVICES AND CHARACTERISTICS

        for (int i = 0; i < services.size(); i++) {
            Log.v(TAG, "SERVICE____: " + services.get(i).getUuid());

            for (int k = 0; k < services.get(i).getCharacteristics().size(); k++) {
                Log.v(TAG, "CHARACTERISTIC____: " + services.get(i).getCharacteristics().get(k).getUuid());
            }

        }
        */

        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        List<UUID> uuidsList;

        UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid();
        UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid();
        UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid();
        UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid();
        //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid();

        uuidsList = new ArrayList<UUID>();

        uuidsList.add(TRANSMISSION_POWER);
        uuidsList.add(BROADCASTING_INTERVAL);
        uuidsList.add(BEACON_NAME);
        uuidsList.add(CONNECTION_MODE);
        //uuidsList.add(SOFT_REBOOT);


        class powerThread extends Thread{

            UUID uuid;
            BluetoothGatt gatt;
            BluetoothGattService service;
            public powerThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) {
                this.gatt = gatt;
                this.service = service;
                this.uuid = uuid;
            }
            @Override
            public void run() {
                gatt.readCharacteristic(service.getCharacteristic(uuid));
            }
        }
        powerThread pt = new powerThread(TRANSMISSION_POWER, gatt, rightService);
        pt.run();


        class intervalThread extends Thread{

            UUID uuid;
            BluetoothGatt gatt;
            BluetoothGattService service;
            public intervalThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) {
                this.gatt = gatt;
                this.service = service;
                this.uuid = uuid;
            }
            @Override
            public void run() {
                gatt.readCharacteristic(service.getCharacteristic(uuid));
            }
        }
        intervalThread it = new intervalThread(BROADCASTING_INTERVAL, gatt, rightService);
        it.run();


    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

        Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));

    }

};

对于任何可能遇到相同问题的人,这里有一个使用特征 List 的简单解决方案。

public static final BluetoothGattCallback readGattCallback = new BluetoothGattCallback() {

    List<BluetoothGattCharacteristic> chars = new ArrayList<>();

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            broadcastingInterval = 999;
            transmissionPower = 999;
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        List<BluetoothGattService> services = gatt.getServices();
        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        chars.add(rightService.getCharacteristics().get(4));
        chars.add(rightService.getCharacteristics().get(6));

        requestCharacteristics(gatt);

    }

    public void requestCharacteristics(BluetoothGatt gatt) {
        gatt.readCharacteristic(chars.get(chars.size()-1));
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {

            if (characteristic.getUuid().toString().substring(7, 8).equals("5")) {
                transmissionPower = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                Log.v(TAG, "tPOWER READ");

            } else if (characteristic.getUuid().toString().substring(7,8).equals("7")) {
                broadcastingInterval = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                Log.v(TAG, "INTERVAL READ");
            }

            chars.remove(chars.get(chars.size() - 1));

            if (chars.size() > 0) {
                requestCharacteristics(gatt);
            } else {
                gatt.disconnect();
            }
        }
    }

};
  1. 创建特征列表
  2. 在 onServicesDiscovered 中,使用您想要读/写的特征填充列表
  3. 创建一个名为 requestCharacteristics(gatt) 的新方法并将 gatt 对象传递给它。将特征添加到列表后,从 onServicesDiscovered 调用此方法。
  4. 在 requestCharacteristics() 方法中调用 gatt.readCharacteristic(chars.get(chars.size()-1));
  5. 在 onCharacteristicRead 中检查列表的大小是否不为零,然后读取您的特征,删除列表的最后一项并再次调用 requestCharacteristic()。
  6. 就这样
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android如何使用PriorityQueue读取多个BLE特征 的相关文章

  • Locale.getDefault().getCountry() 返回空字符串

    我正在尝试使用国家 地区代码获取用户语言 例如en US es es 但是当我使用Locale getDefault getCountry 它返回空字符串 虽然它给了我正确的语言Locale getDefault getLanguage N
  • 如何在 Android 上的 HttpPost 中发送 unicode 字符

    我试图在我的应用程序中允许多语言支持 这会发出 HTTP post 来上传新消息 我需要做什么才能支持日语和其他非拉丁语语言 我的代码目前看起来像这样 note the msg string is a JSON message by the
  • 尝试在没有 GatewayIntent 的情况下访问消息内容

    我希望每当我写一条打招呼的消息时 机器人都会在控制台中响应一条消息 但它只是给我一个错误 JDA MainWS ReadThread WARN JDA Attempting to access message content without
  • 如何在java中使jpeg无损?

    有没有人可以告诉我如何使用编写 jpeg 文件losslessjava中的压缩 我使用下面的代码读取字节来编辑字节 WritableRaster raster image getRaster DataBufferByte buffer Da
  • 版本 5 上带有 getBackground().setAlpha 的按钮 - 棒棒糖无法正常工作

    我有这段代码 适用于自 API 14 以来的每个版本 但在 Android 5 0 Lollipop 上无法正常工作 以下是我希望按钮出现的方式 单击按钮1 buttonArrivals getBackground setAlpha 180
  • 打印包含 JBIG2 图像的 PDF

    请推荐一些库 帮助我打印包含 JBIG2 编码图像的 PDF 文件 PDFRenderer PDFBox别帮我 这些库可以打印简单的 PDF 但不能打印包含 JBIG2 图像的 PDF PDFRenderer尝试修复它 根据 PDFRedn
  • Android:如何监控WiFi信号强度

    当信号强度发生变化时我会收到通知 我尝试创建以下方法并在 onCreate 中调用它 private void initializeWiFiListener Log i TAG executing initializeWiFiListene
  • onTouchEvent()中如何区分移动和点击?

    在我的应用程序中 我需要处理移动和单击事件 一次点击是由一个 ACTION DOWN 操作 多个 ACTION MOVE 操作和一个 ACTION UP 操作组成的序列 理论上 如果您收到 ACTION DOWN 事件 然后收到 ACTIO
  • 如何使用 Mockito 和 Junit 模拟 ZonedDateTime

    我需要模拟一个ZonedDateTime ofInstant 方法 我知道SO中有很多建议 但对于我的具体问题 到目前为止我还没有找到任何简单的解决办法 这是我的代码 public ZonedDateTime myMethodToTest
  • 点击监听器的公共类

    我的所有 6 项活动中有 7 个按钮 所有 6 个按钮在所有活动中都具有相同的功能 如何为这 6 个按钮执行通用的单击事件侦听器 您可以创建一个实现 View OnClickListener 的新类 如下所示 public class My
  • Android开发:未定义方法

    大家好 我是 Android 和 Eclipse 的新手 我刚刚遵循了developer android com 上的教程 现在我在添加操作栏 http developer android com training basics actio
  • 无法在 Android 模拟器中安装 apk

    我正在尝试通过 adb shell 在 ICS 模拟器中安装 apk 从一个站点下载 但出现以下错误 失败 INSTALL FAILED UID CHANGED 可能是什么问题 只需 rm r 有问题的数据目录即可 如果您在安装时遇到此错误
  • Android:如何使视图增长以填充可用空间?

    这看起来很简单 但我不知道该怎么做 我有一个带有 EditText 和两个 ImageButtons 的水平布局 我希望 ImageButtons 具有固定大小 并且 EditText 占据布局中的剩余空间 如何才能做到这一点
  • struts 教程或示例

    我正在尝试在 Struts 中制作一个登录页面 这个想法是验证用户是否存在等 然后如果有错误 则返回到登录页面 错误显示为红色 典型的登录或任何表单页面验证 我想知道是否有人知道 Struts 中的错误管理教程 我正在专门寻找有关的教程 或
  • Android:如何通过右侧的十字按钮清除EditText

    我创建了一个EditText用于搜索 左侧包含搜索图标 右侧包含图标
  • 如何将实例变量传递到 Quartz 作业中?

    我想知道如何在 Quartz 中外部传递实例变量 下面是我想写的伪代码 如何将 externalInstance 传递到此作业中 public class SimpleJob implements Job Override public v
  • Java中获取集合的幂集

    的幂集为 1 2 3 is 2 3 2 3 1 2 1 3 1 2 3 1 假设我有一个Set在爪哇中 Set
  • gradle-experimental:0.1.0 buildConfigField

    谁知道怎么定义buildConfigField在实验性的 gradle 插件中 android productFlavors create demo applicationId com anthonymandra rawdroid buil
  • Java 推断泛型类型

    我正在寻找类似的推断捕获泛型类型的概念 类似于以下方法片段 但不是捕获泛型类型的类 public
  • java中如何找到class文件的包

    我正在编写一个使用 class 文件的 java 程序 我希望能够读取文件系统上的 class 文件 使用 InputStream 并确定它所在的包 该 class 文件可能不在一个好的包目录结构中 它可能位于某个随机位置 我怎样才能做到这

随机推荐

  • Keras 似乎在调用 fit_generator 后挂起

    我正在尝试适应 Keras 的实现SqueezeDet模型到一个新的数据集 对配置文件进行适当的更改后 我尝试运行训练脚本 但它似乎在调用后挂起fit generator 当我得到以下输出时 anaconda envs py35 lib p
  • EF4 Code First:如何仅更新特定字段

    如何仅更新实体上的某些字段 我有一个像这样的用户实体 public class User public string UserId get set public string PasswordHash get set public bool
  • 永久 PowerShell 变量

    有没有办法在 PowerShell 中定义变量 以便当我打开新的 PowerShell 窗口时 它会保持相同的值 我需要这个变量来保留其值 因为我需要时不时地重新启动我的服务器 并且我不想丢失这些值 储藏 variable export c
  • 为什么 xdebug 没有出现在 phpinfo() 中

    我正在尝试进行以下设置工作 Windows 7 64 位 XAMPP 1 7 4 XDebug php xdebug 2 1 0 5 3 vc9 x86 64 dll 当我从 Xampps 主页运行 phpinfo 时 XDebug 它没有
  • 使用go静态文件服务器时如何自定义处理找不到文件?

    所以我使用 go 服务器来提供单页 Web 应用程序 这适用于为根路由上的所有资产提供服务 所有 CSS 和 HTML 均已正确提供 fs http FileServer http Dir build http Handle fs 所以当网
  • 在 CouchDB 中按键返回唯一值

    有没有办法在 CouchDB 中执行以下操作 一种通过给定键返回唯一 不同值的方法 SELECT DISTINCT field FROM table WHERE key key1 key1 gt somevalue key1 gt some
  • 具有有关文件的自定义元数据的 ItemGroup

    我正在尝试创建一个 文件 任务项组 其中包含名为 TargetPath 的元数据属性 其中填充了文件的相对路径 Example 对于这些路径 D 测试 Blah exeD 测试 配置 fun configD Test en US my re
  • 未找到类异常 com.squareup.okhttp.logging.HttpLoggingInterceptor

    即使在添加依赖项并导入类之后 我仍然收到 java lang NoClassDefFoundError com squareup okhttp logging HttpLoggingInterceptor 有人可以帮忙吗 Gradle 构建
  • 在 Angular 中更新/合并 i18n 翻译文件

    我们最近决定为我们的应用程序支持多种语言 Angular 13 x 经过研究 我们决定使用angular localize看起来很适合我们的需求的包 一切似乎都在解决唯一的问题 即在后续的构建和更改中保持翻译文件最新 因此 请遵循 Angu
  • 取消 DataAdapter.Fill()

    设想 我们有一个附加到 DataAdapter 数据表 的 DataGridView 我们在单独的线程 使用 delegate 和 beginInvoke 中使用 adapter fill query datatable 将数据加载到数据表
  • 通过 Node JS 使用文件内容确定 MIME 类型

    似乎所有流行的 Node js MIME 类型库都只是使用文件扩展名 而不是通过查看文件来确定 MIME 类型 有没有一种好方法可以使用 Node 跳转到文件并智能地确定文件的 MIME 类型 以防扩展名不存在 确实感觉很可惜 最受欢迎的M
  • SQL Server Raiserror 不会在 .NET 客户端中引起异常

    我在 SQL Server 2005 数据库上有一个存储过程 其中有如下语句 IF Condition 0 BEGIN RAISERROR some error message 16 1 RETURN END 它是从 C 客户端调用的 如下
  • 如何将 List 绑定到 gridview?

    这可能是一个非常奇怪的问题 因为通常人们只将复杂类型绑定到网格视图 但我需要绑定一个 Int 列表 对于字符串也是如此 通常 由于要绑定的属性使用对象的属性名称 但是当使用 Int 或 String 时 该值正是对象本身 而不是属性 获取对
  • __init__.py 的目的是什么? [复制]

    这个问题在这里已经有答案了 创建 Python 包时 我被告知创建一个名为的空白文件init py 我不明白的是为什么我需要创建这个文件 这distutils构建脚本不会修改它 所以五个构建后它仍然是空白的 它的目的是什么 它向 Pytho
  • 了解 CSS 表格单元格和百分比宽度

    我正在检查 Github 如何显示以下菜单 如果您注意到 每个菜单项都具有相同的宽度 在CSS中 我们应该给它任何百分比值 这背后的原因是什么 请注意 父 div 没有给出 display table 属性 div border 1px s
  • Android - 在活动中嵌入 Unity3d 场景 - 需要取消注册接收器?

    我成为 SO 成员已经有一段时间了 但从未真正问过问题 所以这里 My Aim 我正在尝试制作一个包含两个活动的 Android 应用程序 第一个是菜单屏幕 使用标准 Android UI 元素 其中有一个用于打开游戏活动的按钮 游戏活动将
  • 如何获取Android指南针读数?

    既然 SENSOR ORIENTATION 已弃用 那么获取罗盘航向的最佳做法是什么 老方法就是这么简单 以下是获取指南针方向并将其显示在 TextView 中的基本示例 它通过实现 SensorEventListener 接口来实现这一点
  • 更改引导程序中活动类的颜色

    我正在尝试更改 html 代码中活动类的颜色 我正在创建导航侧边栏 这是我的代码 div class col sm 2 ul class nav nav pills nav stacked nav static li class activ
  • javascript:使用 window.open() 发送自定义参数,但它不起作用

  • Android如何使用PriorityQueue读取多个BLE特征

    有点卡在这里 可能需要你的帮助 我想一次读取多个 BLE 特性 有些人建议使用 PriorityQueue 我已经知道所有的 uuid 等 只需要一种方法可以一次读取多个 谁能解释一下它究竟应该是什么样子 或者也许还有另一个更简单的解决方案