|
@@ -4,10 +4,12 @@
|
|
// const BtIOSHelper = require('./BtIOSHelper');
|
|
// const BtIOSHelper = require('./BtIOSHelper');
|
|
// const QueueManager = require('./QueueManager');
|
|
// const QueueManager = require('./QueueManager');
|
|
// const DeviceManager = require('./DeviceManager');
|
|
// const DeviceManager = require('./DeviceManager');
|
|
-const BtCmd = require('./../devices/bluetooth/bt_cmd');
|
|
|
|
|
|
+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 VolumeUtil = require('./VolumeUtil');
|
|
// 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 EnumConnectStatus = require('./EnumConnectStatus');
|
|
// const EnumConnectStatus = require('./EnumConnectStatus');
|
|
// const EnumOpen = require('./EnumOpen');
|
|
// const EnumOpen = require('./EnumOpen');
|
|
@@ -47,6 +49,7 @@ class BtHelper {
|
|
// _helper;
|
|
// _helper;
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
|
+ this.timer = null;
|
|
// if (DeviceUtil.isAndroid) {
|
|
// if (DeviceUtil.isAndroid) {
|
|
// this._helper = BtAndroidHelper.instance;
|
|
// this._helper = BtAndroidHelper.instance;
|
|
// } else {
|
|
// } else {
|
|
@@ -67,7 +70,8 @@ class BtHelper {
|
|
|
|
|
|
async search(mqttFilterList) {
|
|
async search(mqttFilterList) {
|
|
let res = await bleManager.startScan()
|
|
let res = await bleManager.startScan()
|
|
- setTimeout(() => {
|
|
|
|
|
|
+ this.timer = null;
|
|
|
|
+ this.timer = setTimeout(() => {
|
|
bleManager.stopScan();
|
|
bleManager.stopScan();
|
|
}, 10000);
|
|
}, 10000);
|
|
// console.log(res);
|
|
// console.log(res);
|
|
@@ -83,58 +87,68 @@ class BtHelper {
|
|
bleManager.stopScan();
|
|
bleManager.stopScan();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ async _connectSuccess() {
|
|
|
|
+ await btHelper.checkDevice()
|
|
|
|
+ await btHelper.getDeviceInfo()
|
|
|
|
+ }
|
|
|
|
+
|
|
async connect(data, onChanged) {
|
|
async connect(data, onChanged) {
|
|
// await this._helper.connect({ data, onChanged, isClick });
|
|
// await this._helper.connect({ data, onChanged, isClick });
|
|
try {
|
|
try {
|
|
|
|
+ clearTimeout(this.timer);
|
|
|
|
+ this.timer = null;
|
|
|
|
+ bleManager.stopScan()
|
|
var res = await bleManager.connectToDevice(data);
|
|
var res = await bleManager.connectToDevice(data);
|
|
console.log('连接成功');
|
|
console.log('连接成功');
|
|
// this.setData({ connectedDeviceId: deviceId });
|
|
// this.setData({ connectedDeviceId: deviceId });
|
|
if (res === false) {
|
|
if (res === false) {
|
|
console.log("连接失败")
|
|
console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
const serviceId = await bleManager.discoverServices(data.deviceId);
|
|
const serviceId = await bleManager.discoverServices(data.deviceId);
|
|
-
|
|
|
|
|
|
+ if (serviceId == "") {
|
|
|
|
+ console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
// this.setData({ services });
|
|
// this.setData({ services });
|
|
- console.log("服务ID", serviceId)
|
|
|
|
-
|
|
|
|
- var characteristics = await bleManager.discoverCharacteristics({
|
|
|
|
- deviceId: data.deviceId,
|
|
|
|
- serviceId: serviceId,
|
|
|
|
- })
|
|
|
|
|
|
+ console.log("服务ID:" + serviceId)
|
|
|
|
|
|
- console.log('device特征值:', res.characteristics)
|
|
|
|
- for (let i = 0; i < res.characteristics.length; i++) {
|
|
|
|
- let charc = res.characteristics[i];
|
|
|
|
|
|
+ var characteristics = await bleManager.discoverCharacteristics(data.deviceId, serviceId)
|
|
|
|
+ if (characteristics == "") {
|
|
|
|
+ console.log("连接失败")
|
|
|
|
+ this.disconnect(data)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ console.log('device特征值:', characteristics)
|
|
|
|
+ for (let i = 0; i < characteristics.length; i++) {
|
|
|
|
+ let charc = characteristics[i];
|
|
if (charc.properties.notify) {
|
|
if (charc.properties.notify) {
|
|
// 订阅的
|
|
// 订阅的
|
|
- bleManager.notifyCharacteristicValueChange(data.deviceId, service.serviceId, characteristics, true, (res) => {
|
|
|
|
- // console.log(res)
|
|
|
|
|
|
+ bleManager.notifyCharacteristicValueChange(charc.uuid, (res) => {
|
|
|
|
+ // console.log('收到数据:', BtParse.parseTLV);
|
|
|
|
+ BtParse.parseTLV(res);
|
|
})
|
|
})
|
|
}
|
|
}
|
|
- if (charc.properties.write || element.properties.writeWithoutResponse) {
|
|
|
|
|
|
+ if (charc.properties.write || charc.properties.writeWithoutResponse) {
|
|
// 写入的
|
|
// 写入的
|
|
|
|
+ bleManager.setWrite(charc, charc.uuid)
|
|
|
|
+
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ this._connectSuccess()
|
|
|
|
+ }, 1000);
|
|
|
|
+
|
|
|
|
+ if (onChanged) {
|
|
|
|
+ onChanged(true)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
if (charc.properties.read) {
|
|
if (charc.properties.read) {
|
|
- console.log('读read_id:', that.data.read_id);
|
|
|
|
- var chara = await bleManager.readCharacteristicValue({
|
|
|
|
- deviceId: data.deviceId,
|
|
|
|
- serviceId: uuid,
|
|
|
|
- characteristicId: characteristics,
|
|
|
|
- })
|
|
|
|
|
|
+ var chara = await bleManager.readCharacteristicValue(charc.uuid,)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- console.log("查询特征值:", chara)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- if (onChanged) {
|
|
|
|
- onChanged(true)
|
|
|
|
- }
|
|
|
|
// this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
|
|
// this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
|
|
|
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -143,7 +157,6 @@ class BtHelper {
|
|
}
|
|
}
|
|
|
|
|
|
async disconnect(data) {
|
|
async disconnect(data) {
|
|
- // await this._helper.disconnect();
|
|
|
|
bleManager.disconnect(data.deviceId)
|
|
bleManager.disconnect(data.deviceId)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -152,7 +165,11 @@ class BtHelper {
|
|
}
|
|
}
|
|
|
|
|
|
async send(cmd, type) {
|
|
async send(cmd, type) {
|
|
- // bleManager.sendData()
|
|
|
|
|
|
+ // console.log('开始发送数据:', cmd)
|
|
|
|
+ if (cmd) {
|
|
|
|
+ bleManager.sendData(cmd)
|
|
|
|
+ }
|
|
|
|
+
|
|
// QueueManager.instance.addTask({ task: cmd });
|
|
// QueueManager.instance.addTask({ task: cmd });
|
|
}
|
|
}
|
|
// onLoad: function () {
|
|
// onLoad: function () {
|
|
@@ -162,85 +179,47 @@ class BtHelper {
|
|
|
|
|
|
async initBluetooth() {
|
|
async initBluetooth() {
|
|
try {
|
|
try {
|
|
- let res = await bleManager.initBluetoothAdapter();
|
|
|
|
- console.log(res)
|
|
|
|
|
|
+ bleManager.initBluetoothAdapter();
|
|
|
|
+ // console.log(res)
|
|
|
|
|
|
- let per = await bleManager.checkBluetoothPermission();
|
|
|
|
- console.log(per)
|
|
|
|
|
|
+ bleManager.checkBluetoothPermission();
|
|
|
|
+ // console.log(per)
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(error);
|
|
console.error(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ closeBle() {
|
|
|
|
+ bleManager.closeBle()
|
|
|
|
+ }
|
|
|
|
+
|
|
async getConnectedDevices() {
|
|
async getConnectedDevices() {
|
|
try {
|
|
try {
|
|
- // const connectedDevices = await bleManager.getConnectedDevices();
|
|
|
|
- const allDevices = await bleManager.getAllConnectedDevices()
|
|
|
|
- console.log("全部设备:", allDevices.length)
|
|
|
|
- return allDevices
|
|
|
|
|
|
+ const connectedDevices = await bleManager.getConnectedDevices();
|
|
|
|
+ console.log("全部设备1:", connectedDevices)
|
|
|
|
+ if (connectedDevices.devices.length) {
|
|
|
|
+ // todo 已经连接上的设备
|
|
|
|
+ }
|
|
|
|
+ // const allDevices = await bleManager.getAllConnectedDevices()
|
|
|
|
+ // console.log("全部设备2:", allDevices.length)
|
|
|
|
+ return connectedDevices
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(error);
|
|
console.error(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- // disconnectFromDevice: async function(deviceId) {
|
|
|
|
- // try {
|
|
|
|
- // await bleManager.disconnectFromDevice(deviceId);
|
|
|
|
- // console.log('断开连接成功');
|
|
|
|
- // this.setData({ connectedDeviceId: null });
|
|
|
|
- // } catch (error) {
|
|
|
|
- // console.error(error);
|
|
|
|
- // }
|
|
|
|
- // },
|
|
|
|
-
|
|
|
|
- // sendData: async function(deviceId, serviceId, characteristicId, data) {
|
|
|
|
- // try {
|
|
|
|
- // await bleManager.sendData(deviceId, serviceId, characteristicId, data);
|
|
|
|
- // console.log('数据发送成功');
|
|
|
|
- // } catch (error) {
|
|
|
|
- // console.error(error);
|
|
|
|
- // }
|
|
|
|
- // },
|
|
|
|
-
|
|
|
|
- // stopDiscovery: async function() {
|
|
|
|
- // try {
|
|
|
|
- // await bleManager.stopBluetoothDevicesDiscovery();
|
|
|
|
- // console.log('停止搜索成功');
|
|
|
|
- // } catch (error) {
|
|
|
|
- // console.error(error);
|
|
|
|
- // }
|
|
|
|
- // },
|
|
|
|
-
|
|
|
|
- // readCharacteristicValue: async function(deviceId, serviceId, characteristicId) {
|
|
|
|
- // try {
|
|
|
|
- // const value = await bleManager.readCharacteristicValue(deviceId, serviceId, characteristicId);
|
|
|
|
- // console.log('读取特征值:', value);
|
|
|
|
- // } catch (error) {
|
|
|
|
- // console.error(error);
|
|
|
|
- // }
|
|
|
|
- // },
|
|
|
|
-
|
|
|
|
- // notifyCharacteristicValueChange: async function(deviceId, serviceId, characteristicId, state) {
|
|
|
|
- // try {
|
|
|
|
- // const result = await bleManager.notifyCharacteristicValueChange(deviceId, serviceId, characteristicId, state);
|
|
|
|
- // console.log('通知特征值变化:', result);
|
|
|
|
- // } catch (error) {
|
|
|
|
- // console.error(error);
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
-
|
|
|
|
- checkDevice() {
|
|
|
|
- this.send(BtCmd.checkDevice);
|
|
|
|
|
|
+ async checkDevice() {
|
|
|
|
+ // console.log("校验设备:", BtCmd); // 输出: EarPhone Info
|
|
|
|
+ await this.send(BtCmd.checkDevice());
|
|
//2.0有发这个,不知道是啥
|
|
//2.0有发这个,不知道是啥
|
|
- this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]));
|
|
|
|
|
|
+ await this.send([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]);
|
|
}
|
|
}
|
|
|
|
|
|
- getVersion() {
|
|
|
|
- this.send(BtCmd.queryVersion);
|
|
|
|
|
|
+ async getVersion() {
|
|
|
|
+ await this.send(BtCmd.queryVersion());
|
|
}
|
|
}
|
|
|
|
|
|
- static get _time() {
|
|
|
|
|
|
+ _time() {
|
|
return 400;
|
|
return 400;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -252,58 +231,58 @@ class BtHelper {
|
|
// this.send(BtCmd.queryLowPower);
|
|
// this.send(BtCmd.queryLowPower);
|
|
// this.send(BtCmd.queryCtrlStatus);
|
|
// this.send(BtCmd.queryCtrlStatus);
|
|
// } else {
|
|
// } else {
|
|
- await this.send(BtCmd.setTime);
|
|
|
|
- await this.send(BtCmd.queryKwh);
|
|
|
|
|
|
+ await this.send(BtCmd.setTime());
|
|
|
|
+ await this.send(BtCmd.queryKwh());
|
|
await this.getSleep();
|
|
await this.getSleep();
|
|
await this.getAlert();
|
|
await this.getAlert();
|
|
- await this.send(BtCmd.queryRGB);
|
|
|
|
|
|
+ await this.send(BtCmd.queryRGB());
|
|
await this.getVolume();
|
|
await this.getVolume();
|
|
await this.getPauseSleep();
|
|
await this.getPauseSleep();
|
|
// }
|
|
// }
|
|
}
|
|
}
|
|
|
|
|
|
- getAlert() {
|
|
|
|
- this.send(BtCmd.queryAlarm);
|
|
|
|
|
|
+ async getAlert() {
|
|
|
|
+ await this.send(BtCmd.queryAlarm());
|
|
}
|
|
}
|
|
|
|
|
|
- getSleep() {
|
|
|
|
- this.send(BtCmd.querySleep);
|
|
|
|
|
|
+ async getSleep() {
|
|
|
|
+ await this.send(BtCmd.querySleep());
|
|
}
|
|
}
|
|
|
|
|
|
- setAlert(open, weekCycle, hour, minutes, channel = 1) {
|
|
|
|
- this.send(BtCmd.setAlarm({ switchStatus: open, weekCycle, hour, minutes }));
|
|
|
|
|
|
+ async setAlert(open, weekCycle, hour, minutes, channel = 1) {
|
|
|
|
+ await this.send(BtCmd.setAlarm({ switchStatus: open, weekCycle, hour, minutes }));
|
|
}
|
|
}
|
|
|
|
|
|
- setAutoPlay(open) {
|
|
|
|
- this.send(BtCmd.setAutoPlay({ switchStatus: open }));
|
|
|
|
|
|
+ async setAutoPlay(open) {
|
|
|
|
+ await this.send(BtCmd.setAutoPlay({ switchStatus: open }));
|
|
}
|
|
}
|
|
|
|
|
|
- getAutoPlay() {
|
|
|
|
- this.send(BtCmd.getAutoPlay);
|
|
|
|
|
|
+ async getAutoPlay() {
|
|
|
|
+ await this.send(BtCmd.getAutoPlay);
|
|
}
|
|
}
|
|
|
|
|
|
- setLowKwhWarningTone(notify) {
|
|
|
|
- this.send(BtCmd.setLowKwhWarningTone({ notify }));
|
|
|
|
|
|
+ async setLowKwhWarningTone(notify) {
|
|
|
|
+ await this.send(BtCmd.setLowKwhWarningTone({ notify }));
|
|
}
|
|
}
|
|
|
|
|
|
- setPauseSleep(time) {
|
|
|
|
- this.send(BtCmd.setSleepAfterPlayPause({ time }));
|
|
|
|
|
|
+ async setPauseSleep(time) {
|
|
|
|
+ await this.send(BtCmd.setSleepAfterPlayPause({ time }));
|
|
}
|
|
}
|
|
|
|
|
|
- getPauseSleep() {
|
|
|
|
- this.send(BtCmd.querySleepAfterPlayPause);
|
|
|
|
|
|
+ async getPauseSleep() {
|
|
|
|
+ await this.send(BtCmd.querySleepAfterPlayPause());
|
|
}
|
|
}
|
|
|
|
|
|
- setRGB(r, g, b) {
|
|
|
|
- this.send(BtCmd.setRGB({ r, g, b }));
|
|
|
|
|
|
+ async setRGB(r, g, b) {
|
|
|
|
+ await this.send(BtCmd.setRGB({ r, g, b }));
|
|
}
|
|
}
|
|
|
|
|
|
- setSleep(open, hour, minutes) {
|
|
|
|
- this.send(BtCmd.setSleep({ switchStatus: open, hour, minutes }));
|
|
|
|
|
|
+ async setSleep(open, hour, minutes) {
|
|
|
|
+ await this.send(BtCmd.setSleep({ switchStatus: open, hour, minutes }));
|
|
}
|
|
}
|
|
|
|
|
|
- setTime() {
|
|
|
|
- this.send(BtCmd.setTime);
|
|
|
|
|
|
+ async setTime() {
|
|
|
|
+ await this.send(BtCmd.setTime());
|
|
}
|
|
}
|
|
|
|
|
|
async setVolume(volume) {
|
|
async setVolume(volume) {
|
|
@@ -314,11 +293,11 @@ class BtHelper {
|
|
|
|
|
|
console.log(`phoneMax=${phoneMax}, deviceMax=${CmdBase.volumeMax}, setVolume=${volume}, result=${result}`);
|
|
console.log(`phoneMax=${phoneMax}, deviceMax=${CmdBase.volumeMax}, setVolume=${volume}, result=${result}`);
|
|
|
|
|
|
- this.send(BtCmd.setVolume({ volume: result }));
|
|
|
|
|
|
+ await this.send(BtCmd.setVolume({ volume: result }));
|
|
}
|
|
}
|
|
|
|
|
|
- getVolume() {
|
|
|
|
- this.send(BtCmd.queryVolume);
|
|
|
|
|
|
+ async getVolume() {
|
|
|
|
+ await this.send(BtCmd.queryVolume());
|
|
}
|
|
}
|
|
|
|
|
|
stop() {
|
|
stop() {
|
|
@@ -360,47 +339,47 @@ class BtHelper {
|
|
console.log(`回调结束录音:${supplier}`);
|
|
console.log(`回调结束录音:${supplier}`);
|
|
switch (supplier) {
|
|
switch (supplier) {
|
|
case EnumSupplier.jieLi:
|
|
case EnumSupplier.jieLi:
|
|
- this.send(BtCmd.jlStopVoiceResponse);
|
|
|
|
|
|
+ this.send(BtCmd.jlStopVoiceResponse());
|
|
break;
|
|
break;
|
|
case EnumSupplier.lingXin:
|
|
case EnumSupplier.lingXin:
|
|
- this.send(BtCmd.lxStopVoiceResponse);
|
|
|
|
|
|
+ this.send(BtCmd.lxStopVoiceResponse());
|
|
break;
|
|
break;
|
|
case EnumSupplier.qiXinWei:
|
|
case EnumSupplier.qiXinWei:
|
|
- this.send(BtCmd.b1StopVoiceResponse);
|
|
|
|
|
|
+ this.send(BtCmd.b1StopVoiceResponse());
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- getLowDelayMode() {
|
|
|
|
- this.send(BtCmd.queryLowDelayMode);
|
|
|
|
|
|
+ async getLowDelayMode() {
|
|
|
|
+ await this.send(BtCmd.queryLowDelayMode());
|
|
}
|
|
}
|
|
|
|
|
|
- setLowDelayMode(open, mode) {
|
|
|
|
- this.send(BtCmd.setLowDelayMode({ open, mode }));
|
|
|
|
|
|
+ async setLowDelayMode(open, mode) {
|
|
|
|
+ await this.send(BtCmd.setLowDelayMode({ open, mode }));
|
|
}
|
|
}
|
|
|
|
|
|
- setLowPowerMode(isOpen) {
|
|
|
|
- this.send(BtCmd.setLowPowerMode({ isOpen: isOpen ? EnumOpen.open : EnumOpen.close }));
|
|
|
|
|
|
+ async setLowPowerMode(isOpen) {
|
|
|
|
+ await this.send(BtCmd.setLowPowerMode({ isOpen: isOpen ? EnumOpen.open : EnumOpen.close }));
|
|
}
|
|
}
|
|
|
|
|
|
- queryLowPower() {
|
|
|
|
- this.send(BtCmd.queryLowPower);
|
|
|
|
|
|
+ async queryLowPower() {
|
|
|
|
+ await this.send(BtCmd.queryLowPower());
|
|
}
|
|
}
|
|
|
|
|
|
- setCtrlStatus(singleClick, doubleClick, longClick) {
|
|
|
|
- this.send(BtCmd.setCtrlStatus({ singleClick, doubleClick, longClick }));
|
|
|
|
|
|
+ async setCtrlStatus(singleClick, doubleClick, longClick) {
|
|
|
|
+ await this.send(BtCmd.setCtrlStatus({ singleClick, doubleClick, longClick }));
|
|
}
|
|
}
|
|
|
|
|
|
- getEQ() {
|
|
|
|
- this.send(BtCmd.queryEQ);
|
|
|
|
|
|
+ async getEQ() {
|
|
|
|
+ await this.send(BtCmd.queryEQ());
|
|
}
|
|
}
|
|
|
|
|
|
- setEQ(list) {
|
|
|
|
- this.send(BtCmd.setEQ({ list }));
|
|
|
|
|
|
+ async setEQ(list) {
|
|
|
|
+ await this.send(BtCmd.setEQ({ list }));
|
|
}
|
|
}
|
|
|
|
|
|
- queryLowKwhWarningTone() {
|
|
|
|
- this.send(BtCmd.queryLowKwhWarningTone);
|
|
|
|
|
|
+ async queryLowKwhWarningTone() {
|
|
|
|
+ await this.send(BtCmd.queryLowKwhWarningTone());
|
|
}
|
|
}
|
|
|
|
|
|
async changeChannelCallBack() {
|
|
async changeChannelCallBack() {
|