123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- // index.js
- // 获取应用实例
- const getBle = require("../../utils/ble");
- const handle = require('../../utils/handle.js');
- 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:{
- }
- },
- 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
- }
- }
- }
- },
- 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'
- }
- }
- }
- },
- }
- 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");
- console.log(handle);
- console.log(handle);
- console.log(handle);
- console.log(handle);
- console.log(handle);
- let [err,res] = await handle(ble.initBle());
- if(err){throw err}
- ble.onSearch = this.onSearchHandle;
- this.setData({
- ble: {...this.data.ble,init: true},
- })
- } catch (error) {
- console.log(error);
-
- wx.showModal({
- title: '初始化蓝牙失败',
- content: error.errMsg,
- success (res) {
- if (res.confirm) {
- console.log('用户点击确定')
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- this.setData({
- ble: {...this.data.ble,init: false},
- })
- }
- },
- async holdFoucus(){
- console.log("发送对焦命令");
- },
- /**
- *
- * @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);
- }
- },
- 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();
- }
- // 记录当前时间. 等抬起时再次获取
- },
- 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)
- }
-
- }
- },
- 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
- })
- }
- },
- 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('设备连接成功');
- 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
- })
- },
- 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
- })
- }
- },
- })
|