ソースを参照

蓝牙配网优化

htc 13 時間 前
コミット
1ba4a0c495
4 ファイル変更84 行追加23 行削除
  1. 2 0
      App.vue
  2. 6 0
      components/deviceBox/index.vue
  3. 59 6
      pagesMy/wifiSearch.vue
  4. 17 17
      pagesMy/wifiSet.vue

+ 2 - 0
App.vue

@@ -1,4 +1,5 @@
 <script>
+	var xBlufi = require("@/utils/blufi/xBlufi.js");
 	export default {
 		data() {
 			return {
@@ -9,6 +10,7 @@
 			
 		},
 		onLaunch: function() {
+			xBlufi.initXBlufi(1);
 		},
 		onShow: function() {
 			// if(uni.getStorageSync('userInfo')){

+ 6 - 0
components/deviceBox/index.vue

@@ -34,6 +34,7 @@
 </template>
 
 <script>
+	var xBlufi = require("@/utils/blufi/xBlufi.js");
 	export default {
 		props:{
 			item:{
@@ -75,6 +76,11 @@
 							this.$api.post('/device/unbind',{deviceId:item.id}).then(res=>{
 								if(res.data.code!==0) return this.$showToast(res.data.msg)
 								this.$showToast('解绑成功');
+								xBlufi.notifyConnectBle({
+									isStart: false,
+									deviceId: item.id,
+									name: '',
+								});
 								setTimeout(()=>{
 									this.$emit('unbindSuccess')
 								},1000)

+ 59 - 6
pagesMy/wifiSearch.vue

@@ -39,7 +39,7 @@
 	</view>
 </template>
 
-<script>
+<script>
 	var xBlufi = require("@/utils/blufi/xBlufi.js");
 	let _this = null;
 	export default {
@@ -54,25 +54,64 @@
 			}
 		},
 		onShow() {
-			this.nodevice = false;
-			this.show = false;
+			// this.nodevice = false;
+			// this.show = false;
+			// this.Search();
 		},
 		onLoad: function() {
-			this.Search();
 			_this = this;
-			xBlufi.initXBlufi(1);
+			this.Search();
+			// xBlufi.initXBlufi(1);
 			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
 		},
 		onUnload() {
 			xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
 		},
 		methods:{
+			initBluetooth() {
+				let that = this;
+				try{
+					wx.openBluetoothAdapter({
+					  success: (res) => {
+						// 监听连接状态变化
+						wx.onBLEConnectionStateChange((res2) => {
+							console.log(res2,'onBLEConnectionStateChange');
+						});
+						wx.startBluetoothDevicesDiscovery({
+						  services: ['0000FFFF-0000-1000-8000-00805F9B34FB'], // Blufi服务UUID
+						  success: (res3) => {
+							// 监听发现新设备
+							wx.onBluetoothDeviceFound((resu) => {
+								let ly = resu.devices.find(d=>d.name.indexOf('cx-')>-1);
+								if(ly){
+									that.lanya = ly;
+									that.nodevice = false;
+									that.show = true;
+								}
+							});
+						  },
+						  fail: (err2) => {
+							console.log(err2,'err2');
+						  }
+						});
+					  },
+					  fail: (err) => {
+					  	console.log(err,'初始化失败');
+					  }
+					});
+				}catch(e){
+					console.log(e,'e');
+				}
+			  },
 			funListenDeviceMsgEvent: function(options) {
 				switch (options.type) {
+					case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
+						console.log(options)
+						break;
 					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
 						if (options.result)
 							_this.devicesList = options.data;
-							let ly = _this.devicesList.find(d=>d.name=='BLUFI_DEVICE');
+							let ly = _this.devicesList.find(d=>d.name.indexOf('cx-')>-1);
 							if(ly){
 								_this.lanya = ly;
 								_this.nodevice = false;
@@ -134,6 +173,20 @@
 				}
 			},
 			connectWiFi: function() {
+				// let that = this;
+				// wx.createBLEConnection({
+				//   deviceId: that.lanya.deviceId,
+				//   success: (res) => {
+				// 	wx.navigateTo({
+				// 		url: '/pagesMy/wifiSet?deviceId=' + that.lanya.deviceId + '&name=' + that.lanya.name
+				// 	})
+				//   },
+				//   fail: (err) => {
+				// 	  console.log(err,'createBLEConnection-err');
+				//   }
+				// });
+				// return
+				
 				//停止搜索
 				xBlufi.notifyStartDiscoverBle({
 					'isStart': false

+ 17 - 17
pagesMy/wifiSet.vue

@@ -101,16 +101,16 @@
 				let that = this
 				let ssid_arry = this.wifiList;
 				switch (options.type) {
-					case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
-						that.connected = options.result
-						if (!options.result) {
-							wx.showModal({
-								title: '很抱歉提醒你!',
-								content: '小程序与设备异常断开',
-								showCancel: false, //是否显示取消按钮
-							})
-						}
-						break;
+					// case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
+					// 	that.connected = options.result
+					// 	if (!options.result) {
+					// 		wx.showModal({
+					// 			title: '很抱歉提醒你!',
+					// 			content: '小程序与设备异常断开',
+					// 			showCancel: false, //是否显示取消按钮
+					// 		})
+					// 	}
+					// 	break;
 					case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
 						wx.hideLoading();
 						if (!options.result)
@@ -128,13 +128,13 @@
 							}
 						}
 						break;
-					case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
-						wx.showModal({
-							title: '收到自定义设备数据',
-							content: `【${options.data}】`,
-							showCancel: false,
-						})
-						break;
+					// case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
+					// 	wx.showModal({
+					// 		title: '收到自定义设备数据',
+					// 		content: `【${options.data}】`,
+					// 		showCancel: false,
+					// 	})
+					// 	break;
 					case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
 						wx.hideLoading();
 						if ('' === options.data.SSID) break;