1、打开蓝牙适配器
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: res => {
console.log('openBluetoothAdapter success', res);
this.startBluetoothDevicesDiscovery();
},
fail: res => {
if (res.errCode === 10001) {
uni.showToast({
title: '请打开蓝牙',
icon: 'none'
});
uni.onBluetoothAdapterStateChange(res => {
console.log('onBluetoothAdapterStateChange', res);
if (res.available) {
this.startBluetoothDevicesDiscovery();
}
});
}
}
});
},
2、开启蓝牙搜索
startBluetoothDevicesDiscovery() {
if (this.discoveryStarted) {
return;
}
this.discoveryStarted = true;
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: res => {
console.log('startBluetoothDevicesDiscovery success', res);
this.onBluetoothDeviceFound();
}
});
},
3、监听蓝牙设备列表
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (device.name.indexOf('蓝牙名称') == -1) {
return;
}
this.createBLEConnection(device);
});
});
},
4、建立连接
createBLEConnection(device) {
const deviceId = device.deviceId;
uni.createBLEConnection({
deviceId,
success: res => {
//隐藏加载框
this.connect = true;
this.restart();
this.deviceId = deviceId;
this.getBLEDeviceServices(deviceId);
}
});
this.stopBluetoothDevicesDiscovery();
},
5、关闭蓝牙连接
closeBLEConnection() {
uni.closeBLEConnection({
deviceId: this.deviceId
});
this.connect = false;
},
6、停止蓝牙搜搜
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery();
this.discoveryStarted = false;
},
7、获取蓝牙设备服务文章来源:https://www.toymoban.com/news/detail-544851.html
getBLEDeviceServices(deviceId) {
uni.getBLEDeviceServices({
deviceId,
success: res => {
console.log(res);
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
return;
}
}
}
});
},
8、获取蓝牙特征值文章来源地址https://www.toymoban.com/news/detail-544851.html
getBLEDeviceCharacteristics(deviceId, serviceId) {
uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: res => {
console.log('getBLEDeviceCharacteristics success', res.characteristics);
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i];
console.log(item.properties)
if (item.properties.read) {
uni.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid
});
}
if (item.properties.write) {
this.deviceId = deviceId;
this.serviceId = serviceId;
this.characteristicId = item.uuid;
}
if (item.properties.notify || item.properties.indicate) {
uni.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true
});
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res);
}
});
uni.onBLECharacteristicValueChange(characteristic => {
if(!this.connect){
return
}
//自己的操作
let value = util.ab2hex(characteristic.value);
});
uni.onBLEConnectionStateChange(res => {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
this.connect = res.connected;
if (!this.connect) {
this.discoveryStarted = false;
//this.finishStatus = true;
clearInterval(this.interval);
this.openBluetoothAdapter();
}
});
},
到了这里,关于uni-app蓝牙操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!