Exemple #1
0
        /// <summary>
        /// Connects an ARSession.  Note that if user permissions are needed they will be requested and thus this is an
        /// asynchronous method.
        /// </summary>
        /// <param name="sessionConfig">The session configuration.</param>
        /// <returns>An {@link AsyncTask<T>} that completes when the connection has been made or failed. </returns>
        public AsyncTask <SessionConnectionState> Connect(ARCoreSessionConfig sessionConfig)
        {
            const string androidCameraPermissionName = "android.permission.CAMERA";

            if (m_SessionManager == null)
            {
                ARDebug.LogError("Cannot connect because ARCoreSession failed to initialize.");
                return(new AsyncTask <SessionConnectionState>(SessionConnectionState.Uninitialized));
            }

            if (sessionConfig == null)
            {
                ARDebug.LogError("Unable to connect ARSession session due to missing ARSessionConfig.");
                m_SessionManager.ConnectionState = SessionConnectionState.MissingConfiguration;
                return(new AsyncTask <SessionConnectionState>(Session.ConnectionState));
            }

            // We have already connected at least once.
            if (Session.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Multiple attempts to connect to the ARSession.  Note that the ARSession connection " +
                                 "spans the lifetime of the application and cannot be reconfigured.  This will change in future " +
                                 "versions of ARCore.");
                return(new AsyncTask <SessionConnectionState>(Session.ConnectionState));
            }

            // Create an asynchronous task for the potential permissions flow and service connection.
            Action <SessionConnectionState> onTaskComplete;
            var returnTask = new AsyncTask <SessionConnectionState>(out onTaskComplete);

            returnTask.ThenAction((connectionState) =>
            {
                m_SessionManager.ConnectionState = connectionState;
            });

            // Attempt service connection immediately if permissions are granted.
            if (AndroidPermissionsManager.IsPermissionGranted(androidCameraPermissionName))
            {
                _ResumeSession(sessionConfig, onTaskComplete);
                return(returnTask);
            }

            // Request needed permissions and attempt service connection if granted.
            AndroidPermissionsManager.RequestPermission(androidCameraPermissionName).ThenAction((requestResult) =>
            {
                if (requestResult.IsAllGranted)
                {
                    _ResumeSession(sessionConfig, onTaskComplete);
                }
                else
                {
                    ARDebug.LogError("ARCore connection failed because a needed permission was rejected.");
                    onTaskComplete(SessionConnectionState.UserRejectedNeededPermission);
                }
            });

            return(returnTask);
        }
 public static AsyncTask <AndroidPermissionsRequestResult> RequestPermission(string permissionName)
 {
     if (AndroidPermissionsManager.IsPermissionGranted(permissionName))
     {
         return(new AsyncTask <AndroidPermissionsRequestResult>(new AndroidPermissionsRequestResult(
                                                                    new string[] { permissionName }, new bool[] { true })));
     }
     if (s_CurrentRequest != null)
     {
         ARDebug.LogError("Attempted to make simultaneous Android permissions requests.");
         return(null);
     }
     GetPermissionsService().Call("RequestPermissionAsync", GetUnityActivity(),
                                  new[] { permissionName }, GetInstance());
     s_CurrentRequest = new AsyncTask <AndroidPermissionsRequestResult>(out s_OnPermissionsRequestFinished);
     return(s_CurrentRequest);
 }
Exemple #3
0
        /// <summary>
        /// Connects an ARSession.  Note that if user permissions are needed they will be requested and thus this is an
        /// asynchronous method.
        /// </summary>
        /// <param name="sessionConfig">The session configuration.</param>
        /// <returns>An <c>AsyncTask</c> that completes when the connection has been made or failed. </returns>
        public AsyncTask <SessionConnectionState> Connect(SessionConfig sessionConfig)
        {
            const string ANDROID_CAMERA_PERMISSION_NAME = "android.permission.CAMERA";

            if (sessionConfig == null)
            {
                ARDebug.LogError("Unable to connect ARSession session due to missing ARSessionConfig.");
                SessionManager.ConnectionState = SessionConnectionState.MissingConfiguration;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            bool isSupported;
            ApiServiceErrorStatus status = TangoClientApi.TangoService_IsSupported(out isSupported);

            if (status.IsTangoFailure())
            {
                ARDebug.LogError("There was an error accessing the ARCore API.");
                SessionManager.ConnectionState = SessionConnectionState.ConnectToServiceFailed;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }
            if (!isSupported)
            {
                ARDebug.LogError("Device does not support ARCore.");
                SessionManager.ConnectionState = SessionConnectionState.DeviceNotSupported;
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            // We have already connected at least once.
            if (SessionManager.ConnectionState != SessionConnectionState.Uninitialized)
            {
                ARDebug.LogError("Multiple attempts to connect to the ARSession.  Note that the ARSession connection " +
                                 "spans the lifetime of the application and cannot be reconfigured.  This will change in future " +
                                 "versions of ARCore.");
                return(new AsyncTask <SessionConnectionState>(SessionManager.ConnectionState));
            }

            // Create an asynchronous task for the potential permissions flow and service connection.
            Action <SessionConnectionState> onTaskComplete;
            var returnTask = new AsyncTask <SessionConnectionState>(out onTaskComplete);

            // Attempt service connection immediately if permissions are granted.
            if (AndroidPermissionsManager.IsPermissionGranted(ANDROID_CAMERA_PERMISSION_NAME))
            {
                _ConnectToService(sessionConfig, onTaskComplete);
                return(returnTask);
            }

            // Request needed permissions and attempt service connection if granted.
            var permissionsArray = new string[] { ANDROID_CAMERA_PERMISSION_NAME };

            AndroidPermissionsManager.RequestPermission(permissionsArray).ThenAction((requestResult) => {
                if (requestResult.IsAllGranted)
                {
                    _ConnectToService(sessionConfig, onTaskComplete);
                }
                else
                {
                    ARDebug.LogError("ARCore connection failed because a needed permission was rejected.");
                    SessionManager.ConnectionState = SessionConnectionState.UserRejectedNeededPermission;
                    onTaskComplete(SessionManager.ConnectionState);
                }
            });

            return(returnTask);
        }