ble.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // pages/light/light.js
  2. import { connectStateMap, connectStateTypes } from '../../data/devType.js'
  3. import { MAX_COLOR_TEMPERATURE, MIN_COLOR_TEMPERATURE, COLOR_TEMPERATURE_STEP, bleInfo } from '../../data/lampType.js'
  4. import { handle } from '../../utils/mjs_handle.js'
  5. import { calculateColor } from '../../utils/mjs_color.js'
  6. import light_cmd from '../../utils/light_cmd.js'
  7. import { _curry } from '../../utils/mjs_curry.js'
  8. import BLE from '../../utils/mjs_wxble.js'
  9. console.log(BLE);
  10. const ble = new BLE();
  11. /** mock 数据 */
  12. const mock_bleDevs = [
  13. {
  14. id: 1,
  15. name: "123",
  16. deviceId: "1234"
  17. },
  18. {
  19. id: 2,
  20. name: "设备2",
  21. deviceId: "23456"
  22. }
  23. ]
  24. const mock_bleServices = [
  25. {
  26. uuid: "123",
  27. isPrimary: true,
  28. characteristics: [
  29. {
  30. uuid: "123",
  31. properties: {
  32. read: true,
  33. write: true,
  34. notify: true,
  35. indicate: true,
  36. }
  37. }
  38. ]
  39. },
  40. {
  41. uuid: "1234",
  42. characteristics: [
  43. {
  44. uuid: "12345",
  45. properties: {
  46. read: true,
  47. write: true,
  48. notify: true,
  49. indicate: true,
  50. }
  51. },
  52. {
  53. uuid: "123456",
  54. properties: {
  55. read: true,
  56. write: true,
  57. notify: true,
  58. indicate: true,
  59. }
  60. }
  61. ]
  62. }
  63. ]
  64. // 蓝牙模块连接对象信息参数
  65. const connectBleParams = [];
  66. // 蓝牙服务信息
  67. let bleServices = [];
  68. Page({
  69. /**
  70. * 页面的初始数据
  71. */
  72. data: {
  73. // unConnect scaning connecting connected
  74. connectStateTypes: connectStateTypes,
  75. ble: {
  76. state: connectStateTypes.unConnect,
  77. devName: "123",
  78. deviceId: "",
  79. init: false,
  80. // 控制uuid
  81. controlUuid: "",
  82. // 监听数据uuid
  83. notifyUuid: "",
  84. },
  85. bleDevs: mock_bleDevs,
  86. bleServices: mock_bleServices,
  87. },
  88. searchDeviceHandle() {
  89. this.excuteSearchDevice();
  90. },
  91. disconnectHandle(){
  92. this.excuteDisconnect();
  93. },
  94. async excuteDisconnect(){
  95. console.log('断开连接');
  96. if(this.data.ble.state !== connectStateTypes.connected){
  97. return this.bleFail('断开连接失败', '当前未连接设备');
  98. }
  99. let err,res;
  100. [err,res] = await handle(ble.disconnectDev(this.data.ble.deviceId));
  101. if(err){
  102. return this.bleFail('断开连接失败', err.errMsg);
  103. }
  104. wx.showToast({title: `断开连接成功`, })
  105. this.initPageInfo();
  106. },
  107. // 获取蓝牙发送数据对象
  108. excuteBleSend(data){
  109. console.log('获取蓝牙发送数据对象');
  110. // 固定值
  111. let controlUuid = bleInfo.controlUuid;
  112. let characteristicFirstUuid = bleInfo.characteristicFirstUuid;
  113. let deviceId = this.data.ble.deviceId;
  114. let serverUuid = '';
  115. let characteristicUuid = '';
  116. let characteristic;
  117. let server = bleServices.find(s=>{
  118. console.log(`服务uuid ${s.uuid}`);
  119. console.log(s);
  120. return s.uuid.startsWith(controlUuid);
  121. });
  122. if(!server)
  123. {
  124. serverUuid = bleInfo.controlUuid;
  125. }else
  126. {
  127. serverUuid = server.uuid;
  128. characteristic = server.characteristics.find(c=>
  129. c.uuid.startsWith(characteristicFirstUuid)
  130. );
  131. }
  132. //获取
  133. if(!characteristic)
  134. {
  135. // return bleFail('获取服务失败', '该设备不支持该程序');
  136. characteristicUuid = bleInfo.controlUuid;
  137. }
  138. else
  139. {
  140. characteristicUuid = characteristic.uuid;
  141. }
  142. console.log(`服务uuid ${serverUuid} 特征uuid ${characteristicUuid}`);
  143. return ble.sendData(deviceId, serverUuid, characteristicUuid, data);
  144. },
  145. initPageInfo(){
  146. this.setData({
  147. ble: {...this.data.ble,
  148. state: connectStateTypes.unConnect,
  149. init: false,
  150. devName: "",
  151. deviceId: "",
  152. },
  153. bleDevs: [],
  154. bleServices: []
  155. });
  156. bleServices = [];
  157. },
  158. // 连接失败
  159. bleFail(title,msg){
  160. wx.showModal({
  161. title: title,
  162. content: msg,
  163. success: (res) =>{
  164. this.initPageInfo();
  165. }
  166. });
  167. },
  168. async excuteSearchDevice(){
  169. console.log("搜索 蓝牙设备");
  170. let err,res;
  171. if(!this.data.ble.init){
  172. [err,res] = await handle(ble.initBle());
  173. if(err){
  174. return this.bleFail('蓝牙初始化失败', err.errMsg);
  175. }
  176. ble.onSearch = this.onSearchHandle;
  177. }
  178. this.setData({
  179. ble: {...this.data.ble,
  180. state: connectStateTypes.scaning,
  181. init: true
  182. },
  183. bleDevs: []
  184. });
  185. ble.search(this.onSearchHandle);
  186. },
  187. // 搜索到设备基础函数
  188. onSearchHandle(device){
  189. console.log('搜索到设备');
  190. if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){
  191. return console.log('设备再次被搜索到');
  192. }
  193. console.log(device);
  194. device.id = device.deviceId;
  195. let arr = this.data.bleDevs;
  196. arr.push(device);
  197. this.setData({
  198. bleDevs: arr
  199. })
  200. },
  201. // 连接设备
  202. async connectDevHandle(e) {
  203. console.log("click connectDev")
  204. let err,res;
  205. let dev = e.detail;
  206. console.log(dev);
  207. this.setData({
  208. ble: {...this.data.ble,state: connectStateTypes.connecting,
  209. devName: dev.name,
  210. deviceId: dev.deviceId
  211. },
  212. });
  213. // 停止搜索
  214. [err,res] = await handle(ble.stopSearch());
  215. if(err){
  216. return this.bleFail('停止搜索失败', err.errMsg);
  217. }
  218. wx.showToast({title: `连接${dev.name}中...`, })
  219. // 连接至设备
  220. [err,res] = await handle(ble.connectDev(dev.deviceId));
  221. if(err){ return this.bleFail('连接失败', err.errMsg);}
  222. // 连接成功
  223. this.setData({
  224. ble:{...this.data.ble, state:connectStateTypes.connected }
  225. });
  226. // 设备服务管理
  227. this.connectServices(dev.deviceId);
  228. },
  229. async connectServices(deviceId){
  230. let [err,res] = await handle(ble.getBleServices(deviceId));
  231. if(err){ return this.bleFail('获取服务失败', err.errMsg);}
  232. // 保存服务信息;
  233. bleServices = res;
  234. this.setData({
  235. bleServices: res
  236. })
  237. },
  238. /**
  239. * 生命周期函数--监听页面加载
  240. */
  241. onLoad(options) {
  242. },
  243. /**
  244. * 生命周期函数--监听页面初次渲染完成
  245. */
  246. onReady() {
  247. },
  248. /**
  249. * 生命周期函数--监听页面显示
  250. */
  251. onShow() {
  252. // 配置导航栏
  253. wx.setNavigationBarTitle({
  254. title: '蓝牙工具箱'
  255. })
  256. },
  257. /**
  258. * 生命周期函数--监听页面隐藏
  259. */
  260. onHide() {
  261. },
  262. /**
  263. * 生命周期函数--监听页面卸载
  264. */
  265. onUnload() {
  266. },
  267. /**
  268. * 页面相关事件处理函数--监听用户下拉动作
  269. */
  270. onPullDownRefresh() {
  271. },
  272. /**
  273. * 页面上拉触底事件的处理函数
  274. */
  275. onReachBottom() {
  276. },
  277. /**
  278. * 用户点击右上角分享
  279. */
  280. onShareAppMessage() {
  281. }
  282. })