// 引入必要的模块 // const DeviceUtil = require('./DeviceUtil'); // const BtAndroidHelper = require('./BtAndroidHelper'); // const BtIOSHelper = require('./BtIOSHelper'); // const QueueManager = require('./QueueManager'); // const DeviceManager = require('./DeviceManager'); const { CmdBase, BtCmd, } = require('./../devices/bluetooth/bt_cmd'); const BtParse = require('./../devices/bluetooth/bt_parse'); // const VolumeUtil = require('./VolumeUtil'); // const EventManager = require('./EventManager'); // const CmdBase = require('./../devices/bluetooth/bt_cmd'); const bleManager = require('./ble_manager'); const QueueManager = require('./QueueManager'); // const EnumConnectStatus = require('./EnumConnectStatus'); // const EnumOpen = require('./EnumOpen'); // const EnumSupplier = require('./EnumSupplier'); // const EnumLowDelayModeOpen = require('./EnumLowDelayModeOpen'); // const EnumLowDelayMode = require('./EnumLowDelayMode'); // const EnumPlayStatus = require('./EnumPlayStatus'); class BtHelper { static getInstance() { // if (!BtHelper._instance) { // BtHelper._instance = new BtHelper(); // } return BtHelper._instance; } static _instance = new BtHelper(); static _isConnecting = false; static isDisConnectByOTA = false; // _helper; constructor() { this.timer = null; this.bleManager = new bleManager(); // if (DeviceUtil.isAndroid) { // this._helper = BtAndroidHelper.instance; // } else { // this._helper = BtIOSHelper.instance; // } // QueueManager.instance.listenTask((task) => { // if (!DeviceManager.instance.isWatch) { // this._helper.send({ cmd: task }); // } // }); this.queueManager = new QueueManager(); let _this = this this.queueManager.clear(); this.queueManager.listenTask((task) => { _this.bleManager.sendData(task); }); } resetConnectState(connect) { // BtHelper._isConnecting = connect; } async search(stopCall) { let res = await this.bleManager.startScan() this.timer = null; this.timer = setTimeout(() => { this.stopSearch(); if (stopCall) { stopCall() } }, 10000); // console.log(res); // return await this._helper.search(); } async findDevices(callback) { this.bleManager.onBluetoothDeviceFound(callback) // return await this._helper.search(); } async stopSearch() { clearTimeout(this.timer); this.timer = null; await this.bleManager.stopScan(); } async _connectSuccess() { this.checkDevice() this.getDeviceInfo() } async connect(data, onChanged) { // await this._helper.connect({ data, onChanged, isClick }); 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 } const serviceId = await this.bleManager.discoverServices(data.deviceId); if (serviceId == "") { console.log("连接失败") this.disconnect(data) if (onChanged) { onChanged(false) } 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) } 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 } }); } async disconnect(data) { this.bleManager.disconnect(data.deviceId) } async dispose() { // await this._helper.dispose(); } async sendNow(cmd) { if (cmd) { this.bleManager.sendData(cmd) } } async send(cmd, type) { if (cmd) { this.queueManager.addTask(cmd) } // QueueManager.instance.addTask({ task: cmd }); } // onLoad: function () { // bleManager = new bleManager(); // this.initBluetooth(); // } async initBluetooth(callback) { let adpter = await this.bleManager.initBluetoothAdapter((res) => { if (!res) { wx.showToast({ title: '请开启蓝牙功能', icon: 'none' }) if (callback) { callback(false, false) } return } }); // console.log(res) this.bleManager.checkBluetoothPermission(function (per) { if (!per) { wx.showToast({ title: '没有蓝牙权限', icon: 'none' }) } if (callback) { callback(adpter, per) } }); } closeBle() { this.bleManager.closeBle() } async getConnectedDevices() { try { const connectedDevices = await this.bleManager.getConnectedDevices(); console.log("全部设备1:", connectedDevices) if (connectedDevices.length) { // todo 已经连接上的设备 } const allDevices = await this.bleManager.getAllConnectedDevices() console.log("全部设备2:", allDevices) let newDevices = connectedDevices.concat(allDevices); return newDevices } catch (error) { console.error(error); return [] } } async checkDevice() { // console.log("校验设备:", BtCmd); // 输出: EarPhone Info this.sendNow(BtCmd.checkDevice()); //2.0有发这个,不知道是啥 this.send([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]); } async getVersion() { this.send(BtCmd.queryVersion()); } _time() { return 400; } getDeviceInfo() { // if (DeviceManager.instance.isEarphone) { // this.send(BtCmd.queryKwhEarPhone); // this.send(BtCmd.queryLowDelayMode); // this.send(BtCmd.queryEQ); // this.send(BtCmd.queryLowPower); // this.send(BtCmd.queryCtrlStatus); // } else { // this.send(BtCmd.setDevTime()); this.send(BtCmd.queryKwh()); this.getSleep(); this.getAlert(); this.send(BtCmd.queryRGB()); this.getVolume(); this.setVolume(8) this.getPauseSleep(); // } } async getAlert() { this.send(BtCmd.queryAlarm()); } async getSleep() { this.send(BtCmd.querySleep()); } async setAlert(open, weekCycle, hour, minutes, channel = 1) { this.send(BtCmd.setAlarm({ switchStatus: open, weekCycle, hour, minutes })); } async setAutoPlay(open) { this.send(BtCmd.setAutoPlay({ switchStatus: open })); } async getAutoPlay() { this.send(BtCmd.getAutoPlay); } async setLowKwhWarningTone(notify) { this.send(BtCmd.setLowKwhWarningTone({ notify })); } async setPauseSleep(time) { this.send(BtCmd.setSleepAfterPlayPause({ time })); } async getPauseSleep() { this.send(BtCmd.querySleepAfterPlayPause()); } async setRGB(r, g, b) { this.send(BtCmd.setRGB({ r, g, b })); } async setSleep(open, hour, minutes) { this.send(BtCmd.setSleep({ switchStatus: open, hour, minutes })); } async setTime() { console.log("setTime========111") // this.send(BtCmd.setTime()); } async setVolume(volume) { // VolumeUtil.setVolume(volume, { hideVolumeView: true }); const phoneMax = 15;//await VolumeUtil.getMaxVolume(); const result = Math.floor(CmdBase.volumeMax * volume / phoneMax); console.log(`phoneMax=${phoneMax}, deviceMax=${CmdBase.volumeMax}, setVolume=${volume}, result=${result}`); this.send(BtCmd.setVolume({ volume: 8 })); } async getVolume() { this.send(BtCmd.queryVolume()); } stop() { // EventManager.fire({ event: 'playStatus', playStatus: EnumPlayStatus.stop.index }); } next() { // TODO: implement next } pause() { // EventManager.fire({ event: 'playStatus', playStatus: EnumPlayStatus.pause.index }); } play() { // TODO: implement play } previous() { // TODO: implement previous } startVoiceRecordResponse(supplier) { console.log(`回调开始录音:${supplier}`); // switch (supplier) { // case EnumSupplier.jieLi: // this.send(BtCmd.jlStartVoiceResponse); // break; // case EnumSupplier.lingXin: // this.send(BtCmd.lxStartVoiceResponse); // break; // case EnumSupplier.qiXinWei: // this.send(BtCmd.b1StartVoiceResponse); // break; // } } stopVoiceRecordResponse(supplier) { console.log(`回调结束录音:${supplier}`); // switch (supplier) { // case EnumSupplier.jieLi: // this.send(BtCmd.jlStopVoiceResponse()); // break; // case EnumSupplier.lingXin: // this.send(BtCmd.lxStopVoiceResponse()); // break; // case EnumSupplier.qiXinWei: // this.send(BtCmd.b1StopVoiceResponse()); // break; // } } async getLowDelayMode() { this.send(BtCmd.queryLowDelayMode()); } async setLowDelayMode(open, mode) { this.send(BtCmd.setLowDelayMode({ open, mode })); } async setLowPowerMode(isOpen) { this.send(BtCmd.setLowPowerMode({ isOpen: isOpen ? EnumOpen.open : EnumOpen.close })); } async queryLowPower() { this.send(BtCmd.queryLowPower()); } async setCtrlStatus(singleClick, doubleClick, longClick) { this.send(BtCmd.setCtrlStatus({ singleClick, doubleClick, longClick })); } async getEQ() { this.send(BtCmd.queryEQ()); } async setEQ(list) { this.send(BtCmd.setEQ({ list })); } async queryLowKwhWarningTone() { this.send(BtCmd.queryLowKwhWarningTone()); } async changeChannelCallBack() { this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x0a, 0x29, 0x02, 0x00, 0x01])); this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x39, 0x01, 0x00])); } } // 导出 BtHelper 类 // const btHelper = new BtHelper(); module.exports = { BtHelper }