【问题标题】:Unity - Android 10 - BLE Bluetooth - Background locationUnity - Android 10 - BLE 蓝牙 - 背景位置
【发布时间】:2020-07-10 08:09:53
【问题描述】:

我正在开发一个使用蓝牙扫描服务的 Unity Android 项目。当我升级到 Android 10 (API 29) 时,蓝牙连接开始出现问题。我正在从这篇文章(下面的链接)中获得帮助,其中提到对于 Android 10 及更高版本,需要包含BACKGROUND_LOCATION 权限。

Android 10 not working with BLE Bluetooth scanning

在 Unity 中,我使用权限实用工具编写了一些代码

  if (AndroidRuntimePermissions.CheckPermission(Permission.CoarseLocation) != AndroidRuntimePermissions.Permission.Granted)
  {
      AndroidRuntimePermissions.RequestPermission(Permission.CoarseLocation);   
  }

Permission 类是来自UnityEngine.Android 的内部类,它似乎没有后台位置权限。见下文,

using UnityEngine.Scripting;

/// <summary>
///   <para>Structure describing a permission that requires user authorization.</para>
/// </summary>
[NativeHeader ("Runtime/Export/Android/AndroidPermissions.bindings.h")]
[UsedByNativeCode]
public struct Permission
{
    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to use the camera.</para>
    /// </summary>
    public const string Camera = "android.permission.CAMERA";

    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to use the microphone.</para>
    /// </summary>
    public const string Microphone = "android.permission.RECORD_AUDIO";

    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to use the users location with high precision.</para>
    /// </summary>
    public const string FineLocation = "android.permission.ACCESS_FINE_LOCATION";

    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to use the users location with coarse granularity.</para>
    /// </summary>
    public const string CoarseLocation = "android.permission.ACCESS_COARSE_LOCATION";

    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to read from external storage such as a SD card.</para>
    /// </summary>
    public const string ExternalStorageRead = "android.permission.READ_EXTERNAL_STORAGE";

    /// <summary>
    ///   <para>Used when requesting permission or checking if permission has been granted to write to external storage such as a SD card.</para>
    /// </summary>
    public const string ExternalStorageWrite = "android.permission.WRITE_EXTERNAL_STORAGE";

    /// <summary>
    ///   <para>Check if the user has granted access to a device resource or information that requires authorization.</para>
    /// </summary>
    /// <param name="permission">A string representing the permission to request. For permissions which Unity has not predefined you may also manually provide the constant value obtained from the Android documentation here: https:developer.android.comguidetopicspermissionsoverview#permission-groups such as "android.permission.READ_CONTACTS".</param>
    /// <returns>
    ///   <para>Whether the requested permission has been granted.</para>
    /// </returns>
    [MethodImpl (MethodImplOptions.InternalCall)]
    [StaticAccessor ("PermissionsBindings", StaticAccessorType.DoubleColon)]
    public static extern bool HasUserAuthorizedPermission (string permission);

    /// <summary>
    ///   <para>Request that the user grant access to a device resource or information that requires authorization.</para>
    /// </summary>
    /// <param name="permission">A string that describes the permission to request. For permissions which Unity has not predefined you may also manually provide the constant value obtained from the Android documentation here: https:developer.android.comguidetopicspermissionsoverview#permission-groups such as "android.permission.READ_CONTACTS".</param>
    [MethodImpl (MethodImplOptions.InternalCall)]
    [StaticAccessor ("PermissionsBindings", StaticAccessorType.DoubleColon)]
    public static extern void RequestUserPermission (string permission);
}

如果您解决了这个问题,请告诉我。非常感谢您的帮助。谢谢。

【问题讨论】:

    标签: android visual-studio unity3d bluetooth core-bluetooth


    【解决方案1】:

    它只是一个字符串,所以将你想要的权限类传递给它。

     Permission.RequestUserPermission( "android.permission.ACCESS_BACKGROUND_LOCATION" );
    

    我使用它来确保获得位置许可请求的所有三个选项(“始终”、“仅在打开时”、“从不”)

    【讨论】:

    • 感谢您的帮助。此外,Android 10 需要 ACCESS_FINE_LOCATION 权限以及 ACCESS_BACKGROUND_LOCATION
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2021-07-21
    • 2016-06-14
    • 2018-05-05
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多