|
@@ -1,469 +1,92 @@
|
|
|
-// index.js
|
|
|
-// 获取应用实例
|
|
|
-const getBle = require("../../utils/ble");
|
|
|
-const handle = require("../../utils/handle");
|
|
|
-const sleep = require("../../utils/sleep");
|
|
|
-const TimeClock = require('../../utils/timer');
|
|
|
-const app = getApp();
|
|
|
-const ble = getBle();
|
|
|
-
|
|
|
-let services = [];
|
|
|
-const timeClock = new TimeClock();
|
|
|
-const connectStateMap = {
|
|
|
- 0: {text:'未连接',color:"red"},
|
|
|
- 1: {text:'扫描中',color:"greenYellow"},
|
|
|
- 2: {text:'连接中',color:"#d8e84d"},
|
|
|
- 3: {text:'已连接',color:"#00ff00"}
|
|
|
-}
|
|
|
-const bleStateEnum = {
|
|
|
- notConnect: 0,
|
|
|
- searching: 1,
|
|
|
- connecting: 2,
|
|
|
- connected: 3,
|
|
|
-}
|
|
|
-
|
|
|
-const bleServerMap = {
|
|
|
- control: {
|
|
|
- uuid:'8000CC00-CC00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
- text: '摄像控制服务',
|
|
|
- listen: {
|
|
|
- controlTip: '0000CC01',
|
|
|
- controlType: '0000CC02',
|
|
|
- },
|
|
|
- code:{
|
|
|
+// pages/index/index.js
|
|
|
+import { loadSonyItem } from '../../api/sony.js'
|
|
|
+import { handle } from '../../utils/handle.js'
|
|
|
+Page({
|
|
|
|
|
|
- }
|
|
|
- },
|
|
|
- location : {
|
|
|
- uuid:'8000DD00-DD00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
- text: '相机定位服务'
|
|
|
- },
|
|
|
- remote : {
|
|
|
- uuid:'8000FF00-FF00-FFFF-FFFF-FFFFFFFFFFFF',
|
|
|
- text: '摄像机远程控制服务',
|
|
|
- code:{
|
|
|
- hold:{
|
|
|
- firstUuid: '0000FF01',
|
|
|
- command: {
|
|
|
- cmd1:'0107',//Focus Down
|
|
|
- shutterDown:'0109',//PRESS_TO_FOCUS
|
|
|
- shutterUp:'0108',//HOLD_FOCUS
|
|
|
- cmd4:'0106',//Focus Up
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 页面的初始数据
|
|
|
+ */
|
|
|
+ data: {
|
|
|
+ sonyItems: []
|
|
|
},
|
|
|
- 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'
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面加载
|
|
|
+ */
|
|
|
+ async onLoad(options) {
|
|
|
+ // 获取索尼功能实现清单
|
|
|
+ let err,sonyItems;
|
|
|
+ [err,sonyItems] = await loadSonyItem()
|
|
|
+ if(err){
|
|
|
+ console.log(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(sonyItems)
|
|
|
+ this.setData({
|
|
|
+ sonyItems: sonyItems
|
|
|
+ })
|
|
|
},
|
|
|
|
|
|
-}
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面初次渲染完成
|
|
|
+ */
|
|
|
+ onReady() {
|
|
|
|
|
|
-let isOnceTimer = null;
|
|
|
-let isLongClick = false;// 是否为长按
|
|
|
-let isBulbState = false;// 是否处于b门模式下
|
|
|
-Page({
|
|
|
- data: {
|
|
|
- connectStateMap,
|
|
|
- bleStateEnum,
|
|
|
- isDebug: false,
|
|
|
- ble:{
|
|
|
- state: 0,
|
|
|
- init: false,
|
|
|
- },
|
|
|
- remoteDev:{
|
|
|
- connect: false,
|
|
|
- devName: "",
|
|
|
- devId: '',
|
|
|
- shutter: false,
|
|
|
- },
|
|
|
- bleDevs:[],// 搜索到的蓝牙设备列表
|
|
|
- motto: 'Hello World',
|
|
|
- userInfo: {},
|
|
|
- hasUserInfo: false,
|
|
|
- shutterBtnY1: 0,
|
|
|
- isHolder: false,
|
|
|
- time_minute: 0,// 分
|
|
|
- time_second: 0,// 秒
|
|
|
- time_ms: 0,// 毫秒
|
|
|
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
|
|
|
- canIUseGetUserProfile: false,
|
|
|
- canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
|
|
|
},
|
|
|
- async onShow(){
|
|
|
- timeClock.setHook((msNum)=>{
|
|
|
- // console.log(msNum);
|
|
|
- let res = timeClock.parse(msNum)
|
|
|
- // console.log(res);
|
|
|
- this.setData({
|
|
|
- time_minute:res.minute,
|
|
|
- time_second:res.second,
|
|
|
- time_ms:res.ms,
|
|
|
- })
|
|
|
- });
|
|
|
- // 初始化蓝牙
|
|
|
-
|
|
|
- try {
|
|
|
- console.log("init ble");
|
|
|
- await ble.initBle();
|
|
|
- ble.onSearch = this.onSearchHandle;
|
|
|
- this.setData({
|
|
|
- ble: {...this.data.ble,init: true},
|
|
|
- })
|
|
|
- } catch (error) {
|
|
|
- console.log(error);
|
|
|
- this.setData({
|
|
|
- ble: {...this.data.ble,init: false},
|
|
|
- })
|
|
|
- wx.showModal({
|
|
|
- title: '初始化蓝牙失败',
|
|
|
- content: error.errMsg,
|
|
|
- success (res) {
|
|
|
- if (res.confirm) {
|
|
|
- console.log('用户点击确定')
|
|
|
- } else if (res.cancel) {
|
|
|
- console.log('用户点击取消')
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面显示
|
|
|
+ */
|
|
|
+ onShow() {
|
|
|
+ // 配置导航栏
|
|
|
+ wx.setNavigationBarTitle({
|
|
|
+ title: '蓝牙工具箱'
|
|
|
+ })
|
|
|
},
|
|
|
- async holdFoucus(){
|
|
|
- console.log("发送对焦命令");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生命周期函数--监听页面隐藏
|
|
|
+ */
|
|
|
+ onHide() {
|
|
|
+
|
|
|
},
|
|
|
+
|
|
|
/**
|
|
|
- *
|
|
|
- * @param {*} isDown 按下或者抬起快门
|
|
|
- * @param {*} isBulb 是否为b门模式下释放快门
|
|
|
+ * 生命周期函数--监听页面卸载
|
|
|
*/
|
|
|
- async devControlShutter(isDown = true,isBulb = false){
|
|
|
- let cmds,waitTime=10;
|
|
|
- let code = bleServerMap.remote.code.hold;
|
|
|
- let dev = this.data.bleDevs.find(dev=>this.data.remoteDev.devId===dev.deviceId);
|
|
|
- let server = services.find(s=>s.uuid === bleServerMap.remote.uuid);
|
|
|
- if(!server){console.log('无法找到远程控制服务')}
|
|
|
- let characteristic = server.characteristics.find(c=>c.uuid.startsWith(code.firstUuid));
|
|
|
- if(!characteristic){return console.log('无法匹配到特征')}
|
|
|
- if(isDown){
|
|
|
- console.log('快门抬起')
|
|
|
- // 发送抬起快门
|
|
|
- cmds =[
|
|
|
- code.command.shutterUp,
|
|
|
- ]
|
|
|
- if(isBulb){
|
|
|
- // b 门模式下需要再次按下快门在进行释放
|
|
|
- cmds.push(code.command.shutterDown);
|
|
|
- cmds.push(code.command.shutterUp);
|
|
|
- }
|
|
|
- }else{
|
|
|
- console.log('快门按下')
|
|
|
- cmds = [code.command.shutterDown]
|
|
|
- }
|
|
|
- for (let index = 0; index < cmds.length; index++) {
|
|
|
- const cmd = cmds[index];
|
|
|
- let [_err,_res] = await handle(ble.sendData(
|
|
|
- dev.deviceId,
|
|
|
- server.uuid,
|
|
|
- characteristic.uuid,
|
|
|
- cmd.replace(/\s/g,'')
|
|
|
- ));
|
|
|
- if(_err){
|
|
|
- // console.log('发送命令失败');
|
|
|
- console.error(_err);
|
|
|
- }
|
|
|
- // console.log('发送命令成功');
|
|
|
- await sleep(waitTime);
|
|
|
- }
|
|
|
+ onUnload() {
|
|
|
+
|
|
|
},
|
|
|
- async touchStartHandle(e){
|
|
|
- // console.log("按钮按下");
|
|
|
- // console.log(e);
|
|
|
- // todo 获取当前的y信息
|
|
|
- let touche = e.touches[0];
|
|
|
- let y1 = 0;
|
|
|
- if(touche){
|
|
|
- y1 = touche.clientY;
|
|
|
- }
|
|
|
- this.data.shutterBtnY1 = y1;
|
|
|
- isLongClick = false;
|
|
|
- // 按住700毫秒后视作长按
|
|
|
- isOnceTimer = setTimeout(()=>{
|
|
|
- isLongClick = true;
|
|
|
- isOnceTimer = null;
|
|
|
- },
|
|
|
- 700);
|
|
|
- if(this.data.isHolder){
|
|
|
- // 已经按下,视作已经发送按下快门的操作
|
|
|
- return false;
|
|
|
- }else{
|
|
|
- // todo 未按下,发送按下快门的操作
|
|
|
- this.devControlShutter(false);
|
|
|
- timeClock.start();
|
|
|
- }
|
|
|
- // 记录当前时间. 等抬起时再次获取
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面相关事件处理函数--监听用户下拉动作
|
|
|
+ */
|
|
|
+ onPullDownRefresh() {
|
|
|
+
|
|
|
},
|
|
|
- async touchEndHandle(e){
|
|
|
- console.log("按钮抬起");
|
|
|
- clearTimeout(isOnceTimer);
|
|
|
- isOnceTimer = null;
|
|
|
- if(this.data.isHolder){
|
|
|
- // 按住,not do sth
|
|
|
- isBulbState = true;
|
|
|
- }else{
|
|
|
- // 抬起按钮
|
|
|
- timeClock.stop();
|
|
|
- if(isBulbState){
|
|
|
- // 处于b门模式
|
|
|
- console.log(`[b门长曝光结束] 曝光时长 ${this.data.time_minute}:${this.data.time_second}:${this.data.time_ms}`);
|
|
|
- this.devControlShutter(true,true);
|
|
|
- isBulbState = false;
|
|
|
- return false;
|
|
|
- }
|
|
|
- if(isLongClick){
|
|
|
- // 长按
|
|
|
- this.devControlShutter(true);
|
|
|
- }else{
|
|
|
- // 短按,等待500ms 后发送抬起的命令
|
|
|
- setTimeout(()=>{
|
|
|
- console.log("短按,自动跟发一条抬起快门命令");
|
|
|
- this.devControlShutter(true);
|
|
|
- },500)
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面上拉触底事件的处理函数
|
|
|
+ */
|
|
|
+ onReachBottom() {
|
|
|
+
|
|
|
},
|
|
|
- async touchMoveHandle(e){
|
|
|
- // console.log("按钮移动");
|
|
|
- let touche = e.touches[0];
|
|
|
- let y1 = this.data.shutterBtnY1;
|
|
|
- let y2 = 0;
|
|
|
- if(touche){
|
|
|
- y2 = touche.clientY;
|
|
|
- }
|
|
|
- let endY = y2 - y1;
|
|
|
- if(Math.abs(endY)<15){
|
|
|
- // console.log("移动范围太小")
|
|
|
- return false;
|
|
|
- }
|
|
|
- this.data.shutterBtnY1 = y2;
|
|
|
- if(endY < 0){
|
|
|
- console.log('+');
|
|
|
- this.setData({
|
|
|
- isHolder:true
|
|
|
- })
|
|
|
- }else{
|
|
|
- console.log('-');
|
|
|
- this.setData({
|
|
|
- isHolder:false
|
|
|
- })
|
|
|
- }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户点击右上角分享
|
|
|
+ */
|
|
|
+ onShareAppMessage() {
|
|
|
+
|
|
|
},
|
|
|
- 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: '数据库异常,无法找寻设备',
|
|
|
- })
|
|
|
+
|
|
|
+ toPage(e){
|
|
|
+ // 获取页面路径
|
|
|
+ let url = e.currentTarget.dataset.url
|
|
|
+ if(!url){
|
|
|
+ return
|
|
|
}
|
|
|
- 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('设备连接成功');
|
|
|
- this.setData({
|
|
|
- remoteDev: {
|
|
|
- ...this.data.remoteDev,
|
|
|
- devId: devId,
|
|
|
- devName: dev.name
|
|
|
- }
|
|
|
- });
|
|
|
- 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);
|
|
|
- }
|
|
|
- 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('无法配对')}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if(_err){
|
|
|
- wx.showToast({
|
|
|
- title: 'Err:发送快门控制命令失败',
|
|
|
- })
|
|
|
- // 尝试关闭设备
|
|
|
-
|
|
|
- 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: '蓝牙服务搜索成功',
|
|
|
- });
|
|
|
- services = res;
|
|
|
- },
|
|
|
- // 事件处理函数
|
|
|
- bindViewTap() {
|
|
|
wx.navigateTo({
|
|
|
- url: '../logs/logs'
|
|
|
- })
|
|
|
- },
|
|
|
- async searchDevice(){
|
|
|
- console.log("搜索 蓝牙设备");
|
|
|
- if(!this.data.ble.init){
|
|
|
- return wx.showModal({
|
|
|
- title: '蓝牙错误',
|
|
|
- content: "无法启用蓝牙!!!",
|
|
|
- success (res) { }
|
|
|
- })
|
|
|
- }
|
|
|
- this.setData({
|
|
|
- ble: {...this.data.ble,state: bleStateEnum.searching},
|
|
|
- });
|
|
|
- ble.search(this.onSearchHandle);
|
|
|
- },
|
|
|
- onSearchHandle(device){
|
|
|
- console.log('搜索到设备');
|
|
|
- if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){
|
|
|
- return console.log('设备再次被搜索到');
|
|
|
- }
|
|
|
- console.log(device);
|
|
|
- let arr = this.data.bleDevs;
|
|
|
- arr.push(device);
|
|
|
- this.setData({
|
|
|
- bleDevs:arr
|
|
|
+ url: url
|
|
|
})
|
|
|
- },
|
|
|
- tapState(){
|
|
|
- switch(this.data.ble.state){
|
|
|
- case bleStateEnum.notConnect:
|
|
|
- this.searchDevice();
|
|
|
- break;
|
|
|
- case bleStateEnum.searching:
|
|
|
- break;
|
|
|
- case bleStateEnum.connecting:
|
|
|
- break;
|
|
|
- case bleStateEnum.connected:
|
|
|
- break;
|
|
|
- }
|
|
|
- },
|
|
|
- onLoad() {
|
|
|
- if (wx.getUserProfile) {
|
|
|
- this.setData({
|
|
|
- canIUseGetUserProfile: true
|
|
|
- })
|
|
|
- }
|
|
|
- },
|
|
|
-})
|
|
|
+ }
|
|
|
+})
|