概述
- 蓝牙连接包括搜索蓝牙设备,选择蓝牙设备,监听设备特征值,发送命令,断开蓝牙连接5种基础方法。
- Uni-App BLE 文档地址
- 搜索设备时
搜索蓝牙设备
function discoveryDevices(pushDevice){
console.log('enter search ble bluetooth func');
uni.openBluetoothAdapter({
success(res) {
if (res && res.errMsg && res.errMsg === 'openBluetoothAdapter:ok') {
startScanDevice();
startListenDeviceFound(pushDevice);
}
},
fail(res) {
console.log("uni.openBluetoothAdapter fail:", res);
switch (res.errCode) {
case 10001:
console.log("uni.openBluetoothAdapter faile:当前蓝牙适配器不可用");break;
default:
}
}
})
}
function startScanDevice() {
uni.startBluetoothDevicesDiscovery({
success(res) {
},
fail(res) {
console.log("uni.startBluetoothDevicesDiscovery fail:", res);
}
})
}
function startListenDeviceFound(pushDevice) {
uni.onBluetoothDeviceFound(function (res) {
let devices = res.devices.map(item => {
return {
name: item.localName || item.name,
value: item.deviceId
}
}).filter(item => {return item.name});
devices.forEach(item => {
let vaildArray = deviceListXianXY.filter(item2 => {
return item2.value === item.value;
})
if (vaildArray.length === 0) {
deviceListXianXY.push(item);
pushDevice(item);
}
});
})
}
选择蓝牙设备
function connDevice(deviceId){
console.log("choosed deviceId: ", deviceId);
uni.createBLEConnection({
deviceId,
success(res) {
setTimeout(() => {
getDeviceService(deviceId, () => {
startNotify(deviceId);
})
}, 1000)
stopScanDevice();
},
fail(res) {
console.log("uni.createBLEConnection fail:", res);
}
})
}
function stopScanDevice(){
uni.stopBluetoothDevicesDiscovery({
success(res) {
console.log("uni.stopBluetoothDevicesDiscovery success:", res);
}
})
}
function getDeviceService(deviceId, callback) {
uni.getBLEDeviceServices({
deviceId,
success(res) {
setTimeout(() => {
getDeviceServcieCharacteristics(deviceId, callback);
}, 20);
},
fail(res) {
console.log("uni.getBLEDeviceServices fail:", res);
}
})
}
function getDeviceServcieCharacteristics(deviceId, callback) {
uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success(res) {
if (callback) {
callback();
}
},
fail(res) {
console.log("uni.getDeviceServcieCharacteristics fail:", res);
}
})
}
function startNotify(deviceId) {
console.log("enter startNotify");
uni.notifyBLECharacteristicValueChange({
state: true,
deviceId,
serviceId,
characteristicId: characteristicIdNotify,
success(res) {
},
fail(res) {
console.log("uni.notifyBLECharacteristicValueChange fail:", res);
}
})
}
发送命令
function writeCommand(deviceId,command,ck,failfun){
startListenble(function (buf) {
ck(buf)
});
console.log("command===",command,"长度",command.length);
const subCommandArray = splitArr(command, 20);
sendCommand(deviceId, serviceId, characteristicId2, subCommandArray, 0);
}
function sendCommand(deviceId, serviceId, characteristicId2, commands, index, callback) {
uni.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: characteristicId2,
value: commands[index],
success(res) {
if (index >= commands.length - 1) {
if (callback) {
callback();
}
} else {
sendCommand(deviceId, serviceId, characteristicId2, commands, ++index, callback);
}
},
fail(res) {
console.log('uni.writeBLECharacteristicValue fail', res.errMsg);
}
})
}
function startListenble(callback){
let resultBuf = "";
uni.onBLECharacteristicValueChange(function (res) {
const buf = buf2hex(res.value)
console.log("buf", buf);
resultBuf += buf;
if (resultBuf.endsWith('e9')) {
console.log("响应帧",resultBuf);
callback(resultBuf);
}
})
}
function splitArr(ar, size = 1) {
let index = 0;
let res = [];
while (index < ar.length) {
res.push(ar.slice(index, (index+size)));
index += size;
}
return res;
}
function buf2hex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
关闭连接
function closeConn(deviceId){
uni.closeBLEConnection({
deviceId,
success(res) {
console.log('uni.closeBLEConnection success', res);
},
fail(res) {
console.log('uni.closeBLEConnection fail', res);
}
})
}