流程:
1、 onload中初始化蓝牙设备uni.openBluetoothAdapter,调用成功后获取本机蓝牙适配器状态uni.getBluetoothAdapterState
2、 点击事件,开始搜索蓝牙设备uni.startBluetoothDevicesDiscovery,异步处理定位失败问题并停止搜索蓝牙设备 uni.stopBluetoothDevicesDiscovery
3、开始搜寻附近的蓝牙外围设备uni.startBluetoothDevicesDiscovery
4、监听寻找到新设备的事件uni.onBluetoothDeviceFound
5、获取在蓝牙模块生效期间所有已发现的蓝牙设备uni.getBluetoothDevices
6、对获取到的所有设备信息for循环筛选对应的设备
实际使用:
初始化蓝牙设备和获取本机蓝牙适配器状态
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
console.log(JSON.stringify(e));
this.getBluetoothAdapterState();
},
fail: e => {
console.log(e)
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
uni.showModal({
title: '提示!',
content: '初始化蓝牙失败,请打开本机蓝牙!',
showCancel: false,
confirmText: "确定",
success: (res) => {
if (res.confirm) {
}
}
});
if (e.errCode !== 0) {
//initTypes(e.errCode, e.errMsg);
}
}
});
},
getBluetoothAdapterState() {
uni.getBluetoothAdapterState({
success: res => {
console.log(JSON.stringify(res));
},
fail: e => {
console.log('获取本机蓝牙适配器状态失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
//initTypes(e.errCode);
}
}
});
},
点击事件搜索蓝牙设备
uni.startBluetoothDevicesDiscovery({
success(res2) {
uni.showLoading({
title: "正在搜索设备",
icon:"none",
duration: 2000
});
console.log('搜索蓝牙外围设备完成', res2)
}
});
搜寻附近的蓝牙设备
uni.startBluetoothDevicesDiscovery({
success(res2) {
uni.showLoading({
title: "正在搜索设备",
icon:"none",
duration: 2000
});
console.log('搜索蓝牙外围设备完成', res2)
}
});
监听寻找到新设备的事件
onBluetoothDeviceFound(needlocation) {
uni.onBluetoothDeviceFound(devices => {
console.log('开始监听寻找到新设备的事件');
console.log(devices)
this.getBluetoothDevices(needlocation);
});
},
获取蓝牙模块生效期间所有已发现的设备
getBluetoothDevices(needlocation) {
uni.getBluetoothDevices({
success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg);
let devices = res.devices;
this.dogBluePaisCheck(devices, needlocation);
},
fail: e => {
console.log('获取蓝牙设备错误,错误码:' + e.errCode);
if (e.errCode !== 0) {
}
}
});
},
搜寻到该设备时停止搜索
uni.stopBluetoothDevicesDiscovery({
success(res4) {
console.log('res4',res4)
}
})
链接蓝牙
uni.createBLEConnection({
deviceId: deviceId, // 设备id
success() {
console.log('蓝牙连接成功后关闭蓝牙搜索并获取服务id')
// 获取服务id
}
})
链接成功后获取服务id
uni.getBLEDeviceServices({
deviceId: deviceId,
success(res5) {
console.log("获取服务Id", res5)
// serviceId固定,获取蓝牙设备某个服务中的所有特征值【读写属性】,如果有多个服务可以都尝试下
if (res5.services && res5.services.length > 0) {
let serviceId = res5.services[0].uuid;
console.log('serviceId', serviceId);
}
}
})
调用蓝牙的监听和写入功能
uni.getBLEDeviceCharacteristics({
deviceId: deviceId, // 蓝牙设备id
serviceId: serviceId, // 蓝牙服务UUID
success(res6) {
console.log('数据监听', res6);
res6.characteristics.forEach(item => {
// 003
if (item.properties.notify === true) {
// 监听
let notifyId = item.uuid;
}
// 002
if (item.properties.write === true) {
// 写入
let writeId = item.uuid;
}
}
}
})
监听设备返回数据
uni.notifyBLECharacteristicValueChange({
characteristicId: notify,
deviceId: deviceId,
serviceId: serviceId,
state: true,
success(res) {
// 监听低功耗蓝牙设备的特征值变化
uni.onBLECharacteristicValueChange(result => {
console.log("监听低功耗蓝牙设备的特征值变化", result);
if (result.value) {
let resHex = this.ab2hex(result.value)
console.log('设备返回数据', resHex)
}
})
}
});
向设备发送数据
uni.writeBLECharacteristicValue({
characteristicId: writeId,
deviceId: deviceId,
serviceId: serviceId,
value: buffer, // 具体数据
success(res) {
console.log("writeBLECharacteristicValue success", res);
},
fail(err) {
console.log("报错了", err);
}
});
做数据处理
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}