在网上没有微信小程序蓝牙板块特征值读取和监听的Demo,所以自己写了一个,现在分享出来,下面是Demo的页面
发送指令后(图2)在串口调试助手上正确收到相应值:
串口调试助手上发送指令,小程序也能正确监听到(图3):
Demo源码下载地址:https://download.youkuaiyun.com/download/qq_36456827/10591811
串口调试助手下载地址:https://download.youkuaiyun.com/download/qq_36456827/10591839
下面是部分源码的介绍:
wxml:
<view class="container">
<view class='btn' bindtap='onConnect'>{
{isConnected?'已连接':'连接蓝牙'}}</view>
<view class='btn' style='margin-top:24rpx' catchtap='onGetuuid'>
{
{serviceId&&characteristicId?'已获取设备信息':'获取设备信息'}}
</view>
<input placeholder='在此输入发送指令' bindinput='onCommand'></input>
<view class='btn' style='margin-top:0' catchtap='onSendCommand' >发送指令</view>
<view class='btn' style='margin-top:24rpx' catchtap='onCloseConnect'>断开连接</view>
</view>
页面布局很简单,就四个按钮和一个文本框,下面主要介绍几个比较关键的js函数
onConnect() {
let that = this;
wx.openBluetoothAdapter({
success: function(res) {
if (!that.data.isConnected) {
that.startBluetoothDevicesDiscovery();//开始搜索蓝牙设备
wx.showLoading({
title: '搜索中',
})