微信小程序> 微信小程序实现蓝牙BLEdemo版-微信小程序ble-微信小程序ibeacon

微信小程序实现蓝牙BLEdemo版-微信小程序ble-微信小程序ibeacon

浏览量:2021 时间: 来源:吴小友
微信小程序实现蓝牙BLE1.

步骤:1、wx.openBluetoothAdapter//蓝牙初始化2、wx.onBluetoothDeviceFound//监听寻找到新设备的事件3、wx.startBluetoothDevicesDiscovery//开始搜寻附近的蓝牙外围设备4、wx.getBluetoothDevices//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。5、wx.stopBluetoothDevicesDiscovery//停止搜寻附近的蓝牙外围设备。搜索到需要设备时或者连接时候停止搜索6、wx.createBLEConnection//连接设备7、wx.getBLEDeviceServices//获取所有服务列表8、wx.getBLEDeviceCharacteristics//获取蓝牙设备某个服务中的所有特征值9、wx.onBLEConnectionStateChange//监听设备连接状态(本人用小米2手环测试经常断)10、wx.notifyBLECharacteristicValueChange//启用低功耗蓝牙设备特征值变化是的notify功能11、wx.onBLECharacteristicValueChange//开始监听特征值的变化12、wx.writeBLECharacteristicValue//写入特征值13、wx.readBLECharacteristicValue//读取特征值

2.

以下是demo代码:

onLoad:function(options){varthatthis//蓝牙初始化wx.openBluetoothAdapter({success:function(res){console.log(res)//监听寻找到新设备的事件wx.onBluetoothDeviceFound(function(res){//console.log(res)})},fail:function(res){wx.showModal({title:'提示',content:'请检查手机蓝牙是否打开',})}})},3.

点击搜索设备按钮(lanya):

lanya:function(){wx.showLoading({title:'搜索中',})setTimeout(function(){wx.hideLoading()},1500)//开始搜寻附近的蓝牙外围设备wx.startBluetoothDevicesDiscovery({success:function(res){console.log('搜索完成')//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。wx.getBluetoothDevices({success:function(res){if(res.devices.length0){wx.showModal({title:'提示',content:'没有搜索到设备,请重试',})wx.navigateTo({url:'../index/index',})}console.log(res)//console.log(res.devices[0].deviceId)//console.log(res.devices[0].name)console.log(res.devices)vardatares.deviceswx.navigateTo({url:'../lanya/lanya?data'+JSON.stringify(data),})//停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。//wx.stopBluetoothDevicesDiscovery({//success:function(res){//console.log('已经停止搜索')//},//})},fail:function(res){console.log('没有找到设备')}})},fail:function(fa){console.log(fa)}})},onLoad:function(options){varId[]varName[]vardataJSON.parse(options.data)console.log(data)varthatthisconsole.log(data)that.setData({array:data})},4.

点击连接设备按钮:

sub:function(e){vardeviceIde.currentTarget.idapp.globalData.deviceIddeviceIdconsole.log(e.currentTarget.id)//连接设备wx.createBLEConnection({deviceId:deviceId,success:function(res){console.log(res)//获取服务列表wx.navigateTo({url:'../res/res?id'+deviceId,})console.log('id:'+deviceId)},fail:function(res){wx.showModal({title:'提示',content:'连接超时,请重试',})//console.log('id:'+deviceId)console.log(res)}})},onLoad:function(options){vardeviceIdoptions.idvarthatthis//获取所有服务wx.getBLEDeviceServices({deviceId:deviceId,success:function(res){//console.log(res)console.log(res.services)that.setData({array:res.services})},})},5.

进入某个服务

sub:function(e){varthatthisvarserviceIde.target.id//获取蓝牙设备某个服务中的所有特征值wx.getBLEDeviceCharacteristics({deviceId:app.globalData.deviceId,serviceId:serviceId,success:function(res){console.log(res)that.data.characteristicsres.characteristics//获取characteristicwx.navigateTo({url:'../notify/notify?ct'+JSON.stringify(that.data.characteristics)+'serviceId'+serviceId,})},fail:function(res){console.log(res)}})//监听设备连接状态wx.onBLEConnectionStateChange(function(res){console.log(res)if(res.connectedfalse){wx.showModal({title:'提示',content:'设备连接已断开',})}})},onLoad:function(options){varctJSON.parse(options.ct)serviceIdoptions.serviceIdvarthatthisthat.setData({array:ct,serviceId:serviceId,})},6.

进入某个特征值

sub:function(e){varthatthisvarchte.target.idvardeviceIdapp.globalData.deviceId//启用低功耗蓝牙设备特征值变化是的notify功能wx.notifyBLECharacteristicValueChange({deviceId:deviceId,serviceId:serviceId,characteristicId:cht,state:true,success:function(res){//开始监听wx.onBLECharacteristicValueChange(function(characteristics){console.log(characteristics.value)varacharacteristics.valuevarint8arraynewInt8Array(a);console.log("监听到特征值更新:"+int8array[0])//varhexab2str(a);//console.log("返回的值:".hex)})console.log(res)wx.navigateTo({url:'../write/write?cht'+cht+'serviceId'+serviceId+'deviceId'+deviceId,})},fail:function(res){console.log(res)},})},**```onLoad:function(options){varthatthisvarchtoptions.chtvardeviceIdoptions.deviceIdvarserviceIdoptions.serviceIdthat.setData({deviceId:deviceId,cht:cht,serviceId:serviceId})},7.

向该特征值写入数据:**

formSubmit:function(e){varthatthisvarvaluee.detail.value.pswconsole.log(value)//写入数据wx.writeBLECharacteristicValue({deviceId:that.data.deviceId,serviceId:that.data.serviceId,characteristicId:that.data.cht,value:that.str2ab(value),success:function(res){console.log(res)},fail:function(res){console.log(res)}});wx.readBLECharacteristicValue({deviceId:that.data.deviceId,serviceId:that.data.serviceId,characteristicId:that.data.cht,success:function(e){console.log(e)},})},8.

重点写入的字符串要转换成Arraybuffer

//字符串转为ArrayBuffer对象,参数为字符串str2ab:function(str){varbufnewArrayBuffer(str.length*2);//每个字符占用2个字节varbufViewnewUint16Array(buf);for(vari0,strLenstr.length;istrLen;i++){bufView[i]str.charCodeAt(i);}returnbuf;},9.

**监听的特征值变化回调是ArrayBuffer也要转换为字符串,可是我直接写下标就能获取,写转换获得就是undefined

functionab2str(arrayBuffer){returnString.fromCharCode.apply(null,newUint8Array(arrayBuffer));//letunit8ArrnewUint8Array(arrayBuffer);//letencodedStringString.fromCharCode.apply(null,unit8Arr),//decodedStringdecodeURIComponent(escape((encodedString)));//没有这一步中文会乱码//returndecodedString;}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎