// pages/light/light.js import { connectStateMap, connectStateTypes } from '../../data/devType.js' import { MAX_COLOR_TEMPERATURE, MIN_COLOR_TEMPERATURE, COLOR_TEMPERATURE_STEP, bleInfo } from '../../data/lampType.js' import { handle } from '../../utils/mjs_handle.js' import { calculateColor } from '../../utils/mjs_color.js' import light_cmd from '../../utils/light_cmd.js' import { _curry } from '../../utils/mjs_curry.js' import BLE from '../../utils/mjs_wxble.js' console.log(BLE); const ble = new BLE(); /** mock 数据 */ const mock_bleDevs = [ { id: 1, name: "123", deviceId: "1234" }, { id: 2, name: "设备2", deviceId: "23456" } ] const mock_bleServices = [ { uuid: "123", isPrimary: true, characteristics: [ { uuid: "123", properties: { read: true, write: true, notify: true, indicate: true, } } ] }, { uuid: "1234", characteristics: [ { uuid: "12345", properties: { read: true, write: true, notify: true, indicate: true, } }, { uuid: "123456", properties: { read: true, write: true, notify: true, indicate: true, } } ] } ] // 蓝牙模块连接对象信息参数 const connectBleParams = []; // 蓝牙服务信息 let bleServices = []; Page({ /** * 页面的初始数据 */ data: { // unConnect scaning connecting connected connectStateTypes: connectStateTypes, ble: { state: connectStateTypes.unConnect, devName: "123", deviceId: "", init: false, // 控制uuid controlUuid: "", // 监听数据uuid notifyUuid: "", }, bleDevs: mock_bleDevs, bleServices: mock_bleServices, }, searchDeviceHandle() { this.excuteSearchDevice(); }, disconnectHandle(){ this.excuteDisconnect(); }, async excuteDisconnect(){ console.log('断开连接'); if(this.data.ble.state !== connectStateTypes.connected){ return this.bleFail('断开连接失败', '当前未连接设备'); } let err,res; [err,res] = await handle(ble.disconnectDev(this.data.ble.deviceId)); if(err){ return this.bleFail('断开连接失败', err.errMsg); } wx.showToast({title: `断开连接成功`, }) this.initPageInfo(); }, // 获取蓝牙发送数据对象 excuteBleSend(data){ console.log('获取蓝牙发送数据对象'); // 固定值 let controlUuid = bleInfo.controlUuid; let characteristicFirstUuid = bleInfo.characteristicFirstUuid; let deviceId = this.data.ble.deviceId; let serverUuid = ''; let characteristicUuid = ''; let characteristic; let server = bleServices.find(s=>{ console.log(`服务uuid ${s.uuid}`); console.log(s); return s.uuid.startsWith(controlUuid); }); if(!server) { serverUuid = bleInfo.controlUuid; }else { serverUuid = server.uuid; characteristic = server.characteristics.find(c=> c.uuid.startsWith(characteristicFirstUuid) ); } //获取 if(!characteristic) { // return bleFail('获取服务失败', '该设备不支持该程序'); characteristicUuid = bleInfo.controlUuid; } else { characteristicUuid = characteristic.uuid; } console.log(`服务uuid ${serverUuid} 特征uuid ${characteristicUuid}`); return ble.sendData(deviceId, serverUuid, characteristicUuid, data); }, initPageInfo(){ this.setData({ ble: {...this.data.ble, state: connectStateTypes.unConnect, init: false, devName: "", deviceId: "", }, bleDevs: [], bleServices: [] }); bleServices = []; }, // 连接失败 bleFail(title,msg){ wx.showModal({ title: title, content: msg, success: (res) =>{ this.initPageInfo(); } }); }, async excuteSearchDevice(){ console.log("搜索 蓝牙设备"); let err,res; if(!this.data.ble.init){ [err,res] = await handle(ble.initBle()); if(err){ return this.bleFail('蓝牙初始化失败', err.errMsg); } ble.onSearch = this.onSearchHandle; } this.setData({ ble: {...this.data.ble, state: connectStateTypes.scaning, init: true }, bleDevs: [] }); ble.search(this.onSearchHandle); }, // 搜索到设备基础函数 onSearchHandle(device){ console.log('搜索到设备'); if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){ return console.log('设备再次被搜索到'); } console.log(device); device.id = device.deviceId; let arr = this.data.bleDevs; arr.push(device); this.setData({ bleDevs: arr }) }, // 连接设备 async connectDevHandle(e) { console.log("click connectDev") let err,res; let dev = e.detail; console.log(dev); this.setData({ ble: {...this.data.ble,state: connectStateTypes.connecting, devName: dev.name, deviceId: dev.deviceId }, }); // 停止搜索 [err,res] = await handle(ble.stopSearch()); if(err){ return this.bleFail('停止搜索失败', err.errMsg); } wx.showToast({title: `连接${dev.name}中...`, }) // 连接至设备 [err,res] = await handle(ble.connectDev(dev.deviceId)); if(err){ return this.bleFail('连接失败', err.errMsg);} // 连接成功 this.setData({ ble:{...this.data.ble, state:connectStateTypes.connected } }); // 设备服务管理 this.connectServices(dev.deviceId); }, async connectServices(deviceId){ let [err,res] = await handle(ble.getBleServices(deviceId)); if(err){ return this.bleFail('获取服务失败', err.errMsg);} // 保存服务信息; bleServices = res; this.setData({ bleServices: res }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { // 配置导航栏 wx.setNavigationBarTitle({ title: '蓝牙工具箱' }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })