小程序蓝牙与设备通讯

小程序联调蓝牙与设备通讯

粗略的记录一下

  1. 初始化
wx.openBluetoothAdapter({
  success: (res) => {
   },
   fail: (res) => {
   }
})

2、搜索附近蓝牙设备

wx.startBluetoothDevicesDiscovery({
     allowDuplicatesKey: false,//是否允许重复上报同一设备。false 不允许, true 允许(设备多的情况下手机容易卡顿)
     success: (res) => {
     		
			function ab2hex(buffer) {// ArrayBuffer转16进度字符串示例
			  var hexArr = Array.prototype.map.call(
			    new Uint8Array(buffer),
			    function(bit) {
			      return ('00' + bit.toString(16)).slice(-2)
			    }
			  )
			  return hexArr.join('');
			}
			//搜索到新设备回调
			wx.onBluetoothDeviceFound((res) => {
			  var devices = res.devices;
			})

      },
      fail:(res)=>{
      }
})
  1. 停止搜索设备(找到设备后记得及时停止搜索设备)
wx.stopBluetoothDevicesDiscovery({
        success: (res) => {
        },
        fail:(res)=>{
          
        }
})
  1. 连接设备
  • 4.1.1 连接蓝牙低功耗设备
wx.createBLEConnection({
	deviceId,//设备deviceId
	success: (res) => {
		//支持低功耗就可以获取设备的serviceId
	},
	fail: (res) => {
		reject(res)
	}
})
  • 4.1.2 获取蓝牙低功耗设备所有服务 (service)
wx.getBLEDeviceServices({
	deviceId,
	success: (res) => {
		//得到serviceId
	},
	fail: (res) => {
		
	}
})
  • 4.1.3 获取蓝牙低功耗设备某个服务中所有特征 (characteristic)
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
	for (let i = 0; i < res.characteristics.length; i++) {
		let item = res.characteristics[i]
		if (item.properties.read) {//是否支持读
		//读取蓝牙低功耗设备特征值的二进制数据。注意:必须设备的特征支持 read 才可以成功调用
			wx.readBLECharacteristicValue({
				deviceId,
				serviceId,
				characteristicId:item.uuid,
				success(res) {
					
				}
			})
		}
		if (item.properties.notify || item.properties.indicate) {
			/*启用蓝牙低功耗设备特征值变化时的 notify 功能 */
			wx.notifyBLECharacteristicValueChange({
			deviceId,
			serviceId,
			characteristicId:item.uuid,
			state: true,
			complete(res) {
			}
		})
		}
	}
},
fail(res) {
}
})
  • 4.1.4 连接设备
wx.getConnectedBluetoothDevices({
        services: [serviceId],//需要连接设备的serviceId(数组)
        success (res) {
        },
        fail(error){
        }
 })
  1. 写入数据
//监听设备回复值
wx.onBLECharacteristicValueChange((characteristic) => {
      //得到设备回复
})
let arr = [0x44,0x44,0x44]
wx.writeBLECharacteristicValue({
          deviceId: this.deviceId,//已连接设备的deviceId
          serviceId:serviceId,//已连接设备的serviceId(服务
          characteristicId:charaId,//已连接设备的charaId(特征
          value: new Uint8Array(arr).buffer,
          success(res){
          },
          fail(res){
            
          }
 })
  1. 断开与蓝牙低功耗设备的连接
wx.closeBLEConnection({
        deviceId: this.deviceId,
        success: (res) => {
        },
        fail: (res) => {
        }
})
  1. 关闭蓝牙模块
wx.closeBluetoothAdapter({
            success: (res) => {
            },
            fail: (res) => {
            }
   })
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值