微信公开课
微信公开课小程序热门讨论关于「蓝牙调试小程序不能全部检测周围的蓝牙」的小程序开发问题
补充:需求的场景描述(希望解决的问题)小米8手机上运行小程序扫描周围的蓝牙设备同一时刻下荣耀九扫描周围的设备为什么小米8手机蓝牙调试工具不能完全检测周围的蓝牙状态希望提供的能力微信者回复
展开:需求的场景描述(希望解决的问题)小米8手机上运行小程序扫描周围的蓝牙设备同一时刻下荣耀九扫描周围的设备为什么小米8手机蓝牙调试工具不能完全检测周围的蓝牙状态希望提供的能力即速小程序:pagesindexindexjsvarapp=getApp();varutils=require(utilsutiljs);functioninArray(arr,key,val){for(leti=0;i<arrlength;i++){if(arr[i][key]===val){returni;}}return1;}Page({** *页面的初始数据 *data:{textLog:,isopen:false,蓝牙适配器是否已打开devices:[],connected:false,chs:[]},** *生命周期函数监听页面加载 *onLoad:function(options){varthat=this;consolelog(用户信息,appglobalDatauserInfo);varlog=获取微信版本号:+appgetWxVersion()+
+获取客户端系统:+appgetPlatform()+
+系统版本:+appgetSystem()+
;thatsetData({textLog:log});获取当前设备平台以及微信版本if(appgetPlatform()==android&&utilsversionCompare(657,appgetWxVersion())){wxshowModal({title:提示,content:当前微信版本过低,请更新至最新版本,showCancel:false,success:function(res){if(resconfirm){thatbackPage();}}})}elseif(appgetPlatform()==android&&utilsversionCompare(430,appgetSystem())){wxshowModal({title:提示,content:当前系统版本过低,请更新至Android43以上版本,showCancel:false,success:function(res){if(resconfirm){thatbackPage();}}})}elseif(appgetPlatform()==ios&&utilsversionCompare(656,appgetWxVersion())){wxshowModal({title:提示,content:当前微信版本过低,请更新至最新版本,showCancel:false,success:function(res){if(resconfirm){thatbackPage();}}})}},** *生命周期函数监听页面卸载 *onUnload:function(){consolelog(生命周期函数监听页面卸载);thiscloseBluetoothAdapter();关闭蓝牙模块,使其进入未初始化状态},退出页面backPage:function(){wxnavigateBack({ delta:1})},清空log日志startClear:function(){varthat=this;thatsetData({textLog:});},** *流程: *1先初始化蓝牙适配器, *2获取本机蓝牙适配器的状态, *3开始搜索,当停止搜索以后在开始搜索,就会触发蓝牙是配置状态变化的事件, *4搜索完成以后获取所有已经发现的蓝牙设备,就可以将devices中的设备Array取出来, *5然后就可以得到所有已经连接的设备了 *startScan:function(){varthat=this;that_discoveryStarted=false;if(thatdataisopen){如果已初始化小程序蓝牙模块,则直接执行扫描thatgetBluetoothAdapterState();}else{thatopenBluetoothAdapter();}},初始化小程序蓝牙模块openBluetoothAdapter:function(){varthat=this;wxopenBluetoothAdapter({success:function(res){varlog=thatdatatextLog+打开蓝牙适配器成功
;thatsetData({textLog:log,isopen:true});thatgetBluetoothAdapterState();},fail:function(err){isopen:true;appshowModal1(蓝牙开关未开启);varlog=thatdatatextLog+蓝牙开关未开启
;thatsetData({textLog:log});}})监听蓝牙适配器状态变化事件wxonBluetoothAdapterStateChange(function(res){consolelog(onBluetoothAdapterStateChange,res)varisDvailable=resavailable;蓝牙适配器是否可用if(isDvailable){thatgetBluetoothAdapterState();}else{thatstopBluetoothDevicesDiscovery();停止搜索thatsetData({devices:[]});appshowModal1(蓝牙开关未开启);}})},关闭蓝牙模块,使其进入未初始化状态closeBluetoothAdapter:function(){wxcloseBluetoothAdapter()this_discoveryStarted=false},获取本机蓝牙适配器状态getBluetoothAdapterState:function(){varthat=this;wxgetBluetoothAdapterState({success:function(res){varisDiscov=resdiscovering;是否正在搜索设备varisDvailable=resavailable;蓝牙适配器是否可用if(isDvailable){varlog=thatdatatextLog+本机蓝牙适配器状态:可用
;thatsetData({textLog:log});if(!isDiscov){thatstartBluetoothDevicesDiscovery();}else{varlog=thatdatatextLog+已在搜索设备
;thatsetData({textLog:log});}}}})},开始扫描附近的蓝牙外围设备注意,该操作比较耗费系统资源,请在搜索并连接到设备后调用stop方法停止搜索startBluetoothDevicesDiscovery:function(){varthat=this;if(that_discoveryStarted){return}that_discoveryStarted=true;appshowLoading(正在扫描);varlog=thatdatatextLog+正在扫描
;thatsetData({textLog:log});setTimeout(function(){wxhideLoading();隐藏loading},3000);wxstartBluetoothDevicesDiscovery({services:[],allowDuplicatesKey:true,是否允许重复上报同一设备,如果允许重复上报,则onDeviceFound方法会多次上报同一设备,但是RSSI(信号)值会有不同interval:0,上报设备的间隔0表示找到新设备立即上报,其他数值根据传入的间隔上报20190225success:function(res){varlog=thatdatatextLog+扫描附近的蓝牙外围设备成功,准备监听寻找新设备:+res+
;thatsetData({textLog:log});thatonBluetoothDeviceFound();监听寻找到新设备的事件}});},* 一直寻找20190225停止搜寻附近的蓝牙外围设备若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索stopBluetoothDevicesDiscovery:function(){ varthat=this; varlog=thatdatatextLog+停止搜寻附近的蓝牙外围设备
; thatsetData({ textLog:log }); wxstopBluetoothDevicesDiscovery()},*监听寻找到新设备的事件onBluetoothDeviceFound:function(){varthat=this;wxonBluetoothDeviceFound(function(res){resdevicesforEach(function(device){if(!devicename&&!devicelocalName){return}constfoundDevices=thatdatadevices;constidx=inArray(foundDevices,deviceId,devicedeviceId);constdata={};if(idx===1){data[devices[${foundDeviceslength}]]=device}else{data[devices[${idx}]]=device}thatsetData(data)})})},连接低功耗蓝牙设备createBLEConnection:function(e){varthat=this;constds=ecurrentTargetdataset;constdevId=dsdeviceId;设备UUIDconstname=dsname;设备名thatstopConnectDevices();配对之前先断开已连接设备appshowLoading(正在连接,请稍后);varlog=thatdatatextLog+正在连接,请稍后
;thatsetData({textLog:log});appshowLoading(连接中);wxcreateBLEConnection({deviceId:devId,success:function(res){wxhideLoading();隐藏loadingvarlog=thatdatatextLog+配对成功,获取服务
;thatsetData({textLog:log,connected:true,name,devId,});thatgetBLEDeviceServices(devId)},fail:function(err){wxhideLoading();隐藏loadingvarlog=thatdatatextLog+连接失败,错误码:+errerrCode+
;thatsetData({textLog:log});if(errerrCode===10012){appshowModal1(连接超时,请重试!);}elseif(errerrCode===10013){appshowModal1(连接失败,蓝牙地址无效!);}else{appshowModal1(连接失败,请重试!);+errerrCode10003原因多种:蓝牙设备未开启或异常导致无法连接;蓝牙设备被占用或者上次蓝牙连接未断开导致无法连接}thatcloseBLEConnection()},});thatstopBluetoothDevicesDiscovery();停止搜索},断开与低功耗蓝牙设备的连接closeBLEConnection:function(){wxcloseBLEConnection({deviceId:thisdatadevId})thissetData({connected:false,chs:[],canWrite:false,})},获取蓝牙设备所有service(服务)getBLEDeviceServices:function(devId){varthat=this;wxgetBLEDeviceServices({deviceId:devId,success:function(res){for(leti=0;i<resserviceslength;i++){if(resservices[i]isPrimary){该服务是否为主服务vars=resservices[i]uuid;varlog=thatdatatextLog+该服务是为主服务:+resservices[i]uuid+
;thatsetData({textLog:log});wxnavigateTo({url:pagesfunctionPagefunctionPage?name=+encodeURIComponent(thatdataname)+&deviceId=+encodeURIComponent(devId)+&serviceId=+encodeURIComponent(resservices[i]uuid)});return}}}})},aboutClick:function(){wxnavigateTo({url:pagesaboutabout});}})
更多有关「蓝牙调试小程序不能全部检测周围的蓝牙」的讨论可以前往微信公开课小程序参与互动哦!了解更多
方法1:微信扫描微信公开课小程序码即可订阅热门讨论「蓝牙调试小程序不能全部检测周围的蓝牙」
方法2:微信搜索微信公开课小程序名称进入,即可订阅热门讨论「蓝牙调试小程序不能全部检测周围的蓝牙」
方法3:微信网页访问即速商店,长按识别微信公开课小程序码即可订阅热门讨论「蓝牙调试小程序不能全部检测周围的蓝牙」
微信公开课小程序热门讨论「蓝牙调试小程序不能全部检测周围的蓝牙」由微信公开课原创摘录于微信小程序商店shop.jisuapp.cn,转载请注明出处。
微信公开课热门讨论「蓝牙调试小程序不能全部检测周围的蓝牙」由微信公开课开发者向微信用户提供,并对本服务内容、数据资料及其运营行为等真实性、合法性及有效性承担全部责任。