BLETool.js
import VueFunc from "./vue-func.js"
import Constant from './constant.js'
export default {
scanDeviceList: [],
storageDeviceList: [],
crtName: null,
conState: -1,
checkTimer: null,
init() {
this.openBluetoothAdapter(() => {
this.startBluetoothDevicesDiscovery();
});
uni.onBluetoothDeviceFound(res => {
this.putScanDevice(res.devices[0]);
});
this.storageDeviceList = uni.getStorageSync('storageDeviceList');
if (!this.storageDeviceList) this.storageDeviceList = [];
this.conState = 0;
this.checkTimer = setInterval(() => {
if (this.conState == 0) {
this.createBLEConnection(this.crtName);
}
}, 60000);
uni.onBLEConnectionStateChange(res => {
if (!res.connected) {
this.conState = 0;
}
});
uni.onBLEConnectionStateChange(res => {
})
},
getDeviceByName(name) {
for (var item of this.storageDeviceList) {
if (item.name == name) {
return item;
}
}
for (var item of this.scanDeviceList) {
if (item.name == name) {
return item;
}
}
return null;
},
getDeviceById(deviceId) {
for (var item of this.storageDeviceList) {
if (item.deviceId == deviceId) {
return item;
}
}
for (var item of this.scanDeviceList) {
if (item.deviceId == deviceId) {
return item;
}
}
return null;
},
putScanDevice(device) {
this.addDevice(device, this.scanDeviceList);
},
putStorageDevice(device) {
if (!this.addDevice(device, this.storageDeviceList)) {
uni.setStorageSync('storageDeviceList', this.storageDeviceList);
}
},
addDevice(device, deviceList) {
var isContain = false;
for (var item of deviceList) {
if (item.deviceId == device.deviceId) {
isContain = true;
item.RSSI = device.RSSI;
break;
}
}
if (!isContain) {
deviceList.push(device);
}
return isContain;
},
openBluetoothAdapter(successCallBack) {
uni.openBluetoothAdapter({
success(res) {
console.log(res);
if (successCallBack != null &&
successCallBack != undefined) successCallBack();
},
fail() {
uni.showToast({
title: "蓝牙启动失败,请确认蓝牙已打开",
icon: "none",
duration: 2000
});
}
});
},
startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
success: res => {
console.log(res)
setTimeout(() => {
this.stopBluetoothDevicesDiscovery();
}, 20000);
}
})
},
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log(res)
}
})
},
createByName(name) {
this.crtName = name;
var device = this.getDeviceByName(name);
if (device != null) {
this.conState = 1;
uni.createBLEConnection({
deviceId: device.deviceId,
success: res => {
if (res.errMsg == 'createBLEConnection:ok') {
uni.setBLEMTU({
deviceId: deviceUUID,
mtu: 512
})
console.log("连接成功");
this.putStorageDevice(device);
this.conState = 2;
setTimeout(() => {
this.getBLEDeviceServices(device);
}, 1000);
} else {
this.conState = 0;
}
},
fail: () => {
this.conState = 0;
}
})
} else {
this.startBluetoothDevicesDiscovery();
}
},
createByDeviceId(deviceId) {
var device = this.getDeviceById(name);
if (device != null) {
this.conState = 1;
uni.createBLEConnection({
deviceId: device.deviceId,
success: res => {
if (res.errMsg == 'createBLEConnection:ok') {
uni.setBLEMTU({
deviceId: deviceUUID,
mtu: 512
})
console.log("连接成功");
this.putStorageDevice(device);
this.conState = 2;
setTimeout(() => {
this.getBLEDeviceServices(device);
}, 1000);
} else {
this.conState = 0;
}
},
fail: () => {
this.conState = 0;
}
})
} else {
this.startBluetoothDevicesDiscovery();
}
},
getBLEDeviceServices(deviceId) {
uni.getBLEDeviceServices({
deviceId: deviceId,
success: res => {
console.log('device getBLEDeviceCharacteristics:', res
.characteristics)
setTimeout(() => {
this.getBLEDeviceCharacteristics(deviceId);
}, 1000);
}
});
},
getBLEDeviceCharacteristics(deviceId) {
uni.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: Constant.COM_SERVICE_UUID,
success: res => {
console.log('device getBLEDeviceCharacteristics:', res
.characteristics)
setTimeout(() => {
this.notifyBLECharacteristicValueChange(deviceId);
}, 1000);
}
});
},
notifyBLECharacteristicValueChange(deviceId) {
uni.notifyBLECharacteristicValueChange({
state: true,
deviceId: deviceId,
serviceId: Constant.COM_SERVICE_UUID,
characteristicId: Constant.RX_CHAR_UUID,
success: res => {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
});
},
}
|