微信小程序之蓝牙的链接

微信小程序之蓝牙的链接

2023年6月28日发(作者:)

微信⼩程序之蓝⽛的链接微信⼩程序之蓝⽛的链接微信⼩程序蓝⽛连接2.0说明:1、本版本区分了ANDROID和IOS系统下蓝⽛连接的不同⽅式。

2、兼容了更多情况下的链接包括:(1)未开启设备蓝⽛,当监听到开启了蓝⽛后⾃动开始连接。

(2)初始化蓝⽛失败后每3000ms⾃动重新初始化蓝⽛适配器。

(3)安卓端开启蓝⽛适配器扫描失败,每3000ms⾃动重新开启。

(4)IOS端获取已连接蓝⽛设备为空,每3000ms⾃动重新获取。

(5)安卓端蓝⽛开始链接后中断扫描,连接失败了,重新开始扫描。

(6)IOS端开始连接设备后,停⽌获取已连接设备,连接失败⾃动重新开启获取。

(7)连接成功后,关闭系统蓝⽛,蓝⽛适配器重置。

(8)连接成功后,关闭系统蓝⽛,再次打开蓝⽛,⾃动重新开始连接。

(9)连接成功后,关闭⽬标蓝⽛设备,⾃动重新开始扫描(获取)。

(10)连接成功后,最⼩化⼩程序(连接未中断),打开⼩程序显⽰已连接。

(11)连接成功后,杀掉⼩程序进程,连接关闭,⾃动重新开始扫描(获取)。3、想起来了再来更新....。

4、流程图,明天或后天或...谁有空帮我画⼀下也⾏。我的连接是在中做的。

在中的onLaunch触发是调⽤ init()⽅法。

init代码:init: function (n) { = []; eId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"; eId_2 = "00001803-0000-1000-8000-00805F9B34FB"; eId_3 = "00001814-0000-1000-8000-00805F9B34FB"; eId_4 = "00001802-0000-1000-8000-00805F9B34FB"; eId_5 = "00001804-0000-1000-8000-00805F9B34FB"; eId_6 = "00001535-1212-EFDE-1523-785FEABCD123"; terId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E"; terId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E"; tDeviceIndex = 0; ingConnected = false; overing = false; ecting = false; tedDevice = {}; ('init state', ); if (! || n == 200) { = false; Id = ''; rHasInit = false } onnect(); }说明: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 () return; Id = ""; = false; // 如果适配器已经初始化不在调⽤初始化(重复初始化会报错) if (rHasInit == undefined || rHasInit) return; ading({ title: '初始化蓝⽛', duration: 2000 }); // 开启蓝⽛适配器状态监听 AdapterStateChange(); // 初始化蓝⽛适配器状态(必须步骤,否则⽆法进⾏后续的任何操作) uetoothAdapter({ success: function (res) { ("初始化蓝⽛适配器成功"); etoothAdapterState(); rHasInit = true; }, fail: function (err) { (err); ading({ title: '请开蓝⽛', icon: 'loading', duration: 2000 }) } }); }说明:这段有注释,就不多说了,⽐较简单。在初始化蓝⽛适配器状态成功后调⽤getBluetoothAdapterState()⽅法。getBluetoothAdapterState代码:getBluetoothAdapterState: function () { var that = this; etoothAdapterState({ success: function (res) { (res); var available = ble; overing = ering; if (!available) { ading({ title: '请开蓝⽛', icon: 'loading', duration: 2000 }) } else { if (!tedDevice['state']) { fDiscovering(ering); } } }, fail: function (err) { (err); } }) }说明:此⽅法是⽤来获取当前蓝⽛状态。当检测到蓝⽛可⽤时调⽤judegIfDiscovering⽅法。judegIfDiscovering代码:judegIfDiscovering: function (discovering) { var that = this; if (ectinng) return; nectedBluetoothDevices({ services: [eId], success: function (res) { ("获取处于连接状态的设备", res); var devices = res['devices']; if (devices[0]) { if (oidPlatform) { ast({ title: '蓝⽛连接成功', icon: 'success', duration: 2000 }); } else { nectedBluetoothDevices(256); } } else { if (discovering) { ading({ title: '蓝⽛搜索中' }) } else { if (oidPlatform) { luetoothDevicesDiscovery(); } else { nectedBluetoothDevices(267); } } } }, fail: function (err) { ('getConnectedBluetoothDevices err 264', err); if (oidPlatform) { luetoothDevicesDiscovery(); } else { nectedBluetoothDevices(277); } } }); }说明:

1、此⽅法是⽤来判断是否正在扫描。

2、isAndroidPlatform 是通过⼩程序的getSystemInfo获取到的判断是安卓设备还是IOS设备。如果是安卓设备调⽤startBluetoothDevicesDiscovery()开启扫描,如果是IOS设备调⽤getConnectedBluetoothDevices() 开启获取已配对的蓝⽛设备。startBluetoothDevicesDiscovery代码:startBluetoothDevicesDiscovery: function () { var that = this; if (!oidPlatform) return; if (!tedDevice['state']) { etoothAdapterState({ success: function (res) { (res); var available = ble; overing = ering; if (!available) { ading({ title: '请开蓝⽛', icon: 'loading', duration: 2000 }) } else { if (ering) { ading({ title: '蓝⽛搜索中' }) } else { luetoothDevicesDiscovery({ services: [], allowDuplicatesKey: true, success: function (res) { toothDeviceFound(); ading({ title: '蓝⽛搜索中' }) }, fail: function (err) { if (overing) { ading({ title: '蓝⽛搜索中' }) } else { iscoveryTimer = setTimeout(function () { if (!) { luetoothDevicesDiscovery(); } }, 5000) } } }); } } }, fail: function (err) { (err); } }) }说明:1、仅在安卓端设备上开启扫描附近蓝⽛设备。2、在开启成功的回调中开启发现新蓝⽛设备的事件监听onBluetoothDeviceFound()。onBluetoothDeviceFound代码:[mw_shl_code=javascript,true]onBluetoothDeviceFound: function () { var that = this; toothDeviceFound(function (res) { ('new device list has founded'); if (s[0]) { var name = s[0]['name']; if (f('FeiZhi') != -1) { var deviceId = s[0]['deviceId']; (deviceId); Id = deviceId; if (!ecting) { onnectDevices(); } } } }) }说明:

1、此处对已发现的蓝⽛设备根据name属性进⾏了过滤。

2、当筛选出含有需要连接的设备的name属性的设备是获取到deviceId,开始连接调⽤startConnectDevices()⽅法。startConnectDevices代码:startConnectDevices: function (ltype, array) { var that = this; clearTimeout(nectedTimer); clearTimeout(iscoveryTimer); nectedTimer = null; iscoveryTimer = null; ectinng = true; ading({ title: '正在连接' }); uetoothDevicesDiscovery(); BLEConnection({ deviceId: Id, success: function (res) { ('连接成功', res); ading({ title: '正在连接' }); = true; Id = Id; if (e == 0) { setTimeout(function () { vice(Id); }, 5000) } onnectionStateChange(function (res) { ('连接变化', res); = ted; Id = Id; if (!ted) { ('200'); } }); }, fail: function (err) { ('连接失败:', err); ading(); if (ltype == 'loop') { array = (0, 1); (array); nnect(array); } else { if (oidPlatform) { luetoothDevicesDiscovery(); } else { nectedBluetoothDevices(488); } } }, complete: function () { ectinng = false; } }); }说明:

1、开启连接后终⽌扫描(获取已配对)⽅法。

2、根据deviceId创建低功耗蓝⽛连接。如果连接成功,就继续做后续读写操作。

3、如果连接失败根据设备系统分别调⽤startBluetoothDevicesDiscovery() 或 getConnectedBluetoothDevices();getConnectedBluetoothDevices代码:getConnectedBluetoothDevices: function (n) { var that = this; ingConnected = true; ading({ title: '蓝⽛搜索中' }); nectedBluetoothDevices({ services: [eId], success: function (res) { ("获取处于连接状态的设备", res); var devices = res['devices'], flag = false, index = 0, conDevList = []; h(function (value, index, array) { if (value['name'].indexOf('FeiZhi') != -1) { // 如果存在包含FeiZhi字段的设备 flag = true; index += 1; (value['deviceId']); Id = value['deviceId']; } }); if (flag) { tDeviceIndex = 0; nnect(conDevList); } else { GetConnected(); } }, fail: function (err) { GetConnected(); }, complete: function () { ingConnected = false; } }); }说明:如果获取蓝⽛已配对的蓝⽛设备失败了,或获取到的列表为空调⽤failToGetConnected();failToGetConnected代码:failToGetConnected: function () { var that = this; if (!nectedTimer) { clearTimeout(nectedTimer); nectedTimer = null; } nectedTimer = setTimeout(function () { etoothAdapterState({ success: function (res) { (res); var available = ble; if (!available) { ading({ title: '请开蓝⽛', icon: 'loading', duration: 2000 }) } else { if (!tedDevice['state']) { nectedBluetoothDevices(); } } }, fail: function (err) { (err); } }) }, 5000); }说明:

1、该⽅法调⽤成功后返回的devices是⼀个数组包含多个已经系统配对的蓝⽛设备。

2、如果devices列表获取到调⽤loopConnect()⽅法开始递归调⽤连接蓝⽛设备。loopConnect代码:loopConnect: function (array) { var that = this; var listLen = ; if (array[0]) { Id = array[0]; if (!ecting) { onnectDevices('loop', array); } } else { ('已配对的设备⼩程序蓝⽛连接失败'); if (!oidPlatform) { nectedBluetoothDevices(431); } } }说明:looConnect在创建连接的⽅法连接失败后会操作删除数组的第⼀个值,然后继续调⽤该⽅法,直到其中所有的设备都连接过。

差点漏了:在的onShow⾥调⽤init()⽅法。特别说明:1、安卓和IOS的蓝⽛连接在当前版本中推荐采⽤不同⽅式。安卓设备直接使⽤⼩程序的蓝⽛连接,取消系统配对。IOS设备先系统配对在打开⼩程序可以时效秒连接成功。 2、此版本的连接仍然有待完善,连接不会⾃动终⽌(需要的可以⾃⼰加),会⽆限扫描重连,直到成功。

3、链接成功后的操作如果写⼊数据和开启notify需要同时进⾏,建议先写⼊,后开启notify。(原因未知,否则必然出现10008错误)。如有疑问请留⾔或者到本站社区交流讨论,感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687896404a54841.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信