123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- const app = getApp();
- let xBlufi = require("../../../utils/blufi/xBlufi.js");
- import strings from '../../../utils/strings.js';
- import lexin_devices from '../../../utils/lexin/devices';
- import junzhengUtil from '../../../utils/junzheng/util';
- import routeUtil from '../../../utils/routeUtil.js';
- import routePath from '../../../utils/routePath.js';
- Page({
- data: {
- nvabarData: {
- showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
- title: '连接配网', //导航栏 中间的标题
- },
- devicesList: [],
- searching: false,
- version: '2.0',
- name: '',
- connectedDeviceId: '',
- connected: true,
- deviceInfo: null,
- isInitOK: false,
- customData: '',
- isSubmit: false,
- islanya: false,
- imageUrl: "./../../../img/f.png",
- connectDevice: {},
- },
- onLoad(options) {
- console.log("开始搜索")
- const that = this;
- var param = options.param;
- if (!strings.isEmpty(param)) {
- param = JSON.parse(options.param);
- that.data.connectDevice = param;
- if (!strings.isEmpty(param.typeList)) {
- var typeList = param.typeList;
- typeList = typeList.filter((v) => v.connectType == 3);
- if (!strings.isEmpty(typeList)) {
- that.setData({
- imageUrl: typeList[0].deviceLinkResp.icon2,
- });
- }
- }
- }
- if (app.globalData.oneInitBluetooth) {
- app.globalData.oneInitBluetooth = false;
- xBlufi.initXBlufi(1);
- };
- xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
- that.search();
- },
- ///停止搜索
- stopSearch: function () {
- xBlufi.notifyStartDiscoverBle({
- 'isStart': false,
- });
- },
- ///开始或结束,也可以过滤名称
- search: function () {
- xBlufi.notifyStartDiscoverBle({
- 'isStart': true,
- });
- },
- // {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}}
- // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
- // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}},{"deviceId":"42:E0:22:C2:6E:25","name":"","RSSI":-92,"connectable":true,"advertisData":"4c001007221fa8d7a84ba8","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
- funListenDeviceMsgEvent: function (options) {
- var that = this;
- switch (options.type) {
- //发现设备列表回调开始
- case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
- if (!options.result) {
- console.log("蓝牙未开启 fail =》", options)
- wx.showToast({
- title: '蓝牙未开启',
- icon: 'none'
- })
- } else {
- //蓝牙搜索开始
- that.setData({
- searching: true
- });
- }
- break;
- // [{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0","RSSI":-62,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fd3af0","serviceData":{}}]
- ///发现设备列表回调
- case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
- // 搜索到蓝牙
- if (options.result) {
- var devicesList = options.data.filter((v) => {
- var name = v.name;
- var isLexin = name.indexOf(lexin_devices.leXin) !== -1;
- var isJungzheng1 = junzhengUtil.isJungzheng1(name);
- var isJungzheng2 = junzhengUtil.isJungzheng2(name);
- return isLexin || isJungzheng1 || isJungzheng2;
- // var localName = v.localName;
- // var isLexin1 = localName.indexOf(lexin_devices.leXin) !== -1;
- // var isJungzheng3 = junzhengUtil.isJungzheng1(localName);
- // var isJungzheng4 = junzhengUtil.isJungzheng2(localName);
- // return isLexin || isJungzheng1 || isJungzheng2 || isLexin1 || isJungzheng3 || isJungzheng4;
- ///2AXD
- // return v.name == "AIrSMArT_d0311004155e";
- ///X4
- // return v.name == "AIrSMArT_861210052356337";
- });
- if (!strings.isEmpty(devicesList)) {
- console.log("打印4", JSON.stringify(devicesList))
- ///过滤乐鑫设备
- that.setData({
- islanya: true,
- devicesList: devicesList,
- });
- }
- }
- break;
- // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
- // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
- ///主动去连接连接
- case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
- wx.hideLoading();
- if (options.result) {
- that.setData({
- islanya: false,
- devicesList: [],
- });
- var p = JSON.stringify(that.data.connectDevice);
- var param = '?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "¶m=" + p;
- routeUtil.jumpParam(routePath.deviceConnect3, param);
- } else {
- wx.showModal({
- title: '提示',
- content: '连接失败',
- showCancel: false
- });
- }
- break;
- ///停止发现设备列表回调
- case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
- if (options.result) {
- //蓝牙停止搜索ok
- console.log('蓝牙停止搜索ok')
- } else {
- //蓝牙停止搜索失败
- console.log('蓝牙停止搜索失败')
- }
- that.setData({
- searching: false
- });
- break;
- }
- },
- // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}]
- async Connect(e) {
- var that = this;
- const devicesList = that.data.devicesList;
- if (devicesList.length < 1) {
- wx.showToast({
- title: '请短按设备配对键',
- icon: 'none',
- duration: 2000
- });
- return;
- };
- if (that.data.isSubmit) {
- return;
- };
- that.data.isSubmit = true;
- // 停止搜索
- that.stopSearch();
- var name = devicesList[0].name;
- var deviceId = devicesList[0].deviceId;
- var isJungzheng1 = junzhengUtil.isJungzheng1(name);
- var isJungzheng2 = junzhengUtil.isJungzheng2(name);
- if (isJungzheng1 || isJungzheng2) {
- setTimeout(() => {
- wx.hideLoading();
- that.setData({
- islanya: false,
- devicesList: [],
- });
- var param = '?deviceId=' + deviceId + '&name=' + name + "¶m=" + JSON.stringify(that.data.connectDevice);
- routeUtil.jumpParam(routePath.deviceConnect4, param);
- }, 500);
- return;
- }
- //开始连接
- var name = devicesList[0].name;
- // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
- xBlufi.notifyConnectBle({
- isStart: true,
- name: name,
- deviceId: deviceId,
- });
- wx.showLoading({
- title: '请稍后',
- });
- },
- getBluetoothStatus() {
- app.getBluetoothStatus();
- },
- notRoter() {
- wx.navigateBack({
- delta: 1
- });
- },
- onShow: function () {
- var that = this;
- if (that.data.isSubmit) {
- that.data.isSubmit = false;
- that.search();
- }
- },
- onUnload: function () {
- var that = this;
- xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
- },
- })
|