|
@@ -24,11 +24,16 @@ Page({
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
+ // unConnect scaning connecting connected
|
|
|
connectStateTypes: connectStateTypes,
|
|
|
ble: {
|
|
|
state: connectStateTypes.scaning,
|
|
|
- devName: "123",
|
|
|
- init: false
|
|
|
+ devName: "",
|
|
|
+ init: false,
|
|
|
+ // 控制uuid
|
|
|
+ controlUuid: "",
|
|
|
+ // 监听数据uuid
|
|
|
+ notifyUuid: "",
|
|
|
},
|
|
|
lamp: {
|
|
|
// 信息相关
|
|
@@ -93,43 +98,104 @@ Page({
|
|
|
this.setData({
|
|
|
lamp: {...this.data.lamp, switch: !this.data.lamp.switch}
|
|
|
});
|
|
|
+ this.setData({
|
|
|
+ lamp: {...this.data.lamp, fullOpen: false}
|
|
|
+ })
|
|
|
this.reComputeBgColor();
|
|
|
},
|
|
|
|
|
|
+ bleFail(title,msg){
|
|
|
+ this.setData({
|
|
|
+ ble: {...this.data.ble,
|
|
|
+ state: connectStateTypes.unConnect,
|
|
|
+ init: false
|
|
|
+ },
|
|
|
+ bleDevs: []
|
|
|
+ });
|
|
|
+ wx.showModal({
|
|
|
+ title: title,
|
|
|
+ content: msg,
|
|
|
+ success (res) { }
|
|
|
+ })
|
|
|
+ },
|
|
|
// 蓝牙控制板块
|
|
|
searchDeviceHandle() {
|
|
|
// 开始搜索设备
|
|
|
this.excuteSearchDevice();
|
|
|
},
|
|
|
- connectDevHandle(e) {
|
|
|
- // 连接设备
|
|
|
- console.log("click connectDev")
|
|
|
- console.log(e);
|
|
|
- let dev = e.detail;
|
|
|
- console.log(dev);
|
|
|
- },
|
|
|
+
|
|
|
async excuteSearchDevice(){
|
|
|
console.log("搜索 蓝牙设备");
|
|
|
let err,res;
|
|
|
if(!this.data.ble.init){
|
|
|
[err,res] = await handle(ble.initBle());
|
|
|
if(err){
|
|
|
- return wx.showModal({
|
|
|
- title: '蓝牙错误',
|
|
|
- content: "无法启用蓝牙!!!",
|
|
|
- success (res) { }
|
|
|
- })
|
|
|
+ return this.bleFail('蓝牙初始化失败', err.errMsg);
|
|
|
}
|
|
|
ble.onSearch = this.onSearchHandle;
|
|
|
- this.setData({
|
|
|
- ble: {...this.data.ble, init: true},
|
|
|
- })
|
|
|
}
|
|
|
this.setData({
|
|
|
- ble: {...this.data.ble,state: connectStateTypes.searching},
|
|
|
+ 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();
|
|
|
+ },
|
|
|
+
|
|
|
+ async connectServices(){
|
|
|
+ // uuid FF01
|
|
|
+ let [err,res] = await handle(ble.getServices());
|
|
|
+ if(err){ return this.bleFail('获取服务失败', err.errMsg);}
|
|
|
+ res.forEach(val=>{
|
|
|
+ // 不同的服务有不同的功能
|
|
|
+ console.log(val);
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
|
|
|
|
|
|
|