0

I need to read an Android property on my device, in C++.

The property in question is sys.serialno.

At the moment, I access the property in java, with something similar to:

    Process p = Runtime.getRuntime().exec( "getprop sys.serialno" );
    p.waitFor();
    BufferedReader reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
    String line = reader.readLine();

however I'd now like to do the same in C++. I don't want to emulate the code above, I just need to access the property in the easiest way possible.

I appreciate I can just call the java from my C++, but I'd rather not do that, as ideally I would be performing this call early on in the app life-cycle.

Surfbutler
  • 1,529
  • 2
  • 17
  • 38
  • 1
    The Java code appears to create a subprocess, so you'd definitely not call the Java from C++. That's just pointless. At the very least, you'd call `popen` directly. But there are probably better solutions – MSalters Jan 04 '21 at 13:32
  • haven't had a chance to try it yet, but I think I need to do something along the lines of: auto serialno_len = __system_property_get("ro.serialno", serial); – Surfbutler Jan 04 '21 at 15:57
  • 1
    That appears outdated since Android L: https://stackoverflow.com/questions/26722040/replacement-for-system-property-get-in-android-l-ndk, which also gives my `popen` suggestion. – MSalters Jan 04 '21 at 16:42
  • Some useful notes here (http://android.2317887.n4.nabble.com/system-property-get-td454839.html). Looks like __system_property_get is indeed deprecated, and can break if called with a property name longer than 32 (currently). However, the alternative is a callback, which I don't want to get into right now. My serial number property name is only 11 chars long, and I'm working with known hardware, so I 'think' I'm ok - thanks for your help. – Surfbutler Jan 05 '21 at 16:56
  • Oh and some posts I've seen indicate that popen may be causing crashes on Android from time to time unfortunately... – Surfbutler Jan 05 '21 at 16:57

0 Answers0