微信小程序如何使用蓝牙控制

小程序中蓝牙的步骤如上:

初始化蓝牙wx.openBluetoothAdapter(OBJECT)

开始搜索蓝牙 wx.startBluetoothDevicesDiscovery(OBJECT)

所有已发现的蓝牙设备wx.getBluetoothDevices(OBJECT)

监听寻找到新设备的事件wx.onBluetoothDeviceFound(CALLBACK)

连接低功耗蓝牙设备wx.createBLEConnection(OBJECT)

获取蓝牙设备所有 service(服务) wx.getBLEDeviceServices(OBJECT)

获取蓝牙设备某个服务中的所有 characteristic(特征值)wx.getBLEDeviceCharacteristics(OBJECT)

启用低功耗蓝牙设备特征值变化时的 notify 功能wx.notifyBLECharacteristicValueChange(OBJECT)

写入wx.writeBLECharacteristicValue(OBJECT)

注意:

其中ios初始化后应需要过几秒才能成功搜索到蓝牙设备,所以可以设置setTimeout()

wx.getBluetoothDevices()所获得的参数ios和安卓有所不同。Android 上获取到的deviceId为设备 MAC 地址iOS 上获取到的deviceId则为设备 uuid,因此deviceId不能硬编码到代码中。

 

具体函数参数和返回的值参考官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.stopBluetoothDevicesDiscovery.html

相关文章:

  • 2022-01-11
  • 2021-05-03
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-05
  • 2021-11-29
猜你喜欢
  • 2021-09-14
  • 2021-12-02
  • 2021-10-01
  • 2022-03-01
  • 2021-05-31
相关资源
相似解决方案