BluetoothHelperAndroid_SDK.cs 4.3 KB

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