安卓蓝牙
发布于 6 年前 作者 qiang10 8365 次浏览 来自 问答

今天发现小程序可以支持安卓蓝牙API了,但测试了下wx.getBluetoothDevices这个API报错!

另外向非低功耗蓝牙设备发送数据的场景如何实现?比如我要使用蓝牙便携打印机(非低功耗蓝牙)

10 回复

Page({

  onLoad: function () {

    //var that = this;

    wx.openBluetoothAdapter({

      success: function (res) {

        console.log(res)

        wx.getBluetoothAdapterState({

          success: function (res) {

            console.log(res)

          }

        })

        wx.startBluetoothDevicesDiscovery({

          success: function (res) {

            console.log(res)

          }

        })

        wx.getConnectedBluetoothDevices({

          success: function (res) {

            console.log(res)

          }

        })

        wx.getBluetoothDevices({

          //services: [‘0019E0A29C02’],

          success: function (res) {

            console.log(res)

            // that.setData({

            //   // BLT_name: res.devices[0].name,

            //   // BLT_deviceId: res.devices[0].deviceId,

            //   // BLT_RSSI: res.devices[0].RSSI

            //   // BLT_advertisData: res.devices[0].advertisData

            // })

          }

        })

      }

    })

  }

})  

控制台输出,就wx.getBluetoothDevices报错,其他四个API正常。

错误代码:cannot read property ‘map’ of undefined

这个报错是由于发现的蓝牙设备为0的时候导致的。

如果你发现至少一个设备后,就不会报错了

同遇到thirdScriptError       wx.getBluetoothDevices,报错

怎么解决?

四楼:您说蓝牙设备为0时wx.getBluetoothDevices才报错,感觉不对,手机发现的蓝牙设备有好几个(连接和不连接的),况且wx.getConnectedBluetoothDevices可以取得蓝牙设备一个,这如何解释?还是觉得这个API没做好

请官方人员来解答下么?

四楼,为啥发现的蓝牙设备为0,我测试的时候,旁边有好几个蓝牙,接口有问题吧

刚才尝试了安卓和苹果的手机返回来的数据不一样,这样子输出出来看看就知道了,但是这个

wx.onBLECharacteristicValueChange(CALLBACK)

这个接口没有实时的数据返回来

有官方人员来解答下么?

7楼没错,而且,可以用另了一个方式去获取,就是通过

wx.onBluetoothDeviceFound(CALLBACK) 来获取,但实际上,手机可以发现蓝牙,但不会进入到这个事件函数中去。

所以小程序app无法发现蓝牙设备。

回到顶部