1.终于实现了蓝牙的功能,也找到了合适的硬件,记录一下自己需要注意和总结的地方具体的搜索、连接、断开、消息传输都已经实现了,作为项目的一个页面完成了
相应的代码地址,具体的蓝牙代码在pages/bluetooth当中2.蓝牙部分代码地址
一.硬件部分HC-08蓝牙模块4.0BLE主从一体CC2540无线串口透传模块BLE串口通信硬件官网汇承官网基本连接教程注意:串口模块和usbtottl的连接方式二.Windows串口调试助手下载串口调试助手设置默认波特率为9600三.手机App测试LightBlue和Windows的调试注意:由图4可以看出串口通信的serviceId的UUID为FFE0(读写UUID)四.微信小程序初始化连接(重头戏来了)初始化蓝牙适配器wx.openBluetoothAdapter({success:function(res){console.log('初始化蓝牙适配器成功'+JSON.stringify(res))that.msg'初始化蓝牙适配器成功'wx.showModal({title:'蓝牙适配情况',content:'初始化蓝牙适配器成功'})},fail:function(){that.msg'初始化蓝牙适配器失败'wx.showModal({title:'蓝牙适配情况',content:'蓝牙适配失败,请检查手机蓝牙和定位功能是否打开'})},complete:function(){console.log('初始化蓝牙适配器完成')}})开启蓝牙搜索,并获取蓝牙设备列表wx.getBluetoothAdapterState--wx.onBluetoothAdapterStateChange--wx.startBluetoothDevicesDiscovery--wx.onBluetoothDeviceFound--getBluetoothDevices选择蓝牙设备获取相应的deviceId连接蓝牙设备wx.createBLEConnection--wx.getBLEDeviceService(获取设备的ServiceId)wx.createBLEConnection({deviceId:deviceId,success:function(res){console.log('蓝牙设备连接成功')wx.hideLoading()wx.getBLEDeviceServices({deviceId:deviceId,success:function(res){that.deviceServiceres.servicesfor(vart0;tthat.deviceService.length;t++){varservicethat.deviceService[t]varserviceIdservice.uuid.substring(4,8)if(serviceId'FFE0'){//‘FFE0’为设备定义的读写UUIDthat.serviceIdservice.uuid}}that.nowDevice!that.nowDevicethat.nowService!that.nowServiceconsole.log('获取蓝牙设备Service'+res.errMsg)},fail:function(res){wx.showModal({title:'设备Service信息',content:'蓝牙设备连接成功'+''+'设备信息获取错误'+res.errMsg})}})},fail:function(res){console.log('蓝牙设备连接失败,请稍后重试')wx.hideLoading()wx.showModal({title:'提示',content:'蓝牙设备连接失败,请稍后重试',duration:2000})},complete:function(){console.log('蓝牙设备连接完成')wx.hideLoading()}})连接设备同时关闭蓝牙搜索wx.stopBluetoothDevicesDiscovery向串口发送数据(重要)wx.getBLEDeviceCharacteristics--success:wx.notifyBLECharacteristicValueChang(启用notify功能)--success:wx.writeBLECharacteristicValue--success:wx.readBLECharacteristicValue(必须读取,否则Android设备无法正常向串口发送数据wx.getBLEDeviceCharacteristics({deviceId:that.deviceId,serviceId:that.serviceId,success:function(res){console.log(res.characteristics)that.deviceCharacteristicsres.characteristicsfor(vari0;ithat.deviceCharacteristics.length;i++){that.characteristicthat.deviceCharacteristics[i]that.characteristicPropertiesthat.characteristic.propertiesif(that.characteristicProperties.notifytrue){that.characteristicIdthat.characteristic.uuidwx.notifyBLECharacteristicValueChange({state:true,//启用notify功能deviceId:that.deviceId,serviceId:that.serviceId,characteristicId:that.characteristicId,success:function(res){console.log('开启notify成功'+that.characteristic.uuid)for(leti0;idataView.byteLength;i++){varwriteData'0x'+dataView.getUint8(i).toString(16)that.writeDatasthat.writeDatas+''+writeData}wx.writeBLECharacteristicValue({deviceId:that.deviceId,serviceId:that.serviceId,characteristicId:that.characteristicId,value:dataBuffer,success:function(res){console.log('发送的数据:'+that.writeDatas)console.log('message发送成功')wx.showModal({title:'数据发送成功',content:that.writeDatas})wx.readBLECharacteristicValue({deviceId:that.deviceId,serviceId:that.serviceId,characteristicId:that.characteristicId,success:function(res){console.log('读取数据成功')}})},fail:function(res){//failconsole.log('message发送失败'+that.characteristicIdw)wx.showToast({title:'数据发送失败,请稍后重试',icon:'none'})},complete:function(res){//failconsole.log('message发送完成')}})},fail:function(){console.log('开启notify失败'+that.characteristicId)}})//that.writeMessage(that.deviceId,that.serviceId,that.characteristicIdw,that.characteristicIdr,that.characteristicIdn)}}},fail:function(){console.log('获取characteristic失败')}})读取串口发送的数据(重要)wx.getBLEDeviceCharacteristics--success:wx.notifyBLECharacteristicValueChange--success:wx.readBLECharacteristicValue--wx.onBLECharacteristicValueChange五.微信小程序蓝牙未启动,离开小程序开启蓝牙后再启动小程序3.蓝牙页面启动流程
在onLoad中wx.openBluetoothAdapter在onShow中判断当前蓝牙适配器的状态wx.onBluetoothAdapterStateChange--若更改成功,则wx.openBluetoothAdapter六.微信小程序开发注意Android设备在readBLECharacteristicValu后可能会出现显示message传输成功但是串口调试助手收不到数据的情况解决方法:在writeBLECharacteristicValu后添加readBLECharacteristicValue串口读写设备的时候注意转换数据格式,传输的数据格式为ArrayBuffer,需要转换才可以显示,直接console返回的res.value为空用到的转换格式代码letdatathat.sendData.split(',')letdataBuffernewArrayBuffer(data.length)letdataViewnewDataView(dataBuffer)for(letj0;jdata.length;j++){dataView.setUint8(j,'0x'+data[j])}varwriteData'0x'+dataView.getUint8(i).toString(16)ab2hex:function(buffer){varhexArrArray.prototype.map.call(newUint8Array(buffer),function(bit){return('00'+bit.toString(16)).slice(-2)})returnhexArr.join('')}根据serviceId获取characteristicId时需要采用for循环获取,不使用for循环会无法开启notify,使用for循环会开启两遍,两次开启UUID相同,但是第一次显示开启成功,第二次显示开启失败实际读取串口发送数据的位置为wx.onBLECharacteristicValueChang,wx.readBLECharacteristicValue返回的res中只有接受信息是否成功的信息问题:若手机蓝牙未启动,初始化页面无法成功开启蓝牙适配器,重新打开蓝牙后,页面无法重新开启蓝牙适配器解决:因为只在onLoad中定义了开始蓝牙适配器,没有在onShow中定义,通过《六》中来解决注意:必须在onShow中先进行判断,再选择是否调用wx.openBluetoothAdapter,否则在第一次启动页面的时候调用两次wx.openBluetoothAdapter,wx.showModal会显示两次(初始化页面的时候onLoad后会调用onShow)
微信小程序-BLE蓝牙实现demo-微信小程序ble-微信小程序ibeacon
浏览量:1899
时间:
来源:_Root___
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

最新资讯
-
抖音再现本地生活服务,咫尺同城圈商业变现新通道
短视频成为本地生活探店网红营销变现引流的新阵地,每一位网红都渴望在短视频内“一夜爆红”。即速应用团队对多商家小程序进行升级,打造了咫尺同城圈:“同城探店营销助手”,不仅完善商家营销技巧,还助力探店网红玩转本地生活服务。 -
抖音再现本地生活服务,咫尺同城圈商业变现新通道
短视频成为本地生活探店网红营销变现引流的新阵地,每一位网红都渴望在短视频内“一夜爆红”。即速应用团队对多商家小程序进行升级,打造了咫尺同城圈:“同城探店营销助手”,不仅完善商家营销技巧,还助力探店网红玩转本地生活服务。 -
阿坝小程序代理
阿坝藏族羌族小程序代理公司有哪些?阿坝藏族羌族小程序代理平台哪个好?阿坝藏族羌族小程序代理商怎么收费,代理政策如何?下面就让即速应用产品经理jisuapp.cn来告诉你吧!