BluetoothgattCallback on characteristicRead从未回电

发布于 2025-01-31 07:22:27 字数 3268 浏览 2 评论 0原文

我仍在努力从Android(使用Java)的BLE设备(激光仪表)中读取 基本上,我正在尝试获取我感兴趣的字段(又称措施),

到目前为止,我发现了5个服务(我找到了使用 there ):

  • 3ab10100-f831-4395-b29d-570977dbf94(我不能找出这是什么坏男孩使用)
  • 0x1800->可能是我正在寻找
  • 0x1801-> IDK(仅Writable特征SO IDC)
  • 0x180a->设备名称(我可以从此读取名称)
  • 0x180f->电池

我遇到的第一个问题是,根据文档,自API 13以来,eNCHACTERISTISREAD(3)被弃用,因为它现在添加了一个字节作为参数。但是根据Android Studio的说法,此超载不存在。

第二个是,尽管他打电话给他的10个以上可读特征,

但我现在仅测试了事情,但这是我现在所做的:

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        for (BluetoothGattService serv: gatt.getServices()) {
            System.out.println("-----------------" + serv.getUuid());
            
            for(BluetoothGattCharacteristic chara: serv.getCharacteristics()) {
                
                if((chara.getProperties() & PROPERTY_READ) != 0) {
                    System.out.println("Can read");
                    gatt.readCharacteristic(chara);
                } 
                else {
                    System.out.println("Can't read");
                }
            }
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,  int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        System.out.println("Called");

        if(status == GATT_SUCCESS) {
            System.out.println(new String(characteristic.getValue()));
        } else {
            System.out.println("error during read");
        }
    }

这就是输出:

    I/System.out: -----------------00001800-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/chatty: uid=10168(com.example.adici) Binder:27154_2 identical 1 line
    I/System.out: Can read
    I/System.out: -----------------00001801-0000-1000-8000-00805f9b34fb
    I/System.out: Can't read
    I/System.out: -----------------3ab10100-f831-4395-b29d-570977d5bf94
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: Can't read
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: -----------------0000180f-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: -----------------0000180a-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/System.out: Called
    I/System.out: DISTO 80951028

我也尝试将其添加到循环到等待最后一个回调发生在开始另一个回调之前,因为它被提及了

while(waitingForCallback)
{
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
waitingForCallback = true;
gatt.readCharacteristic(chara);

weatedforcallback在onCharacteristicRead中设置为false,但最终以无限循环为中,因为该方法没有称为

我真的不知道为什么过载是我的过载。 IDE AS SDKMIN = 27和TargetsDK = 31,

任何帮助都将受到赞赏!

I'm still trying hard to read from a BLE device (laser meter) from an android (using java)
Basically I'm trying to get which fields are those I'm interested into (aka the measures)

So far i discovered 5 services (i found the usage here):

  • 3ab10100-f831-4395-b29d-570977d5bf94 (I can't figure out what is this bad boy usage)
  • 0x1800 -> probably the ones I'm looking for
  • 0x1801 -> idk (only writable characteristics so idc)
  • 0x180a -> device name (I am able to read the name from this one)
  • 0x180f -> battery

the first problem i met is that, according to the documentation, onCharacteristicRead(3) is deprecated since API 13 as it adds now an array of byte as parameter. But according to Android studio, this overload does not exist.

The second one is that onCharacteristicRead is only called one despite calling him on more than 10 READABLE characteristics

I'm only testing things out for now but here's what I did:

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        for (BluetoothGattService serv: gatt.getServices()) {
            System.out.println("-----------------" + serv.getUuid());
            
            for(BluetoothGattCharacteristic chara: serv.getCharacteristics()) {
                
                if((chara.getProperties() & PROPERTY_READ) != 0) {
                    System.out.println("Can read");
                    gatt.readCharacteristic(chara);
                } 
                else {
                    System.out.println("Can't read");
                }
            }
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,  int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        System.out.println("Called");

        if(status == GATT_SUCCESS) {
            System.out.println(new String(characteristic.getValue()));
        } else {
            System.out.println("error during read");
        }
    }

And this is the output:

    I/System.out: -----------------00001800-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/chatty: uid=10168(com.example.adici) Binder:27154_2 identical 1 line
    I/System.out: Can read
    I/System.out: -----------------00001801-0000-1000-8000-00805f9b34fb
    I/System.out: Can't read
    I/System.out: -----------------3ab10100-f831-4395-b29d-570977d5bf94
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: Can't read
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: -----------------0000180f-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/System.out: Can read
    I/System.out: -----------------0000180a-0000-1000-8000-00805f9b34fb
    I/System.out: Can read
    I/System.out: Called
    I/System.out: DISTO 80951028

I've also tried adding this while loop to wait for the last callback to happen before starting another one as it was mentionned

while(waitingForCallback)
{
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
waitingForCallback = true;
gatt.readCharacteristic(chara);

Where waitingForCallback is set to false in onCharacteristicRead but it ends up in infinite loop as the method is not called

I really don't get why the overload is a problem for my IDE as Sdkmin = 27 and targetSdk = 31

Any help is appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2025-02-07 07:22:27

在操作已经待定时,您可能不会启动GATT操作(例如阅读特征)。您必须等待回调,然后才能启动下一个读取。

您的睡眠循环无法正常工作,因为蓝牙堆栈在另一个回调已经运行时从未调用您的回调。在这种情况下,如果您在onservices discovered中运行该睡眠环,则将安排OnChacteristicRead回调,但是直到onservices disciscovered返回之前,它才会运行。这是Android中“粘合剂”机制的一个功能,它可以防止同一对象同时运行的回调。

You may not initiate a gatt operation (for example reading a characteristic) while an operation is already pending. You must wait for the callback before you initiate the next read.

You sleep loop won't work since the Bluetooth stack never calls a callback of yours while another callback is already running. In this case if you run that sleep loop in onServicesDiscovered, the onCharacteristicRead callback will be scheduled to run, but it won't run until onServicesDiscovered has returned. This is a feature of the "Binder" mechanism in Android, which prevents callbacks for the same object to run concurrently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文