收藏
回答

wx.onBluetoothDeviceFound查询不到 低功耗 设备?

我们这边是让同事开发了一个可以用蓝牙连接的硬件设备,

硬件蓝牙设备的版本是:

蓝牙 V4.2 BR/EDR,

低功耗蓝牙

然后我这边使用的是官方文档蓝牙扫描相关的API,但是没有扫描到设备,所以问一下怎么回事

async initBT() {

      let allList = [];

      this.btLoading = true

      //开启蓝牙模块

      console.log('openBlue');

      await wx.openBluetoothAdapter({

        fail:(err=>{

          console.log('openBlue',err);

          wx.showToast({

            icon:'error',

            title:'请开启蓝牙',

          })

        })

      })

      console.log('bluetoothAdapterState',bluetoothAdapterState);


      console.log('startScan');

      await wx.startBluetoothDevicesDiscovery({

        fail:(err=>{

          console.log('startScan',err);

          wx.showToast({

            icon:'error',

            title:'扫描蓝牙失败',

          })

        })

      })

      

      //获取蓝牙适配器状态

      let bluetoothAdapterState = await wx.getBluetoothAdapterState()

      console.log('bluetoothAdapterState',bluetoothAdapterState);

      // 不在搜索中

      if (bluetoothAdapterState['discovering']) {

        console.log('scanning');

        wx.onBluetoothDeviceFound(res => {

          allList = [res.devices,...allList];

          allList.forEach(resItem=>{

            resItem.forEach(item=>{

              if(item.connectable){

                const device = item;

                const name = device.name || ''

                const localName = device.localName || ''

                const deviceId = device.deviceId

                const mac = wx.ab2hex(device.advertisData)

                if (this.deviceList.findIndex(e => e.deviceId === deviceId) === -1) {

                  this.deviceList.push({

                    mac: mac,

                    name: name,

                    localName: localName,

                    deviceId: deviceId,

                    advertisData: this.ab2hex(device.advertisData).substring(0, 16)

                  })  

                }

              }

            })

          })

        })

      }


      setTimeout(async ()=>{

        console.log('deviceList',this.deviceList);

        wx.getBluetoothDevices({

          success:(res)=>{

            console.log('allRes',res)

            res.devices.forEach(item=>{

              if(item.connectable){

                console.log('item',item)

              }

            })

          }

        });

        await wx.stopBluetoothDevicesDiscovery()

        this.btLoading = false

      },5000)


    },


最后一次编辑于  2023-06-05
回答关注问题邀请回答
收藏

2 个回答

  • 社区技术运营专员--许涛
    社区技术运营专员--许涛
    2023-06-06

    你好,确认下设备是不是BLE设备

    2023-06-06
    有用
    回复 4
    • Krise丶雪缘
      Krise丶雪缘
      2023-06-06
      我这边同事说是包含传统蓝牙和BLE低功耗蓝牙,版本是4.2的
      2023-06-06
      回复
    • 社区技术运营专员--许涛
      社区技术运营专员--许涛
      2023-06-07回复Krise丶雪缘
      确认下是BLE设备,但是因为已经连接上了,所以设备不再广播呢?
      2023-06-07
      回复
    • Krise丶雪缘
      Krise丶雪缘
      2023-06-08
      确认是BLE设备,但是扫描API给的列表里面没有目标蓝牙设备,另外我们这边已经让同事换了个蓝牙,新蓝牙可以。
      2023-06-08
      回复
    • 社区技术运营专员--许涛
      社区技术运营专员--许涛
      2023-06-09回复Krise丶雪缘
      复现问题时麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,具体时间点
      2023-06-09
      回复
  • LSGL
    LSGL
    2023-06-06

    我也搜索不到一个蓝牙4.0的一个条码打印机。

    2023-06-06
    有用
    回复 1
    • Krise丶雪缘
      Krise丶雪缘
      2023-06-06
      我这边查到的说 微信小程序只能搜到BLE低功耗的蓝牙设备,蓝牙版本还得是4.2以上的,否则搜不到
      2023-06-06
      回复
登录 后发表内容