1、Uni-APP蓝牙使用流程
在阅读这篇内容之前,建议您详细浏览一遍uni-app官方关于蓝牙和低功耗蓝牙(BLE)使用教程文档
uni-app官方低功耗蓝牙接口,如果您有微信小程序方面的开发经验,那您看这篇文章会很快上手,uni-app官方的蓝牙模块接口均是按照微信小程序的蓝牙模块接口编写。
思路流程
手机上打开蓝牙,设备打开蓝牙
APP初始化蓝牙模块
APP调用蓝牙搜寻设备
APP存储搜寻到的指定设备deviceId
APP根据指定deviceId连接蓝牙设备
APP连接设备之后获取设备的服务列表serviceId
APP根据serviceId获取设备的特征值列表characteristicId
APP根据指定特征值属性开启notify订阅特征值
APP根据指定特征值属性向设备发送写入控制指令
设备根据APP发送指令作出相应操作
设备通过特征值数据改变,APP的notify订阅查看改变的值
详细流程
首先要初始化蓝牙模块openBluetoothAdapter,即查看蓝牙是否可用,若初始化失败,则是蓝牙未打开,提示用户打开蓝牙。若已经打开蓝牙,则准备进行蓝牙搜索。
搜索蓝牙startBluetoothDevicesDiscovery,执行搜索方法,开始搜索蓝牙设备。这一步需要和关闭搜索蓝牙stopBluetoothDevicesDiscovery成对使用,长时间占用搜索设备,浪费资源,在查找到需要的设备连接之后需要主动去停止搜索设备。
查看所有已经发现的蓝牙设备getBluetoothDevices。在这一步可以查看到以前已经获取到的蓝牙设备deviceId。可以在这一步中查看以前已经连接到的设备,主动去尝试连接这个设备。
监听寻找到新设备的事件onBluetoothDeviceFound在这里查看搜索蓝牙已经搜索到的设备,可以在这里通过设备名称(name)或其他已知信息(deviceId等)判断是否已经搜寻到需要的指定设备。
在上两步已经获取到需要连接的设备之后,通过设备ID(deviceId)来连接低功耗蓝牙设备createBLEConnection。在这里如果APP若是已经连接过此低功耗蓝牙设备,可以直接传入之前设备ID进行尝试连接。这一步的连接操作需要和关闭连接closeBLEConnection成对操作。如果多次调用创建连接,有可能会导致系统持有一个设备的多个连接实例,导致在调用关闭连接的时候不能真正关闭连接。
在连接设备之后,APP需要主动去获取蓝牙设备getBLEDeviceServices的所有服务(services),设备会返回给APP设备的服务列表(services)包含设备服务的UUID,该服务是否为主服务。
在获取设备的服务列表之后,根据自己设备的蓝牙协议接口文档,根据指定的服务ID(serviceId)使用获取服务特征值方法getBLEDeviceCharacteristic传入两个参数设备ID(deviceId)和服务ID(serviceId)向设备获取该服务中的所有的特征值(characteristic),设备会向APP返回该服务中的所有特征值列表,列表包含设备特征值的UUID,该特征值支持的操作类型。
启用低功耗蓝牙设备特征值变化时候的notify通知功能,订阅特征值notifyBLECharacteristicValueChange。只有设备的该特征值支持notify或者indicate才可以调用此方法。在此之外,需要注意,只有调用了订阅特征值方法notifyBLECharacteristicValueChange,才能监听设备的特征值改变事件characteristicValueChange。在特征值订阅成功后,只有设备的特征值主动更新,才会触发onBLECharacteristicValueChange回调,回调会显示改变的特征值。
在订阅特征值成功之后,可以向设备写入(发送)控制命令writeBLECharacteristicValue,此方法是向低功耗蓝牙设备特征值写入二进制数据。需要注意只有该特征值的属性支持write才可以调用此方法。在此方法调用成功后,设备特征值发生改变,就会触发onBLECharacteristicValueChange回调,主动返回特征值数据。
流程图
2、Uni-APP蓝牙使用案例
使用uni-app连接血压仪
这部分代码是我测试中写的,主要是按照上面总结的思路流程写的代码,写的有些乱,还又长,就像老太太的裹脚布那样,又臭又长,有的写的不好,无意中总会创造各种各样的BUG,在实际项目中改动了好多部分代码,做了一些蓝牙使用场景的判断,尽量去融合蓝牙设备使用中遇到的各式各样的问题,尽量去提高用户体验。
代码下载连接,拷贝进uni-app项目即可
<template>
<view class="content">
<view class="" v-for="device in devicesList" :key="device">
<text>{{device}}</text>
</view>
<button type="primary" @tap="nextToDiscoveryBLE">下一步</button>
<button type="primary" @tap="pickUpOnce">测量一次</button>
<view class="">
<text>动态血压值: {{measureResult[0].pressure}}</text>
</view>
<view class="">
<text>收缩压值: {{measureResult[0].SYS}}</text>
</view>
<view class="">
<text>舒张压值: {{measureResult[0].DIA}}</text>
</view>
<view class="">
<text>脉搏值: {{measureResult[0].PUL}}</text>
</view>
</view>
</template>
<script>
export default {
components: {
},
data() {
return {
//是否已经打开蓝牙,默认为false,当蓝牙适配器初始化成功后为true
isOpenBle: false,
//主服务的UUID
primaryUUID: "FFF0",
//设备列表
devicesList: [{
"device": "初始设备"
}],
//设备Id
deviceId: "",
//服务Id
serviceId: "",
//特征值ID
characteristicId: [{
//支持写入操作的特征值
writeId: "",
//支持notify操作的特征值
notifyId: ""
}],
//控制命令
wirteControlCode: [{
//开始测量命令
beginMeasure: "0240DD04FFFD020198",
//停止测量命令
stopMeasure: "",
//上传数据命令
uploadData: "",
//停止上传数据命令
stopUploadData: "0240DD04FFFD02059C",
//关机命令
shutDown: ""
}],
//测量结果
measureResult: [{
//动态压力值
pressure: "",
//收缩压
SYS: "",
//舒张压
DIA: "",
//脉搏
PUL: ""
}]
}
},
onLoad() {
//在页面加载时候初始化蓝牙适配器
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
this.$data.isOpenBle = true;
console.log(this.$data.isOpenBle);
},
fail: e => {
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
}
});
//同时监听蓝牙连接状态
this.onBLEConnectionStateChange();
},
methods: {
//下一步按钮
nextToDiscoveryBLE() {
//下一步去查找设备
this.startBluetoothDeviceDiscovery();
},
//测量一次
pickUpOnce() {
this.notifyBLECharacteristicValue();
let self = this;
setTimeout(function() {
self.writeBLECharacteristicValue(self.wirteControlCode[0].beginMeasure);
}, 500);
},
startBluetoothDeviceDiscovery() {
//在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
let self = this;
setTimeout(function() {
if (self.isOpenBle) {
console.log("开始搜寻智能设备");
// 血压仪的主服务ID为FFF0
uni.startBluetoothDevicesDiscovery({
//services: ['fff0'],
success: res => {
//
self.onBluetoothDeviceFound();
},
fail: res => {
console.log("查找设备失败!");
uni.showToast({
icon: "none",
title: "查找设备失败!",
duration: 3000
})
}
});
} else {
console.log("未初始化蓝牙是配饰器:" + self.isOpenBle);
}
}, 300);
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log('停止搜索蓝牙设备:' + e.errMsg);
},
fail: e => {
console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
}
});
},
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
console.log("监听寻找新设备");
//this.getBluetoothDevices();
uni.onBluetoothDeviceFound(devices => {
console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices();
});
},
/**
* 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/
getBluetoothDevices() {
console.log("获取蓝牙设备");
uni.getBluetoothDevices({
success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg);
this.devicesList = res.devices;
//在这里查找Belter_BT名称的血压仪
for (let i = 0; i < this.devicesList.length; i++) {
let eq = this.devicesList[i];
if (eq.name === "Belter_BT") {
this.deviceId = eq.deviceId;
console.log("查找到了血压仪:设备deviceId:" + this.deviceId);
//在这准备连接设备
this.createBLEConnection();
//停止搜索设备
this.stopBluetoothDevicesDiscovery();
break;
}
}
},
fail: e => {
console.log('获取蓝牙设备错误,错误码:' + e.errCode);
}
});
},
/**
* 连接设备
*/
createBLEConnection() {
//设备deviceId
let deviceId = this.deviceId;
let self = this;
uni.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success: res => {
console.log("设备连接成功!");
//延迟1.5s获取设备的services
setTimeout(function() {
console.log("获取设备的services");
self.getBLEDeviceServices();
}, 1500);
},
fail: res => {
console.log(JSON.stringify(res));
console.log("设备连接失败!");
}
});
},
/**
* 获取设备的服务ID
*/
getBLEDeviceServices() {
let deviceId = this.deviceId;
let serviceList = [];
let self = this;
uni.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success: res => {
console.log(JSON.stringify(res));
serviceList = res.services;
for (let i = 0; i < serviceList.length; i++) {
let service = serviceList[i];
console.log(JSON.stringify(service) + "----serviceID:" + service.uuid);
//比对service是否是FFF0服务
if (service.uuid.indexOf(self.primaryUUID) != -1) {
self.serviceId = service.uuid;
console.log("设备的serviceId: " + self.serviceId);
//开始获取指定服务的特征值
self.getBLEDeviceCharacteristics();
break;
}
}
},
fail: res => {
console.log('device services:', res.services)
}
});
},
/**
* 获取指定服务的特征值
*/
getBLEDeviceCharacteristics() {
let deviceId = this.deviceId;
let serviceId = this.serviceId;
let characteristicsList = [];
let self = this;
uni.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
success: res => {
console.log(JSON.stringify(res));
console.log("获取的" + serviceId + "服务的特征值:" + JSON.stringify(res.characteristics));
characteristicsList = res.characteristics;
for (let i = 0; i < characteristicsList.length; i++) {
let characteristic = characteristicsList[i];
//判断服务特征值中的特征值FFF3 和 FFF4
if (characteristic.uuid.indexOf("FFF3") != -1) {
self.characteristicId[0].writeId = characteristic.uuid;
console.log("设备的特征值写入ID: " + self.characteristicId[0].writeId);
}
if (characteristic.uuid.indexOf("FFF4") != -1) {
self.characteristicId[0].notifyId = characteristic.uuid;
console.log("设备的特征值notifyID: " + self.characteristicId[0].notifyId);
}
if (self.characteristicId[0].writeId != "" && self.characteristicId[0].notifyId != "") {
break;
}
}
},
fail: res => {
console.log('device getBLEDeviceCharacteristics failed:', JSON.stringify(res))
}
})
},
/**
* 开启订阅特征值
*/
notifyBLECharacteristicValue() {
let deviceId = this.deviceId;
let serviceId = this.serviceId;
let characteristicId = this.characteristicId[0].notifyId;
let notify = true;
let self = this;
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success(res) {
console.log('notifyBLECharacteristicValueChange success:' + JSON.stringify(res));
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
uni.onBLECharacteristicValueChange(function(res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${JSON.stringify(res.value)}`)
var value = ab2hex(res.value);
console.log(value);
if (value.length === 14){
let result = parseInt(value.slice(10, 12), 16);
console.log("动态压力值:" + result);
self.measureResult[0].pressure = result;
}
if (value.length === 34){
let sys = parseInt(value.slice(10, 14), 16);
let dia = parseInt(value.slice(14, 18), 16);
let pul = parseInt(value.slice(22, 26), 16);
console.log("高压值 SYS:" + sys);
console.log("低压值 DIA:" + dia);
console.log("脉搏值 PUL:" + pul);
self.measureResult[0].SYS = sys;
self.measureResult[0].DIA = dia;
self.measureResult[0].PUL = pul;
//停止上传数据
self.writeBLECharacteristicValue(self.wirteControlCode[0].stopUploadData);
}
console.log("----------------------------------------------");
});
},
fail(res) {
console.log('notifyBLECharacteristicValueChange failed:' + res.errMsg);
var value = ab2hex(res.value);
console.log(value);
}
});
},
/**
* 写入控制命令
* writeCode 写入的控制命令
*/
writeBLECharacteristicValue(writeCode) {
let deviceId = this.deviceId;
let serviceId = this.serviceId;
let characteristicId = this.characteristicId[0].writeId;
//因为协议文档中,一个字节两个字符的控制命令,codeLength为命令字节数
let codeLength = writeCode.length / 2;
const buffer = new ArrayBuffer(codeLength);
const dataView = new DataView(buffer)
//在这里解析将要写入的值
for (let i = 0; i < codeLength; i++) {
dataView.setUint8(i, '0X' + writeCode.substring(i * 2, i * 2 + 2));
console.log("次数:" + i + "-----" + writeCode.substring(2 * i, 2 * i + 2));
}
console.log("写入数据中deviceId:" + deviceId);
console.log("写入数据中serviceId:" + serviceId);
console.log("写入数据中characteristicId:" + characteristicId);
console.log("分割线************************************");
console.log("发送的数据:")
for (let i = 0; i < dataView.byteLength; i++) {
console.log("0x" + dataView.getUint8(i).toString(16))
}
uni.writeBLECharacteristicValue({
// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
// 这里的value是ArrayBuffer类型
value: buffer,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
console.log('writeBLECharacteristicValue success', JSON.stringify(res))
//this.notifyCharacteristicValueChange();
},
fail(res) {
console.log("写入数据失败", res.errMsg)
}
});
},
/**
* 监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等
*/
onBLEConnectionStateChange() {
let count = 0;
uni.onBLEConnectionStateChange(res => {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`蓝牙连接状态 -------------------------->`);
console.log(JSON.stringify(res));
if (!res.connected) {
if (this.isStop) return;
console.log('断开低功耗蓝牙成功:');
uni.showToast({
icon: "none",
title: "蓝牙已经断开!",
mask: false,
duration: 3000
});
//在这里尝试重连
//this.createBLEConnection();
//关闭连接
this.closeBluetoothAdapter();
}
});
},
/**
* 断开蓝牙连接
*/
closeBluetoothAdapter() {
uni.closeBluetoothAdapter({
success: res => {
console.log('断开蓝牙模块成功');
uni.showToast({
icon: "none",
title: "蓝牙已经断开!",
mask: false,
duration: 3000
});
}
});
}
}
}
</script>
<style>
.content {}
</style>