123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- // 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() {
-
- }
- })
|