123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- const app = getApp();
- let xBlufi = require("../../utils/blufi/xBlufi.js");
- import strings from '../../utils/strings.js';
- import lexin_devices from '../../utils/lexin/devices';
- import junzheng_util from '../../utils/junzheng/util';
- Page({
- data: {
- nvabarData: {
- showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
- title: '连接配网', //导航栏 中间的标题
- },
- devicesList: [],
- searching: false,
- version: '2.0',
- name: '',
- connectedDeviceId: '',
- connected: true,
- deviceInfo: null,
- isInitOK: false,
- customData: '',
- isOne: true,
- isSubmit: false,
- islanya: false,
- isOnBluetooth: false,
- imageUrl: "./../../img/f.png",
- connectDevice: {},
- },
- onLoad(options) {
- 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,
- });
- }
- }
- }
- const isOne = setTimeout(() => {
- clearTimeout(isOne);
- if (that.data.isOne) {
- that.setData({
- isOne: false,
- });
- }
- }, 2000);
- if (app.globalData.oneInitBluetooth) {
- app.globalData.oneInitBluetooth = false;
- xBlufi.initXBlufi(1);
- };
- xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
- that.Search();
- },
- onShow() {
- var that = this;
- if (!that.data.isOne) {
- that.Search();
- }
- },
- ///开始或结束,也可以过滤名称
- 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) {
- const 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 = junzheng_util.isJungzheng1(name);
- var isJungzheng2 = junzheng_util.isJungzheng2(name);
- return isLexin || isJungzheng1 || isJungzheng2;
- });
- if (!strings.isEmpty(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) {
- if (that.data.isOnBluetooth) {
- return;
- };
- that.setData({
- isOnBluetooth: true,
- });
- setTimeout(() => {
- that.setData({
- isSubmit: false,
- islanya: false,
- devicesList: [],
- isOnBluetooth: false,
- });
- var param = JSON.stringify(that.data.connectDevice);
- wx.navigateTo({
- url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "¶m=" + param,
- });
- }, 200);
- } 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.setData({
- isSubmit: true
- });
- // 停止搜索
- xBlufi.notifyStartDiscoverBle({
- 'isStart': false,
- });
- //开始连接
- let name = devicesList[0].name;
- // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
- xBlufi.notifyConnectBle({
- isStart: true,
- deviceId: devicesList[0].deviceId,
- name
- });
- wx.showLoading({
- title: '请稍后',
- });
- },
- getBluetoothStatus() {
- app.getBluetoothStatus();
- },
- notRoter() {
- wx.navigateBack({
- delta: 1
- });
- },
- onUnload: function () {
- var that = this;
- xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
- },
- })
|