简体   繁体   English

无法使用 Noble 获取通知数据

[英]Unable to get Notify data using Noble

Can't receive any notifications sent from the Server peripheral.无法接收从服务器外围设备发送的任何通知。

I am using ESP32 as Server with the "BLE_notify" code that you can find in the Arduino app (File> Examples ESP32 BLE Arduino > BLE_notify).我正在使用带有“BLE_notify”代码的 ESP32 作为服务器,您可以在 Arduino 应用程序中找到该代码(文件> 示例 ESP32 BLE Arduino > BLE_notify)。 With this code the ESP32 starts notifying new messages every second once a Client connects.使用此代码,一旦客户端连接,ESP32 就会每秒开始通知新消息。 The client used is a Raspberry Pi with Noble node library installed on it ( https://github.com/abandonware/noble ).使用的客户端是安装了 Noble 节点库的 Raspberry Pi( https://github.com/abandonware/noble )。 this is the code I am using.这是我正在使用的代码。

    noble.on('discover', async (peripheral) => {
      console.log('found peripheral:', peripheral.advertisement);
      await noble.stopScanningAsync();
      await peripheral.connectAsync();
      console.log("Connected")

      try {
        const services = await peripheral.discoverServicesAsync([SERVICE_UUID]);
        const characteristics = await services[0].discoverCharacteristicsAsync([CHARACTERISTIC_UUID])
        const ch = characteristics[0]
        
        ch.on('read', function(data, isNotification) {
          console.log(isNotification)
          console.log('Temperature Value: ', data.readUInt8(0));
        })
        
        ch.on('data', function(data, isNotification) {
          console.log(isNotification)
          console.log('Temperature Value: ', data.readUInt8(0));
        })
        ch.notify(true, function(error) {
          console.log(error)
          console.log('temperature notification on');
        })
      
      } catch (e) {
        // handle error
        console.log("ERROR: ",e)
      }
    });

SERVICE_UUID and CHARACTERISTIC_UUID are obviously the UUIDs coded in the ESP32. SERVICE_UUID 和 CHARACTERISTIC_UUID 显然是 ESP32 中编码的 UUID。

This code sort of works, it can find Services and Characteristics and it can successfully connect to the peripheral, but it cannot receive messages notifications.这种代码工作,它可以找到服务和特性,它可以成功连接到外围设备,但它不能接收消息通知。 I also tried an Android app that works as client, from that app I can get all the messages notified by the peripheral once connected to it.我还尝试了一个作为客户端工作的 Android 应用程序,从该应用程序中,一旦连接到外围设备,我就可以获得外围设备通知的所有消息。 So there is something missing in the noBLE client side.所以 noBLE 客户端缺少一些东西。

I think there is something wrong in the on.read/on.data/notify(true) callback methods.我认为on.read/on.data/notify(true)回调方法有问题。 Maybe these are not the methods to receive notifications from Server?也许这些不是从服务器接收通知的方法? I also tried the subscribe methods but still not working.我也尝试了subscribe方法,但仍然无法正常工作。

The official documentation is not clear.官方文档不清楚。 Anyone could get it up and running?任何人都可以启动并运行它吗? Please help.请帮忙。

on.read/on.data/ are event listeners. on.read/on.data/是事件监听器。 There is nothing wrong with them.他们没有错。 They are invoked when there is a certain event.当有特定事件时调用它们。

For example adding characteristic.read([callback(error, data)]);例如添加characteristic.read([callback(error, data)]); would have invoked the on.read .会调用on.read

From the source:来源:

Emitted when:在以下时间发出:

  • Characteristic read has completed, result of characteristic.read(...)特征读取已完成,character.read(...) 的结果
  • Characteristic value has been updated by peripheral via notification or indication, after having been enabled with characteristic.notify(true[, callback(error)])在使用characteristic.notify(true[, callback(error)]) 启用后,外围设备已通过通知或指示更新了特征值

I resolve using the following two envs NOBLE_MULTI_ROLE=1 and NOBLE_REPORT_ALL_HCI_EVENTS=1 (see the documentation https://github.com/abandonware/noble )我使用以下两个环境 NOBLE_MULTI_ROLE=1 和 NOBLE_REPORT_ALL_HCI_EVENTS=1 解决(请参阅文档https://github.com/abandonware/noble

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

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