// 引入必要的模块 // const DeviceUtil = require('./DeviceUtil'); // const BtAndroidHelper = require('./BtAndroidHelper'); // const BtIOSHelper = require('./BtIOSHelper'); // const QueueManager = require('./QueueManager'); // const DeviceManager = require('./DeviceManager'); const BtCmd = require('./../devices/bluetooth/bt_cmd'); // const VolumeUtil = require('./VolumeUtil'); // const EventManager = require('./EventManager'); const CmdBase = require('./../devices/bluetooth/bt_cmd'); const bleManager = require('./ble_manager'); // const EnumConnectStatus = require('./EnumConnectStatus'); // const EnumOpen = require('./EnumOpen'); // const EnumSupplier = require('./EnumSupplier'); // const EnumLowDelayModeOpen = require('./EnumLowDelayModeOpen'); // const EnumLowDelayMode = require('./EnumLowDelayMode'); // const EnumPlayStatus = require('./EnumPlayStatus'); // 或者使用对象字面量 const EnumSupplier = { shanJing: "ShanJing", jieLi: "JieLi", lingXin: "LingXin", qiXinWei: "QiXinWei" }; const EnumPlayStatus = { none: 'none', play: 'play', pause: 'pause', stop: 'stop', completed: 'completed', error: 'error', buffering: 'buffering', // APP自己定义的特殊处理中间状态 }; class BtHelper { // static get instance() { // return this._instance; // } // static _instance = new BtHelper(); static _isConnecting = false; static isDisConnectByOTA = false; // _helper; constructor() { // 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 }); // } // }); } resetConnectState(connect) { // BtHelper._isConnecting = connect; } async search(mqttFilterList) { let res = await bleManager.startScan() setTimeout(() => { bleManager.stopScan(); }, 10000); // console.log(res); // return await this._helper.search(); } async findDevices(callback) { bleManager.onBluetoothDeviceFound(callback) // return await this._helper.search(); } async stopSearch() { bleManager.stopScan(); } async connect(data, onChanged) { // await this._helper.connect({ data, onChanged, isClick }); try { var res = await bleManager.connectToDevice(data); console.log('连接成功'); // this.setData({ connectedDeviceId: deviceId }); if (res === false) { console.log("连接失败") return } const serviceId = await bleManager.discoverServices(data.deviceId); // this.setData({ services }); console.log("服务ID", serviceId) var characteristics = await bleManager.discoverCharacteristics({ deviceId: data.deviceId, serviceId: serviceId, }) console.log('device特征值:', res.characteristics) for (let i = 0; i < res.characteristics.length; i++) { let charc = res.characteristics[i]; if (charc.properties.notify) { // 订阅的 bleManager.notifyCharacteristicValueChange(data.deviceId, service.serviceId, characteristics, true, (res) => { // console.log(res) }) } if (charc.properties.write || element.properties.writeWithoutResponse) { // 写入的 } if (charc.properties.read) { console.log('读read_id:', that.data.read_id); var chara = await bleManager.readCharacteristicValue({ deviceId: data.deviceId, serviceId: uuid, characteristicId: characteristics, }) } } console.log("查询特征值:", chara) if (onChanged) { onChanged(true) } // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } }); } catch (error) { console.error(error); } } async disconnect(data) { // await this._helper.disconnect(); bleManager.disconnect(data.deviceId) } async dispose() { // await this._helper.dispose(); } async send(cmd, type) { // bleManager.sendData() // QueueManager.instance.addTask({ task: cmd }); } // onLoad: function () { // bleManager = new bleManager(); // this.initBluetooth(); // } async initBluetooth() { try { let res = await bleManager.initBluetoothAdapter(); console.log(res) let per = await bleManager.checkBluetoothPermission(); console.log(per) } catch (error) { console.error(error); } } async getConnectedDevices() { try { // const connectedDevices = await bleManager.getConnectedDevices(); const allDevices = await bleManager.getAllConnectedDevices() console.log("全部设备:", allDevices.length) return allDevices } catch (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); //2.0有发这个,不知道是啥 this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01])); } getVersion() { this.send(BtCmd.queryVersion); } static get _time() { return 400; } async 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 { await this.send(BtCmd.setTime); await this.send(BtCmd.queryKwh); await this.getSleep(); await this.getAlert(); await this.send(BtCmd.queryRGB); await this.getVolume(); await this.getPauseSleep(); // } } getAlert() { this.send(BtCmd.queryAlarm); } getSleep() { this.send(BtCmd.querySleep); } setAlert(open, weekCycle, hour, minutes, channel = 1) { this.send(BtCmd.setAlarm({ switchStatus: open, weekCycle, hour, minutes })); } setAutoPlay(open) { this.send(BtCmd.setAutoPlay({ switchStatus: open })); } getAutoPlay() { this.send(BtCmd.getAutoPlay); } setLowKwhWarningTone(notify) { this.send(BtCmd.setLowKwhWarningTone({ notify })); } setPauseSleep(time) { this.send(BtCmd.setSleepAfterPlayPause({ time })); } getPauseSleep() { this.send(BtCmd.querySleepAfterPlayPause); } setRGB(r, g, b) { this.send(BtCmd.setRGB({ r, g, b })); } setSleep(open, hour, minutes) { this.send(BtCmd.setSleep({ switchStatus: open, hour, minutes })); } setTime() { this.send(BtCmd.setTime); } async setVolume(volume) { VolumeUtil.setVolume(volume, { hideVolumeView: true }); const phoneMax = 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: result })); } 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; } } getLowDelayMode() { this.send(BtCmd.queryLowDelayMode); } setLowDelayMode(open, mode) { this.send(BtCmd.setLowDelayMode({ open, mode })); } setLowPowerMode(isOpen) { this.send(BtCmd.setLowPowerMode({ isOpen: isOpen ? EnumOpen.open : EnumOpen.close })); } queryLowPower() { this.send(BtCmd.queryLowPower); } setCtrlStatus(singleClick, doubleClick, longClick) { this.send(BtCmd.setCtrlStatus({ singleClick, doubleClick, longClick })); } getEQ() { this.send(BtCmd.queryEQ); } setEQ(list) { this.send(BtCmd.setEQ({ list })); } queryLowKwhWarningTone() { this.send(BtCmd.queryLowKwhWarningTone); } async changeChannelCallBack() { await this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x0a, 0x29, 0x02, 0x00, 0x01])); await this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x39, 0x01, 0x00])); } } // 导出 BtHelper 类 const btHelper = new BtHelper(); module.exports = btHelper;