Browse Source

蓝牙调试

liuwei 1 week ago
parent
commit
3640056e40

+ 1 - 1
.hbuilderx/launch.json

@@ -20,7 +20,7 @@
         },
         {
             "openVueDevtools" : false,
-            "playground" : "custom",
+            "playground" : "standard",
             "type" : "uni-app:app-android"
         },
         {

+ 7 - 7
common/bluetooth.js

@@ -370,9 +370,10 @@ function connectDevice(device, callback = () => {}, fail = () => {}) {
             return;
         }
     }
-	const deviceIds = app.globalData.connectionState[device.mac_id].deviceId
+	const car_sn=uni.getStorageSync('car_info').car_sn
+	const deviceIds = app.globalData.connectionState[car_sn].deviceId
 	console.log(deviceIds,'deviceIds');
-	const device_name=app.globalData.connectionState[device.mac_id].deviceName
+	const device_name=app.globalData.connectionState[car_sn].deviceName
     // 创建蓝牙连接
     uni.createBLEConnection({
         deviceId: deviceIds,
@@ -637,7 +638,6 @@ function writeData(device, deviceId, data, callback = () => {}, fail = () => {})
     // buffer = common.toArrayBuffer(data.shift());
 	
     //buffer = common.toArrayBuffer(data.shift());
-	console.log()
 	var byteArray = data.shift()
 	const buffer = new ArrayBuffer(byteArray.length);
 	const dataView = new DataView(buffer);
@@ -1075,12 +1075,12 @@ function sendOTACommand(macid, callback = () => {}, fail = () => {}) {
 function executeDeviceCommand(macid, commandName, callback = () => {}, fail = () => {}) {
     console.log('test');
 	const app = getApp();
-	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == macid);
+	const deviceId =  app.globalData.connectionState[macid].deviceId
     if (deviceId == undefined) {
         fail();
         return false;
     }
-    const device = app.globalData.connectionState[deviceId].device;
+    const device = app.globalData.connectionState[macid].device;
     if (!bluetoothDeviceConfig(device)[commandName]) {
         fail();
         return false;
@@ -1134,12 +1134,12 @@ function getSensitivity(macid, callback = () => {}, fail = () => {}) {
 //设置灵敏度
 function setSensitivity(macid,type, callback = () => {}, fail = () => {}) {
 	const app = getApp();
-	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == macid);
+	const deviceId = app.globalData.connectionState[macid].deviceId
 	if (deviceId == undefined) {
 	    fail();
 	    return false;
 	}
-	const device = app.globalData.connectionState[deviceId].device;
+	const device = app.globalData.connectionState[macid].device;
 	if (!bluetoothDeviceConfig(device)[setSensitivity]) {
 	    fail();
 	    return false;

+ 17 - 17
common/bluetooth/ZXCar.js

@@ -72,8 +72,8 @@ function alterConnect(device, deviceId) {
 	// const data = [0x1F, 0x0F, ,0x5A,0x58, 0x35, 0x32, 0x30, 0x32, 0x32, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
 	console.log("校检---》")
 	let data=[ 0x1F, 0x0F]
-	const device_name=app.globalData.connectionState[device.mac_id].deviceName
-	const device_sn=stringToHexArray(device_name)
+	// const device_name=app.globalData.connectionState[device.mac_id].deviceName
+	const device_sn=stringToHexArray(device.mac_id)
 	
 	// const car_info = uni.getStorageSync('car_info')
 	// const device_sn=stringToHexArray(car_info.car_sn)
@@ -468,21 +468,21 @@ function checkUnlock(macid,type='get'){
 							writeData(macid,getNearUnlockSetCmd)
 						}, 200);
 			}else{
-				// uni.showModal({
-				// 	title: '蓝牙配对确认',
-				// 	content: '请在系统通知栏前往同意与'+macid+'设备配对',
-				// 	cancelText: '取消',
-				// 	confirmText: '已同意',
-				// 	success: function(res) {
-				// 		if (res.confirm) {
-				// 			checkUnlock(macid)
-				// 		}else{
-				// 			// uni.redirectTo({ url: '/pages/index/index' })
-				// 		}
-				// 	},
-				// 	fail: function(res) {},
-				// 	complete: function(res) {},
-				// })
+				uni.showModal({
+					title: '蓝牙配对确认',
+					content: '请在系统通知栏前往同意与'+macid+'设备配对',
+					cancelText: '取消',
+					confirmText: '已同意',
+					success: function(res) {
+						if (res.confirm) {
+							checkUnlock(macid)
+						}else{
+							uni.switchTab({ url: '/pages/index/index' })
+						}
+					},
+					fail: function(res) {},
+					complete: function(res) {},
+				})
 			}
 			
 	}

+ 10 - 0
js_sdk/wa-permission/permission.js

@@ -497,6 +497,14 @@ function getDeviceType(bluetoothClass) {
 //设备蓝牙权限是否开启
 function checkBluetoothPermission() {
     // #ifdef APP-PLUS
+	if (isIos) {
+        // iOS 平台蓝牙权限检查
+        // iOS 平台没有独立的蓝牙权限,但需要检查定位权限是否开启
+        // 因为在 iOS 上,蓝牙扫描需要定位权限
+        return judgeIosPermissionLocation();
+    } else {
+        // 安卓平台蓝牙权限检查
+        // #ifdef APP-PLUS
         // 检查蓝牙是否可用
         const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter');
         const bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -514,6 +522,8 @@ function checkBluetoothPermission() {
 
         console.log('蓝牙已启用');
         return true;
+        // #endif
+    }
     // #endif
 }
 

+ 12 - 5
pages/bluetoothUnlock/bluetoothPair.vue

@@ -56,7 +56,8 @@
 				showNotice: false,
 				// model_name:'',
 				car_sn:'',
-				car_info:{}
+				car_info:{},
+				isReconnecting: false // 新增标志位
 			}
 		},
 		onLoad(){
@@ -217,8 +218,13 @@
 								if (!res.connected &&!('lockType' in app.globalData.nearLockInfo)) {
 									// 蓝牙连接断开
 									console.log('蓝牙连接断开');
-									me.loadBluetooth()
-									// common.simpleToast('蓝牙连接断开')
+									if (!me.isReconnecting) { // 检查标志位
+										me.isReconnecting = true; // 设置标志位
+										setTimeout(() => {
+											me.loadBluetooth();
+											me.isReconnecting = false; // 重置标志位
+										}, 100);
+									}
 								} else {
 									console.log('蓝牙连接成功');
 									// uni.hideLoading();
@@ -247,8 +253,9 @@
 								// 	content: showContent,
 								// 	success: function(res) {
 								// 		if (res.confirm) {
-											
-								// 			me.loadBluetooth();
+								// 			setTimeout(()=>{
+								// 				me.loadBluetooth();
+								// 			},100)
 								// 		} else {}
 								// 	}
 								// });