apache / cordova-plugin-device-orientation

Apache Cordova Device Orientation Plugin
https://cordova.apache.org/
Apache License 2.0
58 stars 83 forks source link

Documentation: CompassError code 3 on Android 10 #63

Open pdrhlik opened 3 years ago

pdrhlik commented 3 years ago

Bug Report

Problem

When subscribing to a watchHeading function, I get a CompassError {code: 3} response. It works on older Android phones. I haven't tested iOS. If I browsed the code correctly. The error code is neither one of CompassError.COMPASS_INTERNAL_ERR CompassError.COMPASS_NOT_SUPPORTED.

What is expected to happen?

It should work.

What does actually happen?

I get an undocumented CompassError.

Information

It doesn't work on Android 10.

Command or Code

import { DeviceOrientation } from '@ionic-native/device-orientation/ngx';

constructor(
    private deviceOrientation: DeviceOrientation
  ) { }

this.compass = this.deviceOrientation.watchHeading({frequency: 500}).subscribe(data => {
       // logic
});

Environment, Platform, Device

I used Angular 11, Ionic framework 5 and Capacitor.

Version information

ionic info output:

Ionic:

   Ionic CLI                     : 6.13.1 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.6.4
   @angular-devkit/build-angular : 0.1102.8
   @angular-devkit/schematics    : 9.1.0
   @angular/cli                  : 11.2.8
   @ionic/angular-toolkit        : 2.3.3

Capacitor:

   Capacitor CLI   : 2.4.7
   @capacitor/core : 2.4.7

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : not available
   Cordova Plugins   : not available

Utility:

   cordova-res (update available: 0.15.3) : 0.15.2
   native-run (update available: 1.3.0)   : 1.2.2

System:

   Android SDK Tools : 26.1.1 (/Users/datigo/Library/Android/sdk)
   ios-deploy        : 1.10.0
   ios-sim           : ios-sim/9.0.0 darwin-x64 node-v12.18.3
   NodeJS            : v12.18.3 (/usr/local/Cellar/node@12/12.18.3/bin/node)
   npm               : 6.14.6
   OS                : macOS Catalina
   Xcode             : Xcode 12.4 Build version 12D4e

Checklist

breautek commented 3 years ago

Error code 3 means the android device doesn't have a sensor available to use:

https://github.com/apache/cordova-plugin-device-orientation/blob/ee05df544d7a0c7514a3303193bf9413d41cfcbe/src/android/CompassListener.java#L180

Despite of the suppress deprecation, the method doesn't appear to be deprecated: https://developer.android.com/reference/android/hardware/SensorManager#getSensorList(int)

So this suggest that your device doesn't have an Orientation sensor. The constant is deprecated however, so perhaps you can try replacing it with SensorManager.getOrientation() instead?

pdrhlik commented 3 years ago

Thanks a lot for the clarification @breautek. I didn't realise my phone doesn't have the sensor, which really is the case. So everything's ok then.

I would only suggest documenting ERROR_FAILED_TO_START on the main page so it's a bit more obvious for other people. Thanks again!

breautek commented 3 years ago

We can leave this ticket as a reminder to improve documentation

valeckonext commented 1 year ago

I am experiencing the same issue using an "SM A125U1" phone (Android 12).

Would it be possible for the plugin to throw CompassError.COMPASS_NOT_SUPPORTED when this happens?

loicparent commented 1 year ago

Hello,

I am experiencing the same issue with a Xiaomi Redmi Note 11.

When I go to the Google Maps App, the compass indication works fine and smoothly. So I don't suppose this is about the sensor … maybe a permission or something else?

In my Ionic app, when I try to watch the headings, I get this error code 3.

Kind regards, Loïc

loicparent commented 10 months ago

Hello,

No news about the previous message ⬆️ ? I still have the issue (CompassError Code 3) on Xiaomi Redmi Note 10 and 11 even if the compass is well indicated in other apps.

Someone could help me with this?

Thanks, Loïc

breautek commented 10 months ago

Hello,

No news about the previous message ⬆️ ? I still have the issue (CompassError Code 3) on Xiaomi Redmi Note 10 and 11 even if the compass is well indicated in other apps.

Someone could help me with this?

Thanks, Loïc

https://github.com/apache/cordova-plugin-device-orientation/issues/63#issuecomment-818232652

Error code 3 is returned if the compass sensor could not start, which is the case if the sensor could not be found. This is likely because the sensor (TYPE_ORIENTATION) is deprecated and has been deprecated for some time now and replaced with a non-sensor getOrientation API.

There is no open PR that makes this change, but there is https://github.com/apache/cordova-plugin-device-orientation/pull/78 which attempts to simulate orientation sensor by fusing two different sensors together. I'm not sure if I agree with this approach, but you could test it to see if it solves your problem.

loicparent commented 10 months ago

Hello @breautek,

Thank you for your reply :)

I tried to replace the file compassListener.java in /node_modules/cordova-plugin-device-orientation/src/android/CompassListener.java.

I see a little difference because I don't get an error that don't return the event callback data BUT, now, the event is returned but with incomplete values:

{
  headingAccuracy: 3
  magneticHeading: undefined
  timestamp: 1693572512780
  trueHeading: undefined
}

Do you have an idea why?

Kind regards, Loïc