bt_helper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // 引入必要的模块
  2. // const DeviceUtil = require('./DeviceUtil');
  3. // const BtAndroidHelper = require('./BtAndroidHelper');
  4. // const BtIOSHelper = require('./BtIOSHelper');
  5. // const QueueManager = require('./QueueManager');
  6. // const DeviceManager = require('./DeviceManager');
  7. const BtCmd = require('./../devices/bluetooth/bt_cmd');
  8. // const VolumeUtil = require('./VolumeUtil');
  9. // const EventManager = require('./EventManager');
  10. const CmdBase = require('./../devices/bluetooth/bt_cmd');
  11. const bleManager = require('./ble_manager');
  12. // const EnumConnectStatus = require('./EnumConnectStatus');
  13. // const EnumOpen = require('./EnumOpen');
  14. // const EnumSupplier = require('./EnumSupplier');
  15. // const EnumLowDelayModeOpen = require('./EnumLowDelayModeOpen');
  16. // const EnumLowDelayMode = require('./EnumLowDelayMode');
  17. // const EnumPlayStatus = require('./EnumPlayStatus');
  18. // 或者使用对象字面量
  19. const EnumSupplier = {
  20. shanJing: "ShanJing",
  21. jieLi: "JieLi",
  22. lingXin: "LingXin",
  23. qiXinWei: "QiXinWei"
  24. };
  25. const EnumPlayStatus = {
  26. none: 'none',
  27. play: 'play',
  28. pause: 'pause',
  29. stop: 'stop',
  30. completed: 'completed',
  31. error: 'error',
  32. buffering: 'buffering', // APP自己定义的特殊处理中间状态
  33. };
  34. class BtHelper {
  35. // static get instance() {
  36. // return this._instance;
  37. // }
  38. // static _instance = new BtHelper();
  39. static _isConnecting = false;
  40. static isDisConnectByOTA = false;
  41. // _helper;
  42. constructor() {
  43. // if (DeviceUtil.isAndroid) {
  44. // this._helper = BtAndroidHelper.instance;
  45. // } else {
  46. // this._helper = BtIOSHelper.instance;
  47. // }
  48. // QueueManager.instance.listenTask((task) => {
  49. // if (!DeviceManager.instance.isWatch) {
  50. // this._helper.send({ cmd: task });
  51. // }
  52. // });
  53. }
  54. resetConnectState(connect) {
  55. // BtHelper._isConnecting = connect;
  56. }
  57. async search(mqttFilterList) {
  58. let res = await bleManager.startScan()
  59. console.log(res);
  60. // return await this._helper.search();
  61. }
  62. async findDevices(callback) {
  63. bleManager.onBluetoothDeviceFound(callback)
  64. // return await this._helper.search();
  65. }
  66. async stopSearch() {
  67. bleManager.stopScan();
  68. }
  69. async connect(data, onChanged, isClick = false) {
  70. // await this._helper.connect({ data, onChanged, isClick });
  71. try {
  72. await bleManager.connectToDevice(data.deviceId);
  73. console.log('连接成功');
  74. // this.setData({ connectedDeviceId: deviceId });
  75. const services = await bleManager.searchServices(deviceId);
  76. // this.setData({ services });
  77. for (const service of services) {
  78. const characteristics = await bleManager.searchCharacteristics(deviceId, service.uuid);
  79. bleManager.readBLECharacteristicValue({
  80. deviceId: data.deviceId,
  81. serviceId: services.serviceId,
  82. characteristicId: characteristics,
  83. })
  84. // this.setData({ characteristics: { ...this.data.characteristics, [service.uuid]: characteristics } });
  85. }
  86. } catch (error) {
  87. console.error(error);
  88. }
  89. }
  90. async disconnect(data) {
  91. // await this._helper.disconnect();
  92. bleManager.disconnect(data.deviceId)
  93. }
  94. async dispose() {
  95. // await this._helper.dispose();
  96. }
  97. async send(cmd, type) {
  98. // bleManager.sendData()
  99. // QueueManager.instance.addTask({ task: cmd });
  100. }
  101. // onLoad: function () {
  102. // bleManager = new bleManager();
  103. // this.initBluetooth();
  104. // }
  105. async initBluetooth() {
  106. try {
  107. let res = await bleManager.initBluetoothAdapter();
  108. console.log(res)
  109. let per = await bleManager.checkBluetoothPermission();
  110. console.log(per)
  111. } catch (error) {
  112. console.error(error);
  113. }
  114. }
  115. async getConnectedDevices() {
  116. try {
  117. const connectedDevices = await bleManager.getConnectedDevices();
  118. return connectedDevices
  119. } catch (error) {
  120. console.error(error);
  121. }
  122. }
  123. // disconnectFromDevice: async function(deviceId) {
  124. // try {
  125. // await bleManager.disconnectFromDevice(deviceId);
  126. // console.log('断开连接成功');
  127. // this.setData({ connectedDeviceId: null });
  128. // } catch (error) {
  129. // console.error(error);
  130. // }
  131. // },
  132. // sendData: async function(deviceId, serviceId, characteristicId, data) {
  133. // try {
  134. // await bleManager.sendData(deviceId, serviceId, characteristicId, data);
  135. // console.log('数据发送成功');
  136. // } catch (error) {
  137. // console.error(error);
  138. // }
  139. // },
  140. // stopDiscovery: async function() {
  141. // try {
  142. // await bleManager.stopBluetoothDevicesDiscovery();
  143. // console.log('停止搜索成功');
  144. // } catch (error) {
  145. // console.error(error);
  146. // }
  147. // },
  148. // readCharacteristicValue: async function(deviceId, serviceId, characteristicId) {
  149. // try {
  150. // const value = await bleManager.readCharacteristicValue(deviceId, serviceId, characteristicId);
  151. // console.log('读取特征值:', value);
  152. // } catch (error) {
  153. // console.error(error);
  154. // }
  155. // },
  156. // notifyCharacteristicValueChange: async function(deviceId, serviceId, characteristicId, state) {
  157. // try {
  158. // const result = await bleManager.notifyCharacteristicValueChange(deviceId, serviceId, characteristicId, state);
  159. // console.log('通知特征值变化:', result);
  160. // } catch (error) {
  161. // console.error(error);
  162. // }
  163. // }
  164. checkDevice() {
  165. this.send(BtCmd.checkDevice);
  166. //2.0有发这个,不知道是啥
  167. this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x26, 0x01, 0x01]));
  168. }
  169. getVersion() {
  170. this.send(BtCmd.queryVersion);
  171. }
  172. static get _time() {
  173. return 400;
  174. }
  175. async getDeviceInfo() {
  176. // if (DeviceManager.instance.isEarphone) {
  177. // this.send(BtCmd.queryKwhEarPhone);
  178. // this.send(BtCmd.queryLowDelayMode);
  179. // this.send(BtCmd.queryEQ);
  180. // this.send(BtCmd.queryLowPower);
  181. // this.send(BtCmd.queryCtrlStatus);
  182. // } else {
  183. await this.send(BtCmd.setTime);
  184. await this.send(BtCmd.queryKwh);
  185. await this.getSleep();
  186. await this.getAlert();
  187. await this.send(BtCmd.queryRGB);
  188. await this.getVolume();
  189. await this.getPauseSleep();
  190. // }
  191. }
  192. getAlert() {
  193. this.send(BtCmd.queryAlarm);
  194. }
  195. getSleep() {
  196. this.send(BtCmd.querySleep);
  197. }
  198. setAlert(open, weekCycle, hour, minutes, channel = 1) {
  199. this.send(BtCmd.setAlarm({ switchStatus: open, weekCycle, hour, minutes }));
  200. }
  201. setAutoPlay(open) {
  202. this.send(BtCmd.setAutoPlay({ switchStatus: open }));
  203. }
  204. getAutoPlay() {
  205. this.send(BtCmd.getAutoPlay);
  206. }
  207. setLowKwhWarningTone(notify) {
  208. this.send(BtCmd.setLowKwhWarningTone({ notify }));
  209. }
  210. setPauseSleep(time) {
  211. this.send(BtCmd.setSleepAfterPlayPause({ time }));
  212. }
  213. getPauseSleep() {
  214. this.send(BtCmd.querySleepAfterPlayPause);
  215. }
  216. setRGB(r, g, b) {
  217. this.send(BtCmd.setRGB({ r, g, b }));
  218. }
  219. setSleep(open, hour, minutes) {
  220. this.send(BtCmd.setSleep({ switchStatus: open, hour, minutes }));
  221. }
  222. setTime() {
  223. this.send(BtCmd.setTime);
  224. }
  225. async setVolume(volume) {
  226. VolumeUtil.setVolume(volume, { hideVolumeView: true });
  227. const phoneMax = await VolumeUtil.getMaxVolume();
  228. const result = Math.floor(CmdBase.volumeMax * volume / phoneMax);
  229. console.log(`phoneMax=${phoneMax}, deviceMax=${CmdBase.volumeMax}, setVolume=${volume}, result=${result}`);
  230. this.send(BtCmd.setVolume({ volume: result }));
  231. }
  232. getVolume() {
  233. this.send(BtCmd.queryVolume);
  234. }
  235. stop() {
  236. // EventManager.fire({ event: 'playStatus', playStatus: EnumPlayStatus.stop.index });
  237. }
  238. next() {
  239. // TODO: implement next
  240. }
  241. pause() {
  242. // EventManager.fire({ event: 'playStatus', playStatus: EnumPlayStatus.pause.index });
  243. }
  244. play() {
  245. // TODO: implement play
  246. }
  247. previous() {
  248. // TODO: implement previous
  249. }
  250. startVoiceRecordResponse(supplier) {
  251. console.log(`回调开始录音:${supplier}`);
  252. // switch (supplier) {
  253. // case EnumSupplier.jieLi:
  254. // this.send(BtCmd.jlStartVoiceResponse);
  255. // break;
  256. // case EnumSupplier.lingXin:
  257. // this.send(BtCmd.lxStartVoiceResponse);
  258. // break;
  259. // case EnumSupplier.qiXinWei:
  260. // this.send(BtCmd.b1StartVoiceResponse);
  261. // break;
  262. // }
  263. }
  264. stopVoiceRecordResponse(supplier) {
  265. console.log(`回调结束录音:${supplier}`);
  266. switch (supplier) {
  267. case EnumSupplier.jieLi:
  268. this.send(BtCmd.jlStopVoiceResponse);
  269. break;
  270. case EnumSupplier.lingXin:
  271. this.send(BtCmd.lxStopVoiceResponse);
  272. break;
  273. case EnumSupplier.qiXinWei:
  274. this.send(BtCmd.b1StopVoiceResponse);
  275. break;
  276. }
  277. }
  278. getLowDelayMode() {
  279. this.send(BtCmd.queryLowDelayMode);
  280. }
  281. setLowDelayMode(open, mode) {
  282. this.send(BtCmd.setLowDelayMode({ open, mode }));
  283. }
  284. setLowPowerMode(isOpen) {
  285. this.send(BtCmd.setLowPowerMode({ isOpen: isOpen ? EnumOpen.open : EnumOpen.close }));
  286. }
  287. queryLowPower() {
  288. this.send(BtCmd.queryLowPower);
  289. }
  290. setCtrlStatus(singleClick, doubleClick, longClick) {
  291. this.send(BtCmd.setCtrlStatus({ singleClick, doubleClick, longClick }));
  292. }
  293. getEQ() {
  294. this.send(BtCmd.queryEQ);
  295. }
  296. setEQ(list) {
  297. this.send(BtCmd.setEQ({ list }));
  298. }
  299. queryLowKwhWarningTone() {
  300. this.send(BtCmd.queryLowKwhWarningTone);
  301. }
  302. async changeChannelCallBack() {
  303. await this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x0a, 0x29, 0x02, 0x00, 0x01]));
  304. await this.send(Uint8Array.from([0x54, 0x44, 0x44, 0x48, 0x01, 0x09, 0x39, 0x01, 0x00]));
  305. }
  306. }
  307. // 导出 BtHelper 类
  308. const btHelper = new BtHelper();
  309. module.exports = btHelper;