Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSerialNumberSync api error? #1056

Closed
M45ter opened this issue Aug 11, 2020 · 10 comments
Closed

getSerialNumberSync api error? #1056

M45ter opened this issue Aug 11, 2020 · 10 comments

Comments

@M45ter
Copy link

M45ter commented Aug 11, 2020

Bug report

when android SDK < 26, getSerialNumberSync() always return "unknown"?

 public String getSerialNumberSync() {
     try {
       if (Build.VERSION.SDK_INT >= 26) {
         if (getReactApplicationContext().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
           return Build.getSerial();
         }
       }
     } catch (Exception e) {
       // This is almost always a PermissionException. We will log it but return unknown
       System.err.println("getSerialNumber failed, it probably should not be used: " + e.getMessage());
     }
     return "unknown";
   }

Environment info

react-native info output:

# "react-native": "0.62.2",
# "react-native-device-info": "^5.5.7",
@mikehardy
Copy link
Collaborator

I fixed your code block so it was readable - triple-backticks are your friend https://www.markdownguide.org/extended-syntax#syntax-highlighting

The API is only available from API26 on. https://developer.android.com/reference/android/os/Build#getSerial()

@M45ter
Copy link
Author

M45ter commented Aug 12, 2020

this is better?

if (Build.VERSION.SDK_INT >= 26) {
...
} else {
     return Build.SERIAL;
}

@mikehardy
Copy link
Collaborator

Please read the docs for the API you propose: https://developer.android.com/reference/android/os/Build#SERIAL

A hardware serial number, if available. Alphanumeric only, case-insensitive. This field is always set to Build#UNKNOWN.

So, no, that won't help. Getting hardware identifiers is a losing proposition, you're fighting the operating systems now. They don't want to give you hardware identifiers. My best advice is to stop trying. Use something like Firebase Instance ID or similar

@M45ter
Copy link
Author

M45ter commented Aug 13, 2020

ok, thanks

@stewartmcgown
Copy link
Contributor

We're deploying a lot of Sunmi V2 devices, which allow access to the serial number via BUILD.SERIAL. I don't see a reason to not have the else statement and return BUILD.SERIAL if it isn't unknown?

@stewartmcgown
Copy link
Contributor

In case someone stumbles across this from Google and is in the same very specific sitation:

Add this topatches/react-native-device-info+7.0.2.patch:

--- a/node_modules/react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java
+++ b/node_modules/react-native-device-info/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java
@@ -655,6 +655,8 @@
         if (getReactApplicationContext().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
           return Build.getSerial();
         }
+      } else {
+        return Build.SERIAL;
       }
     } catch (Exception e) {
       // This is almost always a PermissionException. We will log it but return unknown

Grab these two packages:

"patch-package": "^6.2.2",
"postinstall-postinstall": "^2.1.0",

Then add a postinstall script:

"scripts": {
   ...,
  "postinstall": "patch-package"
}

@mikehardy
Copy link
Collaborator

Fascinating, I suppose if some hardware is actually returning it (quite unexpected / contrary to the API docs...) adding an else would be acceptable. If you PR it I'll merge it @stewartmcgown

@stewartmcgown
Copy link
Contributor

I should add that this is specifically on Android 7, which the devices are stuck on. I understand this is the last version where BUILD.SERIAL was populated Vs getSerial

stewartmcgown added a commit to stewartmcgown/react-native-device-info that referenced this issue Mar 24, 2021
…1056)

Following the disussion in react-native-device-info#1056 this will allow users of old MDM devices like the Sunmi V2 and others to access serial numbers via their privileged system access permissions assigned by the MDM.

This shouldn't cause any problems as the Build.SERIAL will be populated with 'unkown' regardless of permissions.
@stewartmcgown
Copy link
Contributor

@mikehardy tiny pr submitted!

@mikehardy
Copy link
Collaborator

mikehardy commented Mar 24, 2021

all PRs are mighty! 😆 - thanks

schie pushed a commit that referenced this issue Mar 24, 2021
Following the disussion in #1056 this will allow users of old MDM devices like the Sunmi V2 and others to access serial numbers via their privileged system access permissions assigned by the MDM.

This shouldn't cause any problems as the Build.SERIAL will be populated with 'unkown' regardless of permissions.
schie pushed a commit that referenced this issue Mar 24, 2021
## [8.0.7](v8.0.6...v8.0.7) (2021-03-24)

### Bug Fixes

* get legacy serial number for old APIs ([#1056](#1056)) ([40aa45b](40aa45b))
WictorWilnd added a commit to WictorWilnd/react-native-device-info that referenced this issue Feb 27, 2023
wdavis122 added a commit to wdavis122/react-native-device-info that referenced this issue Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants