小程序蓝牙控制设备

小程序低功耗蓝牙控制设备

小程序控制蓝牙的过程&api
  1. 初始化蓝牙适配器wx.openBluetoothAdapter()
  2. 获取本机蓝牙链接状态wx.getBluetoothAdapterState()
  3. 开启搜索蓝牙wx.startBluetoothDevicesDiscovery()
  4. 获取所有蓝牙列表wx.getBluetoothDevices()
  5. 根据蓝牙列表匹配自己需要的广播蓝牙(厂商会给自己的设备规则),获取广播数据
  6. 把上一步匹配的到的蓝牙设备,进行连接wx.createBLEConnection()
  7. 连接成功,停止搜索wx.stopBluetoothDevicesDiscovery()
  8. 连接成功后,需要获取设备的uuid,设备特征值,发送指令需要用到wx.getBLEDeviceServices()
  9. 获取设备的characteristicId,发送指令需要用到wx.getBLEDeviceCharacteristics
  10. 发送密钥(密钥是从厂商获取到的)指令给门锁 wx.writeBLECharacteristicValue()
1. 初始化蓝牙适配器
initBluetoothAdapert function()(
	const that = this
	console.log("初始化蓝牙适配器");
	wx.openBluetoothAdapter({
   
	  success: function (res) {
   
		console.log("1.1获取自身蓝牙状态");
	    that.getBluetoothAdapterState();
	  },
	  fail: function (err) {
   
	    console.log(err);
	    wx.showToast({
   
	      title: '请检查您的蓝牙是否开启',
	      icon: 'success',
	      duration: 2000
	    })
	    setTimeout(function () {
   
	      wx.hideToast()
	    }, 2000)
	  }
	});
)

2.获取蓝牙适配器状态
getBluetoothAdapterState() {
   
    console.log('2.获取本机蓝牙连接状态')
    const that = this
    wx.getBluetoothAdapterState({
   
      success: function (res) {
   
        console.log("2.1获取本地蓝牙状态 available=true表示蓝牙已开启")
        console.log(res)
        if (res.available) {
   
          // 去搜索蓝牙
          that.startBluetoothDevicesDiscovery()
        } else {
   
          wx.showToast({
   
            title: '请打开您的蓝牙后重新尝试',
            icon: 'success',
            duration: 2000
          })
        }
      },
      fail: function(res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值