Просмотр исходного кода

安卓部分蓝牙代码优化

lvjincheng 3 лет назад
Родитель
Сommit
375346d49e

+ 1 - 0
Assets/BowArrow/Scripts/Bluetooth/BluetoothAim.cs

@@ -172,6 +172,7 @@ public class BluetoothAim : MonoBehaviour
     void ConnectBleHelper()
     {
         #if UNITY_ANDROID
+        PopupMgr.ins.ClearAllTip();
         if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
         {
             HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));

+ 8 - 17
Assets/BowArrow/Scripts/Bluetooth/BluetoothHelperAndroid.cs

@@ -1,4 +1,3 @@
-using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
@@ -28,34 +27,26 @@ public class BluetoothHelperAndroid
             int sdkInt = buildVersion.GetStatic<int>("SDK_INT");
             List<string> permissionListA = new List<string>();
             List<string> permissionListB = new List<string>();
-            List<string> permissionListC = new List<string>();
             if (sdkInt >= 23)
             {
                 permissionListA.Add(Permission.CoarseLocation);
                 permissionListA.Add(Permission.FineLocation);
-                if (sdkInt >= 29)
-                    permissionListB.Add("android.permission.ACCESS_BACKGROUND_LOCATION");
                 if (sdkInt < 31)
                 {
-                    permissionListC.Add("android.permission.BLUETOOTH");
-                    permissionListC.Add("android.permission.BLUETOOTH_ADMIN");
+                    permissionListB.Add("android.permission.BLUETOOTH");
+                    permissionListB.Add("android.permission.BLUETOOTH_ADMIN");
                 }
                 else
                 {
-                    permissionListC.Add("android.permission.BLUETOOTH_SCAN");
-                    permissionListC.Add("android.permission.BLUETOOTH_ADVERTISE");
-                    permissionListC.Add("android.permission.BLUETOOTH_CONNECT");
+                    permissionListB.Add("android.permission.BLUETOOTH_SCAN");
+                    permissionListB.Add("android.permission.BLUETOOTH_ADVERTISE");
+                    permissionListB.Add("android.permission.BLUETOOTH_CONNECT");
                 }
             }
             if (IsPermissionsGranted(permissionListA)
-                && IsPermissionsGranted(permissionListB)
-                && IsPermissionsGranted(permissionListC)) return false;
-            RequestUserPermissions(permissionListA.ToArray(), () =>
-            {
-                RequestUserPermissions(permissionListB.ToArray(), () =>
-                {
-                    RequestUserPermissions(permissionListC.ToArray(), onAllGranted, onDenied);
-                }, onDenied);
+                && IsPermissionsGranted(permissionListB)) return false;
+            RequestUserPermissions(permissionListA.ToArray(), () => {
+                RequestUserPermissions(permissionListB.ToArray(), onAllGranted, onDenied);
             }, onDenied);
             return true;
         }

+ 2 - 2
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/cn.json

@@ -141,8 +141,8 @@
     "about-us_content": "<color=#FFFFFF00>缩进</color>青凤鸾是一个技能运动智能硬件品牌。产品包含智能弓箭、智能射击、智能滑雪、智能高尔夫等。青凤鸾希望通过创新的智能硬件,让技能运动变得触手可及。并在疫情的大环境下,让人们的室内运动生活更加丰富有趣。",
     "ble-please-open-ble": "请求蓝牙失败,可能原因:\n1.未打开手机蓝牙\n2.未授予蓝牙定位权限",
     "ble-exception1": "未打开手机蓝牙开关",
-    "ble-exception2": "需要始终允许<定位>权限",
-    "ble-exception3": "需要始终允许<连接附近的设备>权限",
+    "ble-exception2": "未授予<定位>权限",
+    "ble-exception3": "未授予<连接附近的设备>权限",
 
     "MagInterferenceTip_content": "由于地磁计初始化易受到周围环境的影响,因此在初始化过程中请按照以下步骤来做:\n1、请在初始化过程中,保持周围环境的稳定,远离金属物体和磁场干扰。\n2、请保持智能弓箭模块和手机、电视等电子设备0.5米以上的距离。\n3、请将安装了模块的智能弓箭或单独模块沿着XYZ三轴进行充分地旋转,直到提示完成为止。\n4、如多次无法完成初始化,也可继续使用,只是瞄准的精度会受到影响,游戏中需多做一次视角归位的操作。",
     "MagInterferenceTip_ok": "确定",

+ 2 - 2
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/en.json

@@ -141,8 +141,8 @@
     "about-us_content": "Qingfengluan is a skill sports intelligent hardware brand. Products include intelligent bow and arrow, intelligent shooting, intelligent skiing, intelligent golf, etc. Qingfengluan hopes to make skill sports accessible through innovative intelligent hardware. And in the context of the epidemic, make people's indoor sports life more rich and interesting.",
     "ble-please-open-ble": "Failed to request Bluetooth. Possible causes:\n1.Bluetooth is not turned on\n2.Bluetooth location permission not granted",
     "ble-exception1": "Bluetooth switch of mobile phone is not turned on",
-    "ble-exception2": "Need to always allow <Get location info> permission",
-    "ble-exception3": "Need to always allow <Connect to nearby devices> permission",
+    "ble-exception2": "<Get location info> permission not granted",
+    "ble-exception3": "<Connect to nearby devices> permission not granted",
 
     "MagInterferenceTip_content": "Since the initialization of the magnetometer is vulnerable to the influence of the surrounding environment, please follow the following steps in the initialization process: \n1. Please keep the surrounding environment stable and away from metal objects and magnetic field interference during the initialization process. \n2. Please keep a distance of more than 0.5m between the smart bow module and electronic devices such as mobile phones and televisions. \n3. Please fully rotate the smart bow or individual module with the module installed along the XYZ axis until the prompt is completed. \n4. If the initialization cannot be completed for many times, it can still be used. However, the accuracy of aiming will be affected, and the game needs to do one more angle of view homing operation.",
     "MagInterferenceTip_ok": "OK",

+ 27 - 19
Assets/HeartRateBand/Bluetooth/HRB_Bluetooth.cs

@@ -145,22 +145,26 @@ public class HRB_Bluetooth : MonoBehaviour
 
     void ConnectBleHelper()
     {
-        try
+        #if UNITY_ANDROID
+        PopupMgr.ins.ClearAllTip();
+        if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
         {
-            #if UNITY_ANDROID
-            using (var helper = new AndroidJavaClass("com.example.smartbowlib.BluetoothHelper")) 
+            HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
+            return;
+        }
+        if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) => {
+            if (permission.Contains("LOCATION"))
             {
-                using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
-                {
-                    using (var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
-                    {
-                        bool ok = helper.CallStatic<bool>("requestPermissions", context);
-                        if (!ok) throw new Exception("安卓12的蓝牙权限-尚未授权");
-                    }
-                }
+                HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
             }
-            #endif
-            
+            else if (permission.Contains("BLUETOOTH"))
+            {
+                HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
+            }
+        })) return;
+        #endif
+        try
+        {
             BluetoothHelper.BLE = true;
             bluetoothHelper = BluetoothHelper.GetNewInstance();
 
@@ -233,14 +237,18 @@ public class HRB_Bluetooth : MonoBehaviour
         }
         catch (Exception e)
         {
-            Debug.LogError(e.Message);
-            Debug.LogError(e.StackTrace);
-            canConnect = true;
-            status = BluetoothStatusEnum.Connect;
-            userDoConnect = false;
-            PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
+            Debug.LogError(e);
+            HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
         }
     }
+    void HandleConnectException(string errorText)
+    {
+        canConnect = true;
+        status = BluetoothStatusEnum.Connect;
+        userDoConnect = false;
+        PopupMgr.ins.ShowTip(errorText);
+    }
+    
 
     void Log(string text)
     {

+ 3 - 1
Assets/Plugins/Android/AndroidManifest.xml

@@ -45,11 +45,13 @@
     </receiver>
 
   </application>
+  <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
+  <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
+  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
   <uses-permission android:name="android.permission.BLUETOOTH" />
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
   <uses-permission-sdk-23 android:name="android.permission.BLUETOOTH" />
   <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

BIN
Assets/Plugins/Android/smartbowlib-debug.aar