|
@@ -10,7 +10,7 @@ const BtParse = require('./../devices/bluetooth/bt_parse');
|
|
// const EventManager = require('./EventManager');
|
|
// const EventManager = require('./EventManager');
|
|
// const CmdBase = require('./../devices/bluetooth/bt_cmd');
|
|
// const CmdBase = require('./../devices/bluetooth/bt_cmd');
|
|
const bleManager = require('./ble_manager');
|
|
const bleManager = require('./ble_manager');
|
|
-// const QueueManager = require('./QueueManager');
|
|
|
|
|
|
+const QueueManager = require('./QueueManager');
|
|
|
|
|
|
// const EnumConnectStatus = require('./EnumConnectStatus');
|
|
// const EnumConnectStatus = require('./EnumConnectStatus');
|
|
// const EnumOpen = require('./EnumOpen');
|
|
// const EnumOpen = require('./EnumOpen');
|
|
@@ -52,10 +52,12 @@ class BtHelper {
|
|
// this._helper.send({ cmd: task });
|
|
// this._helper.send({ cmd: task });
|
|
// }
|
|
// }
|
|
// });
|
|
// });
|
|
- // this.queueManager = new QueueManager();
|
|
|
|
- // this.queueManager.listenTask((task) => {
|
|
|
|
- // bleManager.sendData(task);
|
|
|
|
- // });
|
|
|
|
|
|
+ this.queueManager = new QueueManager();
|
|
|
|
+ let _this = this
|
|
|
|
+ this.queueManager.clear();
|
|
|
|
+ this.queueManager.listenTask((task) => {
|
|
|
|
+ _this.bleManager.sendData(task);
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -90,79 +92,83 @@ class BtHelper {
|
|
async _connectSuccess() {
|
|
async _connectSuccess() {
|
|
this.checkDevice()
|
|
this.checkDevice()
|
|
this.getDeviceInfo()
|
|
this.getDeviceInfo()
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
async connect(data, onChanged) {
|
|
async connect(data, onChanged) {
|
|
// await this._helper.connect({ data, onChanged, isClick });
|
|
// await this._helper.connect({ data, onChanged, isClick });
|
|
- try {
|
|
|
|
- clearTimeout(this.timer);
|
|
|
|
- this.timer = null;
|
|
|
|
- this.bleManager.stopScan()
|
|
|
|
- var res = await this.bleManager.connectToDevice(data);
|
|
|
|
- console.log(res ? '连接成功' : '连接失败');
|
|
|
|
- // this.setData({ connectedDeviceId: deviceId });
|
|
|
|
- if (res === false) {
|
|
|
|
- console.log("连接失败")
|
|
|
|
- this.disconnect(data)
|
|
|
|
- if (onChanged) {
|
|
|
|
- onChanged(false)
|
|
|
|
- }
|
|
|
|
- return
|
|
|
|
|
|
+
|
|
|
|
+ clearTimeout(this.timer);
|
|
|
|
+ this.timer = null;
|
|
|
|
+ this.bleManager.stopScan()
|
|
|
|
+ var res = await this.bleManager.connectToDevice(data);
|
|
|
|
+ console.log(res ? '连接成功' : '连接失败');
|
|
|
|
+ // this.setData({ connectedDeviceId: deviceId });
|
|
|
|
+ if (res === false) {
|
|
|
|
+ console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
|
|
+ if (onChanged) {
|
|
|
|
+ onChanged(false)
|
|
}
|
|
}
|
|
- const serviceId = await this.bleManager.discoverServices(data.deviceId);
|
|
|
|
- if (serviceId == "") {
|
|
|
|
- console.log("连接失败")
|
|
|
|
- this.disconnect(data)
|
|
|
|
- if (onChanged) {
|
|
|
|
- onChanged(false)
|
|
|
|
- }
|
|
|
|
- return
|
|
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ const serviceId = await this.bleManager.discoverServices(data.deviceId);
|
|
|
|
+ if (serviceId == "") {
|
|
|
|
+ console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
|
|
+ if (onChanged) {
|
|
|
|
+ onChanged(false)
|
|
}
|
|
}
|
|
- // this.setData({ services });
|
|
|
|
- console.log("服务ID:" + serviceId)
|
|
|
|
-
|
|
|
|
- var characteristics = await this.bleManager.discoverCharacteristics(data.deviceId, serviceId)
|
|
|
|
- if (characteristics == "") {
|
|
|
|
- console.log("连接失败")
|
|
|
|
- this.disconnect(data)
|
|
|
|
- if (onChanged) {
|
|
|
|
- onChanged(false)
|
|
|
|
- }
|
|
|
|
- return
|
|
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ // this.setData({ services });
|
|
|
|
+ console.log("服务ID:" + serviceId)
|
|
|
|
+
|
|
|
|
+ var characteristics = await this.bleManager.discoverCharacteristics(data.deviceId, serviceId)
|
|
|
|
+ if (characteristics == "") {
|
|
|
|
+ console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
|
|
+ if (onChanged) {
|
|
|
|
+ onChanged(false)
|
|
}
|
|
}
|
|
- console.log('device特征值:', characteristics)
|
|
|
|
- for (let i = 0; i < characteristics.length; i++) {
|
|
|
|
- let charc = characteristics[i];
|
|
|
|
- if (charc.properties.notify) {
|
|
|
|
- console.log('订阅数据:');
|
|
|
|
- // 订阅的
|
|
|
|
- this.bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
|
|
|
|
- BtParse.parseTLV(res);
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- if (charc.properties.write || charc.properties.writeWithoutResponse) {
|
|
|
|
- // 写入的
|
|
|
|
- this.bleManager.setWrite(charc, charc.uuid)
|
|
|
|
-
|
|
|
|
- setTimeout(() => {
|
|
|
|
- this._connectSuccess()
|
|
|
|
- }, 1000);
|
|
|
|
-
|
|
|
|
- if (onChanged) {
|
|
|
|
- onChanged(true)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (charc.properties.read) {
|
|
|
|
- var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
|
|
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ console.log('device特征值:', characteristics)
|
|
|
|
+ // let uuids = ["ab00", "ffc0"];
|
|
|
|
+ for (let i = 0; i < characteristics.length; i++) {
|
|
|
|
+ let charc = characteristics[i];
|
|
|
|
+ // if (!uuids.includes(charc.uuid)) {
|
|
|
|
+ // if (onChanged) {
|
|
|
|
+ // console.log('没有特征值:', charc.uuid);
|
|
|
|
+ // onChanged(false)
|
|
|
|
+ // }
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
+ if (charc.properties.notify) {
|
|
|
|
+ console.log('订阅数据:', charc.uuid);
|
|
|
|
+ // 订阅的
|
|
|
|
+ this.bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
|
|
|
|
+ BtParse.parseTLV(res);
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ if (charc.properties.write || charc.properties.writeWithoutResponse) {
|
|
|
|
+ // 写入的
|
|
|
|
+ this.bleManager.setWrite(charc, charc.uuid)
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ this._connectSuccess()
|
|
|
|
+ }, 500);
|
|
|
|
+ if (onChanged) {
|
|
|
|
+ onChanged(true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ // if (charc.properties.read) {
|
|
|
|
+ // var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
|
|
|
|
+ // }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
- // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
|
|
|
|
|
|
+ // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
|
|
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error(error);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
async disconnect(data) {
|
|
async disconnect(data) {
|
|
@@ -173,11 +179,15 @@ class BtHelper {
|
|
// await this._helper.dispose();
|
|
// await this._helper.dispose();
|
|
}
|
|
}
|
|
|
|
|
|
- async send(cmd, type) {
|
|
|
|
|
|
+ async sendNow(cmd) {
|
|
if (cmd) {
|
|
if (cmd) {
|
|
- // this.queueManager.addTask(cmd)
|
|
|
|
this.bleManager.sendData(cmd)
|
|
this.bleManager.sendData(cmd)
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+ async send(cmd, type) {
|
|
|
|
+ if (cmd) {
|
|
|
|
+ this.queueManager.addTask(cmd)
|
|
|
|
+ }
|
|
// QueueManager.instance.addTask({ task: cmd });
|
|
// QueueManager.instance.addTask({ task: cmd });
|
|
}
|
|
}
|
|
|
|
|
|
@@ -238,7 +248,7 @@ class BtHelper {
|
|
|
|
|
|
async checkDevice() {
|
|
async checkDevice() {
|
|
// console.log("校验设备:", BtCmd); // 输出: EarPhone Info
|
|
// console.log("校验设备:", BtCmd); // 输出: EarPhone Info
|
|
- this.send(BtCmd.checkDevice());
|
|
|
|
|
|
+ this.sendNow(BtCmd.checkDevice());
|
|
//2.0有发这个,不知道是啥
|
|
//2.0有发这个,不知道是啥
|
|
this.send([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]);
|
|
this.send([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]);
|
|
}
|
|
}
|