Skip to content

Instantly share code, notes, and snippets.

@deltheil
Created April 3, 2012 10:59
Show Gist options
  • Save deltheil/2291028 to your computer and use it in GitHub Desktop.
Save deltheil/2291028 to your computer and use it in GitHub Desktop.
Android Native API (JNI): get the device name
#include <stdio.h>
#include <string.h>
#include <sys/system_properties.h>
/* Get device name
--
1/ Compile with the Android NDK Toolchain:
arm-linux-androideabi-gcc -static pname.c -o pname
2/ Transfer on device:
adb push pname /data/local/tmp
3/ Run:
adb shell
$ /data/local/tmp/pname
[device]: [HTC/HTC Sensation Z710e]
NOTE: these properties can be queried via adb:
adb shell getprop ro.product.manufacturer */
int main() {
char man[PROP_VALUE_MAX + 1], mod[PROP_VALUE_MAX + 1];
/* A length 0 value indicates that the property is not defined */
int lman = __system_property_get("ro.product.manufacturer", man);
int lmod = __system_property_get("ro.product.model", mod);
int len = lman + lmod;
char *pname = NULL;
if (len > 0) {
pname = malloc(len + 2);
snprintf(pname, len + 2, "%s/%s", lman > 0 ? man : "", lmod > 0 ? mod : "");
}
printf("[device]: [%s]\n", pname ? pname : "N/A");
if (pname) free(pname);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment