1

无法接收从服务器外围设备发送的任何通知。

我使用 ESP32 作为服务器,您可以在 Arduino 应用程序中找到“BLE_notify”代码(文件> 示例 ESP32 BLE Arduino > BLE_notify)。使用此代码,一旦客户端连接,ESP32 就会每秒开始通知新消息。使用的客户端是安装了 Noble 节点库的 Raspberry Pi ( https://github.com/abandonware/noble )。这是我正在使用的代码。

    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 和 CHARACTERISTIC_UUID 显然是 ESP32 中编码的 UUID。

这种代码工作,它可以找到服务和特性,它可以成功连接到外围设备,但它不能接收消息通知。我还尝试了一个作为客户端工作的 Android 应用程序,一旦连接到它,我就可以从该应用程序获取外围设备通知的所有消息。所以 noBLE 客户端缺少一些东西。

我认为on.read/on.data/notify(true)回调方法有问题。也许这些不是从服务器接收通知的方法?我也尝试了这些subscribe方法,但仍然无法正常工作。

官方文档不清楚。任何人都可以启动并运行它吗?请帮忙。

4

1 回答 1

0

on.read/on.data/是事件监听器。他们没有错。当有特定事件时调用它们。

例如添加characteristic.read([callback(error, data)]);会调用on.read.

来源:

在以下时间发出:

  • 特征读取已完成,character.read(...) 的结果
  • 在使用characteristic.notify(true[, callback(error)]) 启用后,外围设备已通过通知或指示更新了特征值
于 2021-06-14T15:29:24.237 回答