|
|
@@ -0,0 +1,108 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.Events;
|
|
|
+using UnityEngine.Android;
|
|
|
+
|
|
|
+public class BluetoothHelperAndroid
|
|
|
+{
|
|
|
+ public static bool IsBluetoothEnabled()
|
|
|
+ {
|
|
|
+ using (var classBluetoothAdapter = new AndroidJavaClass("android.bluetooth.BluetoothAdapter"))
|
|
|
+ using (var bluetoothAdapter = classBluetoothAdapter.CallStatic<AndroidJavaObject>("getDefaultAdapter"))
|
|
|
+ {
|
|
|
+ if (bluetoothAdapter == null)
|
|
|
+ {
|
|
|
+ Debug.Log("当前设备不支持蓝牙功能");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ bool isEnabled = bluetoothAdapter.Call<bool>("isEnabled");
|
|
|
+ return isEnabled;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool RequestBluetoothPermissions(UnityAction onAllGranted, UnityAction<string> onDenied)
|
|
|
+ {
|
|
|
+ using (var buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
|
|
|
+ {
|
|
|
+ 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");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ permissionListC.Add("android.permission.BLUETOOTH_SCAN");
|
|
|
+ permissionListC.Add("android.permission.BLUETOOTH_ADVERTISE");
|
|
|
+ permissionListC.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);
|
|
|
+ }, onDenied);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static bool IsPermissionsGranted(List<string> permissions)
|
|
|
+ {
|
|
|
+ foreach (var permission in permissions)
|
|
|
+ if (!Permission.HasUserAuthorizedPermission(permission)) return false;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RequestUserPermissions(string[] permissions, UnityAction onAllGranted, UnityAction<string> onDenied)
|
|
|
+ {
|
|
|
+ bool hasExecuteOnDenied = false;
|
|
|
+ List<string> permissionsNeedRequest = new List<string>();
|
|
|
+ foreach (var permission in permissions)
|
|
|
+ if (!Permission.HasUserAuthorizedPermission(permission)) permissionsNeedRequest.Add(permission);
|
|
|
+ if (permissionsNeedRequest.Count > 0)
|
|
|
+ {
|
|
|
+ var requestCallback = new PermissionCallbacks();
|
|
|
+ requestCallback.PermissionGranted += (permission) =>
|
|
|
+ {
|
|
|
+ Debug.Log("用户同意" + permission);
|
|
|
+ permissionsNeedRequest.Remove(permission);
|
|
|
+ if (permissionsNeedRequest.Count == 0) onAllGranted?.Invoke();
|
|
|
+ };
|
|
|
+ requestCallback.PermissionDenied += (permission) =>
|
|
|
+ {
|
|
|
+ Debug.LogWarning("用户拒绝" + permission);
|
|
|
+ if (!hasExecuteOnDenied)
|
|
|
+ {
|
|
|
+ hasExecuteOnDenied = true;
|
|
|
+ onDenied?.Invoke(permission);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ requestCallback.PermissionDeniedAndDontAskAgain += (permission) =>
|
|
|
+ {
|
|
|
+ Debug.LogWarning("用户拒绝且要求不再询问" + permission);
|
|
|
+ if (!hasExecuteOnDenied)
|
|
|
+ {
|
|
|
+ hasExecuteOnDenied = true;
|
|
|
+ onDenied?.Invoke(permission);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Permission.RequestUserPermissions(permissionsNeedRequest.ToArray(), requestCallback);
|
|
|
+ }
|
|
|
+ else onAllGranted?.Invoke();
|
|
|
+ }
|
|
|
+}
|