Native call to get device name?

1,910 views
Skip to first unread message

Ken Turkowski

unread,
Oct 30, 2013, 5:57:14 PM10/30/13
to andro...@googlegroups.com
Is there a native call that I can make to get the device name to model number?
I run a number of unit tests and benchmarks, and it would be nice if they were self-annotating.
I know that I can get it in Java, but my tests are written in C++ with GoogleTest.

-Ken

Ali Helmy

unread,
Oct 30, 2013, 6:00:22 PM10/30/13
to Ken Turkowski, andro...@googlegroups.com
Ken,

I use a simple jni call to get this info

const char *const GetDeviceName() {
  // Fetch the device model as name for now
  JNIEnv *env = GetEnv();
  jclass build_class = env->FindClass("android/os/Build");
  jfieldID model_id = env->GetStaticFieldID(build_class, "MODEL", "Ljava/lang/String;");
  jstring model_obj  = (jstring)env->GetStaticObjectField(build_class, model_id);
  const char *deviceName = env->GetStringUTFChars(model_obj, 0);
  return deviceName;
}

You can, of course, get any other fields in the Build class, which should have the info you need

cheers,
ali
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

Ken Turkowski

unread,
Oct 30, 2013, 7:00:24 PM10/30/13
to Ali Helmy, andro...@googlegroups.com
Thanks, Ali,

I might need a little extra hand-holding.

Will this work in a standalone C++ command line tool?

I can't seem to get GetEnv() to work as given. The GetEnv() APIs in jni.h seem to want a JavaVM, which I don't have. Can I create one in a jiffy just to get this info?

-Ken

Ali Helmy

unread,
Oct 30, 2013, 7:02:34 PM10/30/13
to Ken Turkowski, andro...@googlegroups.com
Ah! No idea, mate, sorry… I have no idea about interfacing Android NDK with a native C++ CLI tool

Sorry, thats the extent of my knowledge

cheers,
ali

Brendon Costa

unread,
Oct 31, 2013, 3:00:23 PM10/31/13
to andro...@googlegroups.com, Ali Helmy
Give the following a try (or other property fields you think might be better):

#include <sys/system_properties.h>
char model_string[PROP_VALUE_MAX+1];
__system_property_get("ro.product.model", model_string);


Ken Turkowski

unread,
Nov 13, 2013, 7:28:30 PM11/13/13
to andro...@googlegroups.com, brendon...@gmail.com
Awesome, Brendon! It works perfectly!

-Ken
Reply all
Reply to author
Forward
0 new messages