|
@@ -6,7 +6,6 @@
|
|
|
// const DeviceManager = require('./DeviceManager');
|
|
|
const { CmdBase, BtCmd, } = require('./../devices/bluetooth/bt_cmd');
|
|
|
const BtParse = require('./../devices/bluetooth/bt_parse');
|
|
|
-const ble = require('./ble_manager');
|
|
|
// const VolumeUtil = require('./VolumeUtil');
|
|
|
// const EventManager = require('./EventManager');
|
|
|
// const CmdBase = require('./../devices/bluetooth/bt_cmd');
|
|
@@ -41,6 +40,7 @@ class BtHelper {
|
|
|
|
|
|
constructor() {
|
|
|
this.timer = null;
|
|
|
+ this.bleManager = new bleManager();
|
|
|
// if (DeviceUtil.isAndroid) {
|
|
|
// this._helper = BtAndroidHelper.instance;
|
|
|
// } else {
|
|
@@ -64,7 +64,7 @@ class BtHelper {
|
|
|
}
|
|
|
|
|
|
async search(stopCall) {
|
|
|
- let res = await bleManager.startScan()
|
|
|
+ let res = await this.bleManager.startScan()
|
|
|
this.timer = null;
|
|
|
this.timer = setTimeout(() => {
|
|
|
this.stopSearch();
|
|
@@ -77,14 +77,14 @@ class BtHelper {
|
|
|
}
|
|
|
|
|
|
async findDevices(callback) {
|
|
|
- bleManager.onBluetoothDeviceFound(callback)
|
|
|
+ this.bleManager.onBluetoothDeviceFound(callback)
|
|
|
// return await this._helper.search();
|
|
|
}
|
|
|
|
|
|
async stopSearch() {
|
|
|
clearTimeout(this.timer);
|
|
|
this.timer = null;
|
|
|
- await bleManager.stopScan();
|
|
|
+ await this.bleManager.stopScan();
|
|
|
}
|
|
|
|
|
|
async _connectSuccess() {
|
|
@@ -97,8 +97,8 @@ class BtHelper {
|
|
|
try {
|
|
|
clearTimeout(this.timer);
|
|
|
this.timer = null;
|
|
|
- bleManager.stopScan()
|
|
|
- var res = await bleManager.connectToDevice(data);
|
|
|
+ this.bleManager.stopScan()
|
|
|
+ var res = await this.bleManager.connectToDevice(data);
|
|
|
console.log('连接成功');
|
|
|
// this.setData({ connectedDeviceId: deviceId });
|
|
|
if (res === false) {
|
|
@@ -109,7 +109,7 @@ class BtHelper {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
- const serviceId = await bleManager.discoverServices(data.deviceId);
|
|
|
+ const serviceId = await this.bleManager.discoverServices(data.deviceId);
|
|
|
if (serviceId == "") {
|
|
|
console.log("连接失败")
|
|
|
this.disconnect(data)
|
|
@@ -121,7 +121,7 @@ class BtHelper {
|
|
|
// this.setData({ services });
|
|
|
console.log("服务ID:" + serviceId)
|
|
|
|
|
|
- var characteristics = await bleManager.discoverCharacteristics(data.deviceId, serviceId)
|
|
|
+ var characteristics = await this.bleManager.discoverCharacteristics(data.deviceId, serviceId)
|
|
|
if (characteristics == "") {
|
|
|
console.log("连接失败")
|
|
|
this.disconnect(data)
|
|
@@ -135,14 +135,14 @@ class BtHelper {
|
|
|
let charc = characteristics[i];
|
|
|
if (charc.properties.notify) {
|
|
|
// 订阅的
|
|
|
- bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
|
|
|
+ this.bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
|
|
|
// console.log('收到数据:', BtParse.parseTLV);
|
|
|
BtParse.parseTLV(res);
|
|
|
})
|
|
|
}
|
|
|
if (charc.properties.write || charc.properties.writeWithoutResponse) {
|
|
|
// 写入的
|
|
|
- bleManager.setWrite(charc, charc.uuid)
|
|
|
+ this.bleManager.setWrite(charc, charc.uuid)
|
|
|
|
|
|
setTimeout(() => {
|
|
|
this._connectSuccess()
|
|
@@ -153,7 +153,7 @@ class BtHelper {
|
|
|
}
|
|
|
}
|
|
|
if (charc.properties.read) {
|
|
|
- var chara = await bleManager.readCharacteristicValue(charc.uuid,)
|
|
|
+ var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -166,7 +166,7 @@ class BtHelper {
|
|
|
}
|
|
|
|
|
|
async disconnect(data) {
|
|
|
- bleManager.disconnect(data.deviceId)
|
|
|
+ this.bleManager.disconnect(data.deviceId)
|
|
|
}
|
|
|
|
|
|
async dispose() {
|
|
@@ -176,7 +176,7 @@ class BtHelper {
|
|
|
async send(cmd, type) {
|
|
|
if (cmd) {
|
|
|
// this.queueManager.addTask(cmd)
|
|
|
- bleManager.sendData(cmd)
|
|
|
+ this.bleManager.sendData(cmd)
|
|
|
}
|
|
|
// QueueManager.instance.addTask({ task: cmd });
|
|
|
}
|
|
@@ -187,7 +187,7 @@ class BtHelper {
|
|
|
// }
|
|
|
|
|
|
async initBluetooth(callback) {
|
|
|
- let adpter = await bleManager.initBluetoothAdapter((res) => {
|
|
|
+ let adpter = await this.bleManager.initBluetoothAdapter((res) => {
|
|
|
if (!res) {
|
|
|
wx.showToast({
|
|
|
title: '请开启蓝牙功能',
|
|
@@ -202,7 +202,7 @@ class BtHelper {
|
|
|
// console.log(res)
|
|
|
|
|
|
|
|
|
- bleManager.checkBluetoothPermission(function (per) {
|
|
|
+ this.bleManager.checkBluetoothPermission(function (per) {
|
|
|
if (!per) {
|
|
|
wx.showToast({
|
|
|
title: '没有蓝牙权限',
|
|
@@ -216,17 +216,17 @@ class BtHelper {
|
|
|
}
|
|
|
|
|
|
closeBle() {
|
|
|
- bleManager.closeBle()
|
|
|
+ this.bleManager.closeBle()
|
|
|
}
|
|
|
|
|
|
async getConnectedDevices() {
|
|
|
try {
|
|
|
- const connectedDevices = await bleManager.getConnectedDevices();
|
|
|
+ const connectedDevices = await this.bleManager.getConnectedDevices();
|
|
|
console.log("全部设备1:", connectedDevices)
|
|
|
if (connectedDevices.length) {
|
|
|
// todo 已经连接上的设备
|
|
|
}
|
|
|
- const allDevices = await bleManager.getAllConnectedDevices()
|
|
|
+ const allDevices = await this.bleManager.getAllConnectedDevices()
|
|
|
console.log("全部设备2:", allDevices)
|
|
|
let newDevices = connectedDevices.concat(allDevices);
|
|
|
return newDevices
|