BluetoothHelperAndroid.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.Android;
  5. public class BluetoothHelperAndroid
  6. {
  7. public static bool IsBluetoothEnabled()
  8. {
  9. using (var classBluetoothAdapter = new AndroidJavaClass("android.bluetooth.BluetoothAdapter"))
  10. using (var bluetoothAdapter = classBluetoothAdapter.CallStatic<AndroidJavaObject>("getDefaultAdapter"))
  11. {
  12. if (bluetoothAdapter == null)
  13. {
  14. Debug.Log("当前设备不支持蓝牙功能");
  15. return false;
  16. }
  17. bool isEnabled = bluetoothAdapter.Call<bool>("isEnabled");
  18. return isEnabled;
  19. }
  20. }
  21. public static bool RequestBluetoothPermissions(UnityAction onAllGranted, UnityAction<string> onDenied)
  22. {
  23. using (var buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
  24. {
  25. int sdkInt = buildVersion.GetStatic<int>("SDK_INT");
  26. List<string> permissionListA = new List<string>();
  27. List<string> permissionListB = new List<string>();
  28. if (sdkInt >= 23)
  29. {
  30. permissionListA.Add(Permission.CoarseLocation);
  31. permissionListA.Add(Permission.FineLocation);
  32. if (sdkInt < 31)
  33. {
  34. permissionListB.Add("android.permission.BLUETOOTH");
  35. permissionListB.Add("android.permission.BLUETOOTH_ADMIN");
  36. }
  37. else
  38. {
  39. permissionListB.Add("android.permission.BLUETOOTH_SCAN");
  40. permissionListB.Add("android.permission.BLUETOOTH_ADVERTISE");
  41. permissionListB.Add("android.permission.BLUETOOTH_CONNECT");
  42. }
  43. }
  44. if (IsPermissionsGranted(permissionListA)
  45. && IsPermissionsGranted(permissionListB)) return false;
  46. RequestUserPermissions(permissionListA.ToArray(), () => {
  47. RequestUserPermissions(permissionListB.ToArray(), onAllGranted, onDenied);
  48. }, onDenied);
  49. return true;
  50. }
  51. }
  52. private static bool IsPermissionsGranted(List<string> permissions)
  53. {
  54. foreach (var permission in permissions)
  55. if (!Permission.HasUserAuthorizedPermission(permission)) return false;
  56. return true;
  57. }
  58. private static void RequestUserPermissions(string[] permissions, UnityAction onAllGranted, UnityAction<string> onDenied)
  59. {
  60. bool hasExecuteOnDenied = false;
  61. List<string> permissionsNeedRequest = new List<string>();
  62. foreach (var permission in permissions)
  63. if (!Permission.HasUserAuthorizedPermission(permission)) permissionsNeedRequest.Add(permission);
  64. if (permissionsNeedRequest.Count > 0)
  65. {
  66. var requestCallback = new PermissionCallbacks();
  67. requestCallback.PermissionGranted += (permission) =>
  68. {
  69. Debug.Log("用户同意" + permission);
  70. permissionsNeedRequest.Remove(permission);
  71. if (permissionsNeedRequest.Count == 0) onAllGranted?.Invoke();
  72. };
  73. requestCallback.PermissionDenied += (permission) =>
  74. {
  75. Debug.LogWarning("用户拒绝" + permission);
  76. if (!hasExecuteOnDenied)
  77. {
  78. hasExecuteOnDenied = true;
  79. onDenied?.Invoke(permission);
  80. }
  81. };
  82. requestCallback.PermissionDeniedAndDontAskAgain += (permission) =>
  83. {
  84. Debug.LogWarning("用户拒绝且要求不再询问" + permission);
  85. if (!hasExecuteOnDenied)
  86. {
  87. hasExecuteOnDenied = true;
  88. onDenied?.Invoke(permission);
  89. }
  90. };
  91. Permission.RequestUserPermissions(permissionsNeedRequest.ToArray(), requestCallback);
  92. }
  93. else onAllGranted?.Invoke();
  94. }
  95. }