Ver Fonte

蓝牙调整

liuwei há 1 semana atrás
pai
commit
86c9afef85
4 ficheiros alterados com 62 adições e 71 exclusões
  1. 13 9
      common/bluetooth.js
  2. 44 48
      common/bluetooth/ZXCar.js
  3. 4 13
      js_sdk/wa-permission/permission.js
  4. 1 1
      mixin/index.js

+ 13 - 9
common/bluetooth.js

@@ -51,24 +51,26 @@ function initBluetooth() {
     //监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification
     uni.onBLECharacteristicValueChange((res) => {
 		console.log(res,'restest1111');
-        if (app.globalData.connectionState[res.deviceId]) {
-            const device = app.globalData.connectionState[res.deviceId].device;
+		const car_sn=uni.getStorageSync('car_info').car_sn;
+        if (app.globalData.connectionState[car_sn]) {
+            const device = app.globalData.connectionState[car_sn].device;
 			console.log(res.serviceId.toUpperCase() == bluetoothDeviceConfig(device).readServiceID.toUpperCase(),'00000');
 			if (
                 res.serviceId.toUpperCase() == bluetoothDeviceConfig(device).readServiceID.toUpperCase() &&
                 res.characteristicId.toUpperCase() == bluetoothDeviceConfig(device).readID.toUpperCase()
             ) {
-                var data = bluetoothDeviceConfig(device).readData(device, res.value, app.globalData.connectionState[res.deviceId].data);
-				// console.log(data,'data--------');
+				console.log(app.globalData.connectionState[car_sn],bluetoothDeviceConfig(device),'data--------');
+                var data = bluetoothDeviceConfig(device).readData(device, res.value, app.globalData.connectionState[car_sn].data);
+				
 				if (data) {
 					const app = getApp();
-                    app.globalData.connectionState[res.deviceId].data = data;
+                    app.globalData.connectionState[car_sn].data = data;
                     if (app.globalData.characteristicStateChangeFunc[device.mac_id]) {
                         Object.keys(app.globalData.characteristicStateChangeFunc[device.mac_id]).forEach((p) =>
                             app.globalData.characteristicStateChangeFunc[device.mac_id][p](data)
                         );
-						// common.simpleToast(app.globalData.connectionState[res.deviceId].data)
-                        console.log(app.globalData.connectionState[res.deviceId].data);
+						// common.simpleToast(app.globalData.connectionState[car_sn].data)
+                        console.log(app.globalData.connectionState[car_sn].data);
                     }
                 }
             }
@@ -251,6 +253,7 @@ function findDevice(device, callback = () => {}, fail = () => {}) {
 								uni.stopBluetoothDevicesDiscovery(); //查找到蓝牙设备停止搜索
 								const car_info= uni.getStorageSync('car_info')
 								const deviceIds = data.deviceId;
+								console.log(deviceIds,'deviceIds123');
 								const deviceId = car_info.car_sn;
 								if (app.globalData.connectionState[deviceId]) {
 									app.globalData.connectionState[deviceId].device = device;
@@ -300,6 +303,7 @@ function findDevice(device, callback = () => {}, fail = () => {}) {
                         uni.stopBluetoothDevicesDiscovery();
 						const car_info= uni.getStorageSync('car_info')
                         const deviceIds = data.deviceId;
+						console.log(deviceIds,'deviceIds456');
 						const deviceId = car_info.car_sn;
 						const app = getApp();
                          // 检查全局状态中是否已经存在该设备
@@ -379,7 +383,7 @@ function connectDevice(device, callback = () => {}, fail = () => {}) {
             app.globalData.connectionState[deviceId] = {
                 device: device,
 				deviceName: device_name,
-                deviceId: deviceId,
+                deviceId: deviceIds,
                 connected: true,
                 data: {}
             };
@@ -1085,7 +1089,7 @@ function executeDeviceCommand(macid, commandName, callback = () => {}, fail = ()
     if (data) {
 		setTimeout(()=>{
 			writeData(device, deviceId, data, callback, fail);
-		},200)
+		},100)
         return true;
     }
     fail();

+ 44 - 48
common/bluetooth/ZXCar.js

@@ -94,8 +94,8 @@ function alterConnect(device, deviceId) {
 	const secondPart = data;
 	// const threePart = data;
 	// return [data]
-	// return [firstPart,secondPart]
-	return [[0x22]]
+	return [firstPart,secondPart]
+	// return [[0x22]]
 }
 
 function crc8IEEE8023(data) {
@@ -443,54 +443,49 @@ function getNearCarCmdInfo(macid,data){
 		
 }
 function checkUnlock(macid,type='get'){
-	const app = getApp();
-	let deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == macid);
-	let device = app.globalData.connectionState[deviceId]
-	console.log(app.globalData.connectionState,'app.globalData.connectionState1111');
-	console.log(device,'device------------');
-	permision.getBondedDevices(deviceId).then(res=>{
-		if(res){
+	console.log('test0000000000000');
+	const res=permision.getBondedDevices()
+	let app=getApp()
+	if(res){
 			app.globalData.nearLockCheck=true
 			common.simpleToast('感应解锁已开启,前往设置灵敏度页面')
 			uni.navigateTo({ url: '/pages/bluetoothUnlock/unlockSet' })
 			console.log('test');
 			//redirectTo 关闭当前页面 防止用户重复操作
-			
 		}else{
-				if(type==='login'){
-							//下发接近解锁指令
-							// const nearUnlockCmd=nearUnlock()
-							// console.log(nearUnlockCmd,'nearUnlockCmd');
-							// setTimeout(() => {
-							//     writeData(macid,nearUnlockCmd)
-							// }, 500);
-							
-							// 获取接近解锁指令信息
-							const getNearUnlockSetCmd=getNearUnlockSet()
-							console.log(getNearUnlockSetCmd,'getNearUnlockSetCmd');
-							setTimeout(() => {
-							    writeData(macid,getNearUnlockSetCmd)
-							}, 500);
-				}else{
-					uni.showModal({
-						title: '蓝牙配对确认',
-						content: '请在系统通知栏前往同意与'+device.deviceName+'设备配对',
-						cancelText: '取消',
-						confirmText: '已同意',
-						success: function(res) {
-							if (res.confirm) {
-								checkUnlock(macid)
-							}else{
-								// uni.redirectTo({ url: '/pages/index/index' })
-							}
-						},
-						fail: function(res) {},
-						complete: function(res) {},
-					})
-				}
-				
-		}
-	})
+			if(type==='login'){
+						//下发接近解锁指令
+						// const nearUnlockCmd=nearUnlock()
+						// console.log(nearUnlockCmd,'nearUnlockCmd');
+						// setTimeout(() => {
+						//     writeData(macid,nearUnlockCmd)
+						// }, 500);
+						
+						// 获取接近解锁指令信息
+						const getNearUnlockSetCmd=getNearUnlockSet()
+						console.log(getNearUnlockSetCmd,'getNearUnlockSetCmd');
+						setTimeout(() => {
+							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) {},
+				// })
+			}
+			
+	}
 }
 
 
@@ -633,8 +628,9 @@ function splitNumber(num) {
 // 定义一个函数,用于向蓝牙设备写入数据
 function writeData(mac_id, data, callback = () => {}, fail = () => {}) {
 	const app = getApp();
-    const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == mac_id);
-    if (deviceId == undefined) {
+	const deviceIds = app.globalData.connectionState[mac_id].deviceId
+	console.log(deviceIds,'deviceIds');
+    if (deviceIds == undefined) {
         return ;
     }
 	// 如果数据长度为0,则直接返回
@@ -646,7 +642,7 @@ function writeData(mac_id, data, callback = () => {}, fail = () => {}) {
     buffer = common.toArrayBuffer(data.shift());
     // 调用uni.writeBLECharacteristicValue方法,向蓝牙设备写入数据
     uni.writeBLECharacteristicValue({
-        deviceId: deviceId,
+        deviceId: deviceIds,
         serviceId: writeServiceID,
         characteristicId: writeID,
         value: buffer,
@@ -658,7 +654,7 @@ function writeData(mac_id, data, callback = () => {}, fail = () => {}) {
             } else {
                 // 否则,延时500毫秒后再次调用writeData函数
                 setTimeout(() => {
-                    writeData(device, deviceId, data, callback, fail);
+                    writeData(device,  data, callback, fail);
                 }, 500);
             }
         },

+ 4 - 13
js_sdk/wa-permission/permission.js

@@ -444,9 +444,9 @@ function openGeneralSettings() {
     main.startActivity(intent);
 }
 
-async function getBondedDevices(deviceId) {
+function getBondedDevices() {
   // #ifdef APP-PLUS
-  try {
+
 	// 检查蓝牙是否可用
 	const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter');
 	const bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -474,7 +474,7 @@ async function getBondedDevices(deviceId) {
 	const car_sn=car_info.car_sn
 	const app = getApp();
 	
-	let deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == car_sn);
+	let deviceId = uni.getStorageSync('car_info').car_sn
 	if(!deviceId) return false
 	//配对配对蓝牙设备是否在蓝牙列表里面
 	deviceArray.map(device => {
@@ -483,16 +483,7 @@ async function getBondedDevices(deviceId) {
 		}
 	});
 	return isPaired
-  } catch (e) {
-	console.error('获取蓝牙设备失败:', e);
-	// uni.showToast({ title: '获取设备列表失败', icon: 'none' });
-	return false;
-  }
-  // #endif
-  
-  // #ifndef APP-PLUS
-  console.warn('此方法仅支持APP平台');
-  return false;
+
   // #endif
 }
 

+ 1 - 1
mixin/index.js

@@ -107,7 +107,7 @@ export default {
 				// 判断车辆是否在线状态 true 在线调用接口 不在线提示连接蓝牙
 				if (this.carOnline) {
 					if (this.cmdType == 'turnOnOrOff') {
-						const switchType = this.contrilList.find(item => item.isTurnOn)
+						const switchType = (this.contrilList.find(item => item.isTurnOn).isTurnOn == 1) ? 0 : 1
 						const pData = {
 							car_sn: car_info.car_sn,
 							switch: switchType