新闻详细
新闻当前位置:新闻详细

小程序蓝牙接收的数据是一位的嘛,小程序连接低功耗蓝牙硬件的方案记录

专业小程序设计开发——助力新电商新零售

电话+V: 152079-09430 ,欢迎咨询小程序蓝牙接收的数据是一位的嘛,[小程序设计与开发],[小程序投流与推广],[小程序后台搭建],[小程序整套源码打包],[为个体及小微企业助力],[电商新零售模式],[小程序运营推广及维护]

小程序蓝牙接收的数据是一位的嘛

一、蓝牙数据接收基本原理

我们需要了解蓝牙数据接收的基本原理。蓝牙是一种无线通信技术,它通过短距离的无线电波进行数据传输。当我们使用小程序接收蓝牙数据时,实际上是通过蓝牙模块将数据解析成可以识别的信号,然后再将这些信号转换成我们可以理解的信息。

二、小程序蓝牙接收的数据格式

小程序蓝牙接收的数据是一位的吗?答案是否定的。在蓝牙通信中,数据的传输通常以字节为单位,而不是一位。一个字节由8位(bit)组成,这是计算机最基本的信息单位。因此,当我们在小程序中接收到蓝牙数据时,实际上是接收到了一串由多个字节组成的数据流。

三、如何解析蓝牙数据

既然我们知道了小程序蓝牙接收的数据是以字节为单位的,那么如何解析这些数据呢?这需要我们对数据进行解码。在蓝牙通信中,常用的编码方式有ASCII码、UTF-8码等。我们需要根据具体的编码方式,将接收到的字节数据转换为相应的字符或数字,从而实现对数据的解析和处理。

四、注意事项

在使用小程序接收蓝牙数据时,还需要注意以下几点:

1.确保蓝牙设备与小程序正确配对,以便顺利接收数据。

2.根据蓝牙设备的说明书,了解其支持的数据格式和编码方式,以便正确解析数据。

3.注意检查数据是否完整,避免因数据传输过程中的丢失或错误导致解析失败。

小程序连接低功耗蓝牙硬件的方案记录

原创2023-04-1210:28·程序员周小帅前段时间搞了几天小程序连接硬件蓝牙实现读取数据写入数据的功能。

之前没搞过还是遇到了一些问题。这次记录一下方案。

看下效果



根据小程序官方说明大概就是获取蓝牙设备列表连接指定蓝牙设备获取蓝牙设备的服务连接指定蓝牙设备的指定服务根据服务获取该服务的特征拿到可读特征,可写特征,可通知特征根据设备ID服务ID服务通知特征创建一个数据监听用来接收蓝牙响应数据根据设备ID服务ID可写特征uuid来写入数据不说这么多自己封装了一个函数js文件贴在这里有需要的老铁可以自己领取

//低功耗蓝牙链接模块函数封装//寻找数组索引exportconstinArray=(arr,key,val)=>{for(leti=0;i<arr.length;i++){if(arr[i][key]===val){returni;}}return-1;}//ArrayBuffer转16进度字符串示例exportconstab2hex=(buffer)=>{varhexArr=Array.prototype.map.call(newUint8Array(buffer),function(bit){return('00'+bit.toString(16)).slice(-2)})returnhexArr.join('');}exportconstreadFloat16=(bytes,offset,littleEndian)=>{letsign=(bytes[offset+1]0x80)?-1:1;letexponent=((bytes[offset+1]0x7f)<<1)|(bytes[offset]>>7);letfraction=((bytes[offset]0x7f)<<8)|bytes[offset+1];if(exponent===0fraction===0){return0;}elseif(exponent===0){returnsign*Math.pow(2,-14)*(fraction/Math.pow(2,10));}elseif(exponent===0x1f){returnfraction!==0?NaN:sign*Infinity;}returnsign*Math.pow(2,exponent-15)*(1+fraction/Math.pow(2,10));}//A链接蓝牙关闭蓝牙扫描exportconststopBluetoothDevicesDiscovery=()=>{wx.stopBluetoothDevicesDiscovery()//关闭蓝牙扫描}//01获取蓝牙列表新设备会连续查找exportconstgetBluetList=(callout=null)=>{//初始化蓝牙模块wx.openBluetoothAdapter({success:function(res){//开始搜索设备wx.startBluetoothDevicesDiscovery({success:function(res){//找到设备后停止搜索wx.onBluetoothDeviceFound(function(devices){calloutcallout(devices.devices)})},fail:function(err){console.log('01搜索设备失败:',err)}})},fail:function(err){console.log('01初始化蓝牙失败:',err)}})}//02链接蓝牙exportconstcreateBLEConnection=(deviceId,callout=null)=>{wx.createBLEConnection({deviceId,success:(res)=>{calloutcallout(deviceId)},fail(e){console.log('02链接蓝牙失败:',e)}})this.stopBluetoothDevicesDiscovery()}//03获取指定蓝牙服务exportconstgetBLEDeviceServices=(deviceId,callout=null)=>{wx.getBLEDeviceServices({deviceId:deviceId,success:function(res){console.log('03获取蓝牙服务成功:',res.services)calloutcallout(res.services)},fail(e){console.log('03获取蓝牙服务失败:',e)}})}//04获取低功耗蓝牙某个指定服务得特征exportconstgetBLEDeviceCharacteristics=(deviceId,serviceId,callout=null)=>{wx.getBLEDeviceCharacteristics({deviceId,serviceId,success:(res)=>{console.log('04获取蓝牙某个服务特征成功',res.characteristics)constquery={deviceId:deviceId,serviceId:serviceId,characteristicId:'',writeCharacteristicId:'',readCharacteristicId:'',}res.characteristics.forEach(item=>{//获取可读特征读取蓝牙低功耗设备特征值的二进制数据注意:必须设备的特征支持read才可以成功调用if(item.properties.read){wx.readBLECharacteristicValue({deviceId,serviceId,characteristicId:item.uuid,})query.characteristicId=item.uuid}elseif(item.properties.write){//蓝牙可写特征query.writeCharacteristicId=item.uuid}elseif(item.properties.notify){//蓝牙可通知特征query.readCharacteristicId=item.uuid}});calloutcallout(query)},fail(res){console.error('04获取低功耗蓝牙某个指定服务得特征error:',res)}})}//05启用蓝牙低功耗设备特征值变化时的notify功能,订阅特征。注意:必须设备的特征支持notify或者indicate才可以成功调用。exportconstnotifyBLECharacteristicValueChange=(deviceId,services_UUID,characteristic_UUID,callout=null)=>{wx.notifyBLECharacteristicValueChange({deviceId:deviceId,serviceId:services_UUID,characteristicId:characteristic_UUID,state:true,type:'notification',success(res){console.log('启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值:成功---',characteristic_UUID);wx.onBLECharacteristicValueChange((res)=>{calloutcallout(res.value)})},fail(res){console.log('05监听失败:',res)},});}//06蓝牙写入函数exportconstwriteBufferToBLE=(deviceId,serviceId,writeCharacteristicId,sendBuffer,callout=null)=>{wx.writeBLECharacteristicValue({deviceId:deviceId,serviceId:serviceId,//在这里替换成你的设备服务IDcharacteristicId:writeCharacteristicId,//在这里替换成你的设备特征IDvalue:sendBuffer,success:function(res){console.log('写入writeBLECharacteristicValuesuccess',res)calloutcallout(res)}})}//B通用连续调用方式整合方法---------------------------------------------constbluetConfig={list:[],deviceId:'',services:[],serviceId:null,//指定服务的UUIDcharacteristicId:'',writeCharacteristicId:'',readCharacteristicId:'',}exportconstbluetFuns=()=>{//获取蓝牙列表getBluetList((res)=>{bluetConfig.list=[...bluetConfig.list,...res]})bluetConfig.deviceId=bluetConfig.list[0]['id']//连接蓝牙-并且监听数据-并模拟写入数据createBLEConnection(bluetConfig.deviceId,(res)=>{//获取蓝牙所有的服务getBLEDeviceServices(res,(res)=>{bluetConfig.services=res//获取指定服务的特征xxxx代表自己想要的指定的服务的uuid或其中的某一段唯一标识res.forEach(box=>{if(box.uuid.indexOf('xxxx')!=-1){bluetConfig.serviceId=box.uuid//获取这个服务的所有特征getBLEDeviceCharacteristics(bluetConfig.deviceId,bluetConfig.serviceId,(res)=>{bluetConfig.characteristicId=res.characteristicIdbluetConfig.writeCharacteristicId=res.writeCharacteristicIdbluetConfig.readCharacteristicId=res.readCharacteristicId//服务特征变化监听notifyBLECharacteristicValueChange(bluetConfig.deviceId,bluetConfig.serviceId,bluetConfig.readCharacteristicId,(res)=>{//注意这里是实时响应的在这里处理响应数据把console.log('监听数据:',res)})//这里模拟一个写入方法letsendBuffer=newArrayBuffer(2)letdataView=newDataView(sendBuffer)dataView.setUint8(0,0x00)writeBufferToBLE(bluetConfig.deviceId,bluetConfig.serviceId,bluetConfig.writeCharacteristicId,sendBuffer,(res)=>{console.log('蓝牙写入回调:',res)})})}})})})}

【GSFAI BANK FINANCING】尊享直接对接老板

电话+V: 152079-09430

专注于小程序推广配套流程服务方案。为企业及个人客户提供了高性价比的运营方案,解决小微企业和个体拓展客户的问题

小程序蓝牙接收的数据是一位的嘛
Copyright2025未知推广科技