回答

收藏

[评测分享] 【Silicon Labs xG24-EK2703A 套件】-5-微信小程序蓝牙点灯

#板卡评测 #板卡评测 1262 人阅读 | 0 人回复 | 2023-06-15

本帖最后由 SensorYoung 于 2023-6-15 12:06 编辑

首先感谢网友“dql2016”在隔壁论坛分享的一个微信小程序的模板,我再次基础上稍微修改了一下,能够完成微信小程序打开或者关闭Silicon Labs xG24-EK2703A开发板上LED灯目标。
下面左图是实际小程序运行界面,右侧是开发工具中的模拟器。在开发的过程中也遇到了小程序开发工具中无法查看log等情况,https://developers.weixin.qq.com/community/develop/article/doc/000826faa68ae033e2cf7ab6b5c813,后来发现别的朋友也有类似的问题,暂时没有解决。




在使用的时候,首先点击“搜索蓝牙设备”,这样手机就作为蓝牙中心设备搜索附件的BLE外设。之后找到例程和BLE外设地址,确认后点击确定,选择开发板的例程。这里之所以显示的是xG24-EK2703A Explor Kit,因由于在SSV5配置工具中对此处进行了相应的修改。

在index.wxml文件中,按钮“搜索蓝牙设备”绑定了一个回调函数“searchBleEvent”。
  1.   <button bindtap="searchBleEvent" class='btn' style="width: 292rpx; height: 263rpx; display: block; box-sizing: border-box; left: -190rpx; top: 40rpx; position: relative">
  2.     <image style="width: 81rpx; height: 78rpx; display: inline-block; box-sizing: border-box; left: NaNrpx; top: NaNrpx"  src="/images/icon/BlueToothSearch.png" class='btn-img' />
  3.     <view>搜索蓝牙设备</view>
  4.   </button>
复制代码
这个函数的定义在index.js文件中,如下所示。之后会调用initBLE()来进行BLE初始化
  1.   searchBleEvent: function(ret){
  2.     var ssid = this.data.ssid;
  3.     var pass = this.data.pass;
  4.     console.log(ssid, pass);
  5.     this.initBLE();
  6.   },
复制代码
接下来调用微信小程序BLE api:wx.openBluetoothAdapter()来初始化BLE模块,参考:https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth/wx.openBluetoothAdapter.html
  1.   initBLE: function() {
  2.     this.printLog("启动蓝牙适配器, 蓝牙初始化")
  3.     var that = this;
  4.     wx.openBluetoothAdapter({
  5.       success: function(res) {
  6.         console.log(res);
  7.         that.findBLE();
  8.       },
  9.       fail: function(res) {
  10.         util.toastError('请先打开蓝牙');
  11.       }
  12.     })
  13.   },
复制代码



接下来点击“蓝牙设备配网”,会进行BLE初始化,然后与上一步中选择的BLE外设进行BLE连接,获取service以及character等操作。“蓝牙配网”按钮绑定的是“bleConfigEvent”这个函数。
  1.   <button bindtap="bleConfigEvent" class='btn' style="position: relative; left: 181rpx; top: -221rpx; width: 292rpx; height: 263rpx; display: block; box-sizing: border-box">
  2.     <image style="width: 81rpx; height: 74rpx; display: inline-block; box-sizing: border-box; left: NaNrpx; top: NaNrpx"  src="/images/icon/BlueTooth.png" class='btn-img' />
  3.     <view>蓝牙设备配网</view>
  4.   </button>
复制代码

bleConfigEvent”会调用“createBLE”来建立BLE连接等。
  1.   createBLE: function(deviceId){
  2.     this.printLog("连接: [" + deviceId+"]");
  3.     var that = this;
  4.     this.closeBLE(deviceId, function(res){
  5.       console.log("预先关闭,再打开");
  6.       setTimeout(function(){
  7.         wx.createBLEConnection({
  8.           deviceId: deviceId,
  9.           success: function (res) {
  10.             that.printLog("设备连接成功");
  11.             that.getBLEServiceId(deviceId);
  12.           },
  13.           fail: function (res) {
  14.             that.printLog("设备连接失败" + res.errMsg);
  15.           }
  16.         })
  17.       }, 2000)
  18.     });
  19.   },
复制代码



相关的基础知识可以参考微信小程序官方文档:蓝牙低功耗 (Bluetooth Low Energy, BLE) https://developers.weixin.qq.com/miniprogram/dev/framework/device/ble.html


下面是实物展示:
串口助手也可以正常接收数据:



总结:
Silicon Labs提供了完整且功能强大的蓝牙开发工具,包括了各种software Component,可以方便开发各种BLE应用以满足实际项目需求。




分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条