|
@@ -1,8 +1,11 @@
|
|
|
// index.js
|
|
|
// 获取应用实例
|
|
|
const getBle = require("../../utils/ble");
|
|
|
+const handle = require("../../utils/handle");
|
|
|
+const sleep = require("../../utils/sleep");
|
|
|
const app = getApp();
|
|
|
const ble = getBle();
|
|
|
+
|
|
|
const connectStateMap = {
|
|
|
0: {text:'未连接',color:"red"},
|
|
|
1: {text:'扫描中',color:"greenYellow"},
|
|
@@ -15,6 +18,57 @@ const bleStateEnum = {
|
|
|
connecting: 2,
|
|
|
connected: 3,
|
|
|
}
|
|
|
+
|
|
|
+const bleServerMap = {
|
|
|
+ control: {
|
|
|
+ uuid:'8000CC00-CC00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
+ text: '摄像控制服务',
|
|
|
+ listen: {
|
|
|
+ controlTip: '0000CC01',
|
|
|
+ controlType: '0000CC02',
|
|
|
+ },
|
|
|
+ code:{
|
|
|
+
|
|
|
+ }
|
|
|
+ },
|
|
|
+ location : {
|
|
|
+ uuid:'8000DD00-DD00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
+ text: '相机定位服务'
|
|
|
+ },
|
|
|
+ remote : {
|
|
|
+ uuid:'8000FF00-FF00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
+ text: '摄像机远程控制服务',
|
|
|
+ code:{
|
|
|
+ hold:{
|
|
|
+ firstUuid: '0000FF01',
|
|
|
+ command: {
|
|
|
+ cmd1:'0106',//SHUTTER_RELEASED
|
|
|
+ cmd2:'0107',//PRESS_TO_FOCUS
|
|
|
+ cmd3:'0108',//HOLD_FOCUS
|
|
|
+ cmd4:'0109',//TAKE_PICTURE
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ pariring : {
|
|
|
+ uuid:'8000EE00-EE00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
+ text: '相机配对服务',
|
|
|
+ code: {
|
|
|
+ pariring: {
|
|
|
+ firstUuid: '0000EE01',
|
|
|
+ command: {
|
|
|
+ // 配对
|
|
|
+ connect: '06 08 01 00 00 00',
|
|
|
+ // 取消配对
|
|
|
+ disConnect: '06 08 02 00 00 00',
|
|
|
+ // 关机
|
|
|
+ poweroff:'03 08 13'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+}
|
|
|
Page({
|
|
|
data: {
|
|
|
connectStateMap,
|
|
@@ -61,6 +115,176 @@ Page({
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
+ },
|
|
|
+ async holdFoucus(){
|
|
|
+ console.log("发送对焦命令");
|
|
|
+ },
|
|
|
+ async connectDev(e){
|
|
|
+ let devId = e.currentTarget.dataset.dev;
|
|
|
+ let dev = this.data.bleDevs.find(dev=>devId===dev.deviceId);
|
|
|
+ let err,res;
|
|
|
+ if(!dev){
|
|
|
+ return wx.showToast({
|
|
|
+ title: '数据库异常,无法找寻设备',
|
|
|
+ })
|
|
|
+ }
|
|
|
+ console.log(dev);
|
|
|
+ // 连接设备
|
|
|
+ this.setData({
|
|
|
+ ble: {...this.data.ble,state: bleStateEnum.connecting},
|
|
|
+ });
|
|
|
+ [err,res] = await handle(ble.stopSearch());
|
|
|
+ if(err){return wx.showToast({ title: '关闭微信失败', })}
|
|
|
+ wx.showToast({
|
|
|
+ title: `尝试${dev.name}中...`,
|
|
|
+ })
|
|
|
+ [err,res] = await handle(ble.connectDev(devId));
|
|
|
+ if(err){
|
|
|
+ return wx.showToast({
|
|
|
+ title: '连接失败'+err.errMsg,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ console.log('设备连接成功');
|
|
|
+ console.log(res);
|
|
|
+ // 然后呢?
|
|
|
+ this.setData({
|
|
|
+ ble:{...this.data.ble,state:bleStateEnum.connected}
|
|
|
+ });
|
|
|
+ // 获取服务与特征
|
|
|
+ [err,res] = await handle(ble.getBleServices(devId));
|
|
|
+ if(err){
|
|
|
+ wx.showToast({
|
|
|
+ title: '搜索蓝牙服务失败',
|
|
|
+ });
|
|
|
+ return console.log(err);
|
|
|
+ }
|
|
|
+ console.log(res);
|
|
|
+ res.forEach(async val=>{
|
|
|
+ console.log('******');
|
|
|
+ console.log(val.uuid);
|
|
|
+ let _err,_res;
|
|
|
+ if(val.uuid === bleServerMap.pariring.uuid ){
|
|
|
+ // 尝试直接向设备发起连接
|
|
|
+ console.log('设备配对服务');
|
|
|
+ // 获取特征值id
|
|
|
+ let code_pariring = bleServerMap.pariring.code.pariring
|
|
|
+ let characteristic = val.characteristics.find(c=>c.uuid.startsWith(code_pariring.firstUuid));
|
|
|
+ if(!characteristic){return console.log('无法配对')}
|
|
|
+ [_err,_res] = await handle(ble.sendData(
|
|
|
+ devId,
|
|
|
+ val.uuid,
|
|
|
+ characteristic.uuid,
|
|
|
+ code_pariring.command.connect.replace(/\s/g,'')
|
|
|
+ ));
|
|
|
+ if(_err){
|
|
|
+ wx.showToast({
|
|
|
+ title: 'Err:发送蓝牙数据失败??',
|
|
|
+ })
|
|
|
+ // 尝试关闭设备
|
|
|
+ setTimeout(async ()=>{
|
|
|
+ console.log('发送关机命令');
|
|
|
+ [_err,_res] = await handle(ble.sendData(
|
|
|
+ devId,
|
|
|
+ val.uuid,
|
|
|
+ characteristic.uuid,
|
|
|
+ code_pariring.command.disConnect.replace(/\s/g,'')
|
|
|
+ ));
|
|
|
+ if(_err){
|
|
|
+ console.error(_err)
|
|
|
+ wx.showToast({
|
|
|
+ title: 'Err:发送设备关机命令失败??',
|
|
|
+ })
|
|
|
+ } else{
|
|
|
+ wx.showToast({
|
|
|
+ title: '发送关机命令成功??',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },10000);
|
|
|
+
|
|
|
+ console.log(_err);
|
|
|
+ }
|
|
|
+ console.log(res);
|
|
|
+ wx.showToast({
|
|
|
+ title: '发送配对指令成功',
|
|
|
+ })
|
|
|
+ }else if(val.uuid === bleServerMap.control.uuid){
|
|
|
+ let listenCode = bleServerMap.control.listen;
|
|
|
+ // [_err,_res] = await handle(ble.listenData(devId,val.uuid,listenCode.controlTip));
|
|
|
+ // console.log(_res);
|
|
|
+ // [_err,_res] = await handle(ble.listenData(devId,val.uuid,listenCode.controlTip));
|
|
|
+ // console.log(_res);
|
|
|
+ // if(_err){
|
|
|
+ // console.log('控制端口监听失败');
|
|
|
+ // console.error(_err);
|
|
|
+ // console.log('控制端口监听失败');
|
|
|
+ // }
|
|
|
+ // console.log('控制端口监听完成')
|
|
|
+ }else if(val.uuid === bleServerMap.pariring.uuid ){
|
|
|
+ // 尝试直接向设备发起连接
|
|
|
+ console.log('设备配对服务');
|
|
|
+ // 获取特征值id
|
|
|
+ let code = bleServerMap.remote.code.hold
|
|
|
+ let characteristic = val.characteristics.find(c=>c.uuid.startsWith(code.firstUuid));
|
|
|
+ if(!characteristic){return console.log('无法配对')}
|
|
|
+ [_err,_res] = await handle(ble.sendData(
|
|
|
+ devId,
|
|
|
+ val.uuid,
|
|
|
+ characteristic.uuid,
|
|
|
+ code.command.cmd3.replace(/\s/g,'')
|
|
|
+ ));
|
|
|
+ await sleep(10);
|
|
|
+ [_err,_res] = await handle(ble.sendData(
|
|
|
+ devId,
|
|
|
+ val.uuid,
|
|
|
+ characteristic.uuid,
|
|
|
+ code.command.cmd1.replace(/\s/g,'')
|
|
|
+ ));
|
|
|
+
|
|
|
+ if(_err){
|
|
|
+ wx.showToast({
|
|
|
+ title: 'Err:发送快门控制命令失败',
|
|
|
+ })
|
|
|
+ // 尝试关闭设备
|
|
|
+ setTimeout(async ()=>{
|
|
|
+ console.log('发送释放快门命令');
|
|
|
+ [_err,_res] = await handle(ble.sendData(
|
|
|
+ devId,
|
|
|
+ val.uuid,
|
|
|
+ characteristic.uuid,
|
|
|
+ code.command.cmd2.replace(/\s/g,'')
|
|
|
+ ));
|
|
|
+ if(_err){
|
|
|
+ console.error(_err)
|
|
|
+ wx.showToast({
|
|
|
+ title: 'Err:发送按住快门命令失败??',
|
|
|
+ })
|
|
|
+ } else{
|
|
|
+ wx.showToast({
|
|
|
+ title: '发送按住快门命令成功??',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },20000);
|
|
|
+
|
|
|
+ console.log(_err);
|
|
|
+ }
|
|
|
+ console.log(res);
|
|
|
+ wx.showToast({
|
|
|
+ title: '发送按住快门指令成功',
|
|
|
+ })
|
|
|
+ }
|
|
|
+ console.log('******');
|
|
|
+ console.log(val.uuid);
|
|
|
+ val.characteristics.forEach(c=>{
|
|
|
+ console.log(c);
|
|
|
+ });
|
|
|
+ // 查看是否会有对应特征值
|
|
|
+ console.log('******');
|
|
|
+ })
|
|
|
+ wx.showToast({
|
|
|
+ title: '蓝牙服务搜索成功',
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
},
|
|
|
// 事件处理函数
|
|
|
bindViewTap() {
|
|
@@ -78,7 +302,7 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
this.setData({
|
|
|
- ble: {...this.data.ble,state: 1},
|
|
|
+ ble: {...this.data.ble,state: bleStateEnum.searching},
|
|
|
});
|
|
|
ble.search(this.onSearchHandle);
|
|
|
},
|
|
@@ -114,25 +338,4 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
- getUserProfile(e) {
|
|
|
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
|
|
|
- wx.getUserProfile({
|
|
|
- desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
|
|
- success: (res) => {
|
|
|
- console.log(res)
|
|
|
- this.setData({
|
|
|
- userInfo: res.userInfo,
|
|
|
- hasUserInfo: true
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
- getUserInfo(e) {
|
|
|
- // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
|
|
|
- console.log(e)
|
|
|
- this.setData({
|
|
|
- userInfo: e.detail.userInfo,
|
|
|
- hasUserInfo: true
|
|
|
- })
|
|
|
- }
|
|
|
})
|