微信小程序中蓝牙的链接
这篇文章主要介绍了微信小程序之蓝牙的链接的相关资料,希望通过本文大家能够掌握小程序蓝牙的开发方法,需要的朋友可以参考下
微信小程序之蓝牙的链接
微信小程序蓝牙连接2.0说明:
1、本版本区分了ANDROID和IOS系统下蓝牙连接的不同方式。
2、兼容了更多情况下的链接包括:
(1)未开启设备蓝牙,当监听到开启了蓝牙后自动开始连接。
(2)初始化蓝牙失败后每3000ms自动重新初始化蓝牙适配器。
(3)安卓端开启蓝牙适配器扫描失败,每3000ms自动重新开启。
(4)IOS端获取已连接蓝牙设备为空,每3000ms自动重新获取。
(5)安卓端蓝牙开始链接后中断扫描,连接失败了,重新开始扫描。
(6)IOS端开始连接设备后,停止获取已连接设备,连接失败自动重新开启获取。
(7)连接成功后,关闭系统蓝牙,蓝牙适配器重置。
(8)连接成功后,关闭系统蓝牙,再次打开蓝牙,自动重新开始连接。
(9)连接成功后,关闭目标蓝牙设备,自动重新开始扫描(获取)。
(10)连接成功后,最小化小程序(连接未中断),打开小程序显示已连接。
(11)连接成功后,杀掉小程序进程,连接关闭,自动重新开始扫描(获取)。
3、想起来了再来更新....。
4、流程图,明天或后天或...谁有空帮我画一下也行。
我的连接是在App.js中做的。
在App.js中的onLaunch触发是调用 init()方法。
init代码:
init: function (n) { this.list = []; this.serviceId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"; this.serviceId_2 = "00001803-0000-1000-8000-00805F9B34FB"; this.serviceId_3 = "00001814-0000-1000-8000-00805F9B34FB"; this.serviceId_4 = "00001802-0000-1000-8000-00805F9B34FB"; this.serviceId_5 = "00001804-0000-1000-8000-00805F9B34FB"; this.serviceId_6 = "00001535-1212-EFDE-1523-785FEABCD123"; this.characterId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E"; this.characterId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E"; this.connectDeviceIndex = 0; this.isGettingConnected = false; this.isDiscovering = false; this.isConnecting = false; this.connectedDevice = {}; console.log('init state', this.connectedDevice.state); if (!this.connectedDevice.state || n == 200) { this.connectedDevice.state = false; this.connectedDevice.deviceId = ''; this.adapterHasInit = false } this.startConnect(); }
说明:
1、 serviceId_2~6 是我已知的想要连接的蓝牙设备的serviceId可以只写一个。
2、characterId_write 是我已知的想要连接的蓝牙设备写入数据的特征值。
3、characterId_read是我已知的想要连接的蓝牙设备读取数据的特征值。
(以上3个都是为了做比对,真实的操作按照获取到的sericeid, characterid为准)。
4、connectedDevice 是已连接了的设备信息对象。
init完成后开始调用连接 startConnect();
startConnect代码:
startConnect: function () { var that = this; if (that.connectedDevice.state) return; that.connectedDevice.deviceId = ""; that.connectedDevice.state = false; // 如果适配器已经初始化不在调用初始化(重复初始化会报错) if (this.adapterHasInit == undefined || this.adapterHasInit) return; wx.showLoading({ title: '初始化蓝牙', duration: 2000 }); // 开启蓝牙适配器状态监听 this.listenAdapterStateChange(); // 初始化蓝牙适配器状态(必须步骤,否则无法进行后续的任何操作) wx.openBluetoothAdapter({ success: function (res) { console.log("初始化蓝牙适配器成功"); that.getBluetoothAdapterState(); that.adapterHasInit = true; }, fail: function (err) { console.log(err); wx.showLoading({ title: '请开蓝牙', icon: 'loading', duration: 2000 }) } }); }
说明:这段有注释,就不多说了,比较简单。
在初始化蓝牙适配器状态成功后调用getBluetoothAdapterState()方法。
getBluetoothAdapterState代码:
getBluetoothAdapterState: function () { var that = this; wx.getBluetoothAdapterState({ success: function (res) { console.log(res); var available = res.available; that.isDiscovering = res.discovering; if (!available) { wx.showLoading({ title: '请开蓝牙', icon: 'loading', duration: 2000 }) } else { if (!that.connectedDevice['state']) { that.judegIfDiscovering(res.discovering); } } }, fail: function (err) { console.log(err); } }) }
说明:此方法是用来获取当前蓝牙状态。
当检测到蓝牙可用时调用judegIfDiscovering方法。
judegIfDiscovering代码:
judegIfDiscovering: function (discovering) { var that = this; if (this.isConnectinng) return; wx.getConnectedBluetoothDevices({ services: [that.serviceId], success: function (res) { console.log("获取处于连接状态的设备", res); var devices = res['devices']; if (devices[0]) { if (that.isAndroidPlatform) { wx.showToast({ title: '蓝牙连接成功', icon: 'success', duration: 2000 }); } else { that.getConnectedBluetoothDevices(256); } } else { if (discovering) { wx.showLoading({ title: '蓝牙搜索中' }) } else { if (that.isAndroidPlatform) { that.startBluetoothDevicesDiscovery(); } else { that.getConnectedBluetoothDevices(267); } } } }, fail: function (err) { console.log('getConnectedBluetoothDevices err 264', err); if (that.isAndroidPlatform) { that.startBluetoothDevicesDiscovery(); } else { that.getConnectedBluetoothDevices(277); } } }); }
说明:
1、此方法是用来判断是否正在扫描。
2、isAndroidPlatform 是通过小程序的getSystemInfo获取到的判断是安卓设备还是IOS设备。
如果是安卓设备调用startBluetoothDevicesDiscovery()开启扫描,如果是IOS设备调用getConnectedBluetoothDevices() 开启获取已配对的蓝牙设备。
startBluetoothDevicesDiscovery代码:
startBluetoothDevicesDiscovery: function () { var that = this; if (!this.isAndroidPlatform) return; if (!this.connectedDevice['state']) { wx.getBluetoothAdapterState({ success: function (res) { console.log(res); var available = res.available; that.isDiscovering = res.discovering; if (!available) { wx.showLoading({ title: '请开蓝牙', icon: 'loading', duration: 2000 }) } else { if (res.discovering) { wx.showLoading({ title: '蓝牙搜索中' }) } else { wx.startBluetoothDevicesDiscovery({ services: [], allowDuplicatesKey: true, success: function (res) { that.onBluetoothDeviceFound(); wx.showLoading({ title: '蓝牙搜索中' }) }, fail: function (err) { if (err.isDiscovering) { wx.showLoading({ title: '蓝牙搜索中' }) } else { that.startDiscoveryTimer = setTimeout(function () { if (!that.connectedDevice.state) { that.startBluetoothDevicesDiscovery(); } }, 5000) } } }); } } }, fail: function (err) { console.log(err); } }) }
说明:
1、仅在安卓端设备上开启扫描附近蓝牙设备。
2、在开启成功的回调中开启发现新蓝牙设备的事件监听onBluetoothDeviceFound()。
onBluetoothDeviceFound代码:
[mw_shl_code=javascript,true]onBluetoothDeviceFound: function () { var that = this; wx.onBluetoothDeviceFound(function (res) { console.log('new device list has founded'); if (res.devices[0]) { var name = res.devices[0]['name']; if (name.indexOf('FeiZhi') != -1) { var deviceId = res.devices[0]['deviceId']; console.log(deviceId); that.deviceId = deviceId; if (!that.isConnecting) { that.startConnectDevices(); } } } }) }
说明:
1、此处对已发现的蓝牙设备根据name属性进行了过滤。
2、当筛选出含有需要连接的设备的name属性的设备是获取到deviceId,开始连接调用startConnectDevices()方法。
startConnectDevices代码:
startConnectDevices: function (ltype, array) { var that = this; clearTimeout(this.getConnectedTimer); clearTimeout(this.startDiscoveryTimer); this.getConnectedTimer = null; this.startDiscoveryTimer = null; this.isConnectinng = true; wx.showLoading({ title: '正在连接' }); that.stopBluetoothDevicesDiscovery(); wx.createBLEConnection({ deviceId: that.deviceId, success: function (res) { console.log('连接成功', res); wx.showLoading({ title: '正在连接' }); that.connectedDevice.state = true; that.connectedDevice.deviceId = that.deviceId; if (res.errCode == 0) { setTimeout(function () { that.getService(that.deviceId); }, 5000) } wx.onBLEConnectionStateChange(function (res) { console.log('连接变化', res); that.connectedDevice.state = res.connected; that.connectedDevice.deviceId = res.deviceId; if (!res.connected) { that.init('200'); } }); }, fail: function (err) { console.log('连接失败:', err); wx.hideLoading(); if (ltype == 'loop') { array = array.splice(0, 1); console.log(array); that.loopConnect(array); } else { if (that.isAndroidPlatform) { that.startBluetoothDevicesDiscovery(); } else { that.getConnectedBluetoothDevices(488); } } }, complete: function () { that.isConnectinng = false; } }); }
说明:
1、开启连接后终止扫描(获取已配对)方法。
2、根据deviceId创建低功耗蓝牙连接。如果连接成功,就继续做后续读写操作。
3、如果连接失败根据设备系统分别调用startBluetoothDevicesDiscovery() 或 getConnectedBluetoothDevices();
getConnectedBluetoothDevices代码:
getConnectedBluetoothDevices: function (n) { var that = this; that.isGettingConnected = true; wx.showLoading({ title: '蓝牙搜索中' }); wx.getConnectedBluetoothDevices({ services: [that.serviceId], success: function (res) { console.log("获取处于连接状态的设备", res); var devices = res['devices'], flag = false, index = 0, conDevList = []; devices.forEach(function (value, index, array) { if (value['name'].indexOf('FeiZhi') != -1) { // 如果存在包含FeiZhi字段的设备 flag = true; index += 1; conDevList.push(value['deviceId']); that.deviceId = value['deviceId']; } }); if (flag) { that.connectDeviceIndex = 0; that.loopConnect(conDevList); } else { that.failToGetConnected(); } }, fail: function (err) { that.failToGetConnected(); }, complete: function () { that.isGettingConnected = false; } }); }
说明:如果获取蓝牙已配对的蓝牙设备失败了,或获取到的列表为空调用failToGetConnected();
failToGetConnected代码:
failToGetConnected: function () { var that = this; if (!that.getConnectedTimer) { clearTimeout(that.getConnectedTimer); that.getConnectedTimer = null; } that.getConnectedTimer = setTimeout(function () { wx.getBluetoothAdapterState({ success: function (res) { console.log(res); var available = res.available; if (!available) { wx.showLoading({ title: '请开蓝牙', icon: 'loading', duration: 2000 }) } else { if (!that.connectedDevice['state']) { that.getConnectedBluetoothDevices(); } } }, fail: function (err) { console.log(err); } }) }, 5000); }
说明:
1、该方法调用成功后返回的devices是一个数组包含多个已经系统配对的蓝牙设备。
2、如果devices列表获取到调用loopConnect()方法开始递归调用连接蓝牙设备。
loopConnect代码:
loopConnect: function (array) { var that = this; var listLen = array.length; if (array[0]) { that.deviceId = array[0]; if (!that.isConnecting) { that.startConnectDevices('loop', array); } } else { console.log('已配对的设备小程序蓝牙连接失败'); if (!that.isAndroidPlatform) { that.getConnectedBluetoothDevices(431); } } }
说明:looConnect在创建连接的方法连接失败后会操作删除数组的第一个值,然后继续调用该方法,直到其中所有的设备都连接过。
差点漏了:在app.js的onShow里调用init()方法。
特别说明:
1、安卓和IOS的蓝牙连接在当前版本中推荐采用不同方式。安卓设备直接使用小程序的蓝牙连接,取消系统配对。IOS设备先系统配对在打开小程序可以时效秒连接成功。
2、此版本的连接仍然有待完善,连接不会自动终止(需要的可以自己加),会无限扫描重连,直到成功。
3、链接成功后的操作如果写入数据和开启notify需要同时进行,建议先写入,后开启notify。(原因未知,否则必然出现10008错误)。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是微信小程序中蓝牙的链接的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

一般来说,我们只需要同时使用耳机或者音响的其中一个设备,但是有些朋友反映在win11系统中,遇到了耳机和音响一起响的问题,其实我们可以在realtek面板中将它关闭,就可以了,下面一起来看一下吧。win11耳机和音响一起响怎么办1、首先在桌面上找到并打开“控制面板”2、进入控制面板,在其中找到并打开“硬件和声音”3、然后再找到一个喇叭图标的“Realtek高清晰音频管理器”4、选择“扬声器”再点击“后面板”进入扬声器设置。5、打开之后我们可以看到设备类型,如果要关闭耳机就取消勾选“耳机”,如果要

1、在屏幕底端向上滑,调出控制中心,如下图,点击蓝牙开关即可打开蓝牙。2、我们就可以连接其他配对过的蓝牙设备或者点击【搜索蓝牙设备】来和新的蓝牙设备进行连接,想要其他小伙伴搜到自己的手机来连接蓝牙时记得要打开【可检测性】开关哦。方法二、1、进入手机桌面,找到并打开设置。2、在【设置】目录下拉找到【更多设置】,点击进入,3、点击打开【蓝牙】,将蓝牙开关打开即可打开蓝牙啦。

闲鱼官方微信小程序悄然上线,在小程序中可以发布闲置与买家/卖家私信交流、查看个人资料及订单、搜索物品等,有用好奇闲鱼微信小程序叫什么,现在快来看一下。闲鱼微信小程序叫什么答案:闲鱼,闲置交易二手买卖估价回收。1、在小程序中可以发布闲置、与买家/卖家私信交流、查看个人资料及订单、搜索指定物品等功能;2、在小程序的页面中有首页、附近、发闲置、消息、我的5项功能;3、想要使用的话必要要开通微信支付才可以购买;

win11系统设备管理器没有蓝牙模块在使用Windows11系统时,有时候会遇到设备管理器中没有蓝牙模块的情况。这可能会给我们的日常使用带来不便,因为蓝牙技术在现代社会中已经变得非常普遍,我们经常需要用到它来连接无线设备。如果你在设备管理器中找不到蓝牙模块,不要担心,下面将为你介绍一些可能的解决方法:1.检查硬件连接:首先要确保你的电脑或笔记本电脑上确实有蓝牙模块。有些设备可能并不自带蓝牙功能,这时你需要购买一个外部的蓝牙适配器来连接。2.更新驱动程序:有时候设备管理器中没有蓝牙模块是因为驱动程

哈利波特魔法觉醒中最近新增了一个魔咒互换功能,需要玩家使用蓝牙或者WiFi来互换魔咒,有些玩家发现自己使用不了蓝牙互换,那么怎么才能使用蓝牙互换魔咒呢?接下来小编就给大家带来了哈利波特魔咒互换用不了蓝牙解决方法,希望能够帮助到大家。哈利波特魔咒互换用不了蓝牙解决方法1、首先玩家需要在图书馆中找到魔咒互换,就可以使用蓝牙或者WiFi进行互换了。2、点击使用蓝牙,提示需要下载新的安装包,但是之前已经下载过了,有些玩家就开始犯糊涂了。3、其实新的安装包玩家去商店即可下载,ios就去苹果商店中更新,安卓

我们在买手机的时候,会看到手机参数里有一个支持蓝牙选项,有时候就会遇到购买的蓝牙耳机和手机不匹配的情况,那么蓝牙5.3需要手机支持吗,其实是不需要的。蓝牙5.3需要手机支持吗:答:蓝牙5.3需要手机支持。不过只要支持蓝牙的手机就可以使用。1、蓝牙是向下兼容的,不过要使用对应版本就需要手机支持。2、举例来说,如果我们购买了一款使用蓝牙5.3的无线蓝牙耳机。3、那么,如果我们的手机仅支持蓝牙5.0,那么在连接时使用的就是蓝牙5.0。4、因此,我们依旧是可以使用这款手机连接耳机来听歌的,但是速度不如蓝

Win11系统蓝牙无法使用怎么办随着Win11系统的推出,许多用户都迫不及待地升级了他们的电脑。然而,一些用户在升级后遇到了一个常见的问题:蓝牙无法使用。这对于那些依赖蓝牙设备的用户来说是一个令人沮丧的问题。幸运的是,有一些简单的解决方法可以帮助您解决这个问题。首先,您可以尝试重新启动您的电脑。有时候,只需简单地重新启动系统就可以解决一些问题,包括蓝牙无法使用的问题。在重新启动后,检查蓝牙是否正常工作。如果重新启动没有解决问题,那么您可以尝试更新蓝牙驱动程序。有时候,旧的或损坏的驱动程序可能会导

2月21日消息,三星公司今天发布公告,宣布为已升级OneUI6.1及更高版本的GalaxyS23系列、ZFold5、ZFlip5手机,以及GalaxyTabS9系列平板,带来蓝牙Auracast音频广播功能。三星于去年开始,已陆续在GalaxyBuds2Pro耳机、三星智能电视上添加对蓝牙Auracast音频广播功能的支持,运行OneUI6.1的GalaxyS24系列手机已添加支持。符合适配升级到OneUI6.1的三星设备,可以打开“设置”>“蓝牙”>“三点菜单”>“使用Auracast广播声音
