Device Id from NDK

4,628 views
Skip to first unread message

terr

unread,
Jun 14, 2010, 4:44:00 PM6/14/10
to android-ndk
I am trying to get the device id from the NDK level. I see that there
are multiple ways to get a unique id from the App itself but my
requirement is that I can access this id from the NDK (not passed in
from the Java layer).

I see that there is the IMEI value that can be retrieved as well as
the android id but neither of these is accessible from the NDK
level.

Does anyone know of a good way to get the device id from the NDK
level?

Thanks

Onur Cinar

unread,
Jun 14, 2010, 10:23:03 PM6/14/10
to android-ndk

Hi,

I can imagine an ugly way of obtaining that information through the
"iphonesubinfo" service from the native layer. But it will be a
solution based on the private API so it won't be good.

Other than that, you can make JNI calls to obtain that information,
which will keep everything in the native layer if you have a JNIEnv
available.

I would suggest using the MAC address of the wireless interface as the
ID, but it will probably take same amount of effort to obtain.

Regards,

-onur



---
www.zdo.com

terr

unread,
Jun 15, 2010, 9:42:51 AM6/15/10
to android-ndk

Thanks for the reply.

I have already tried the Mac address. The downside is that if wifi is
off the wireless chip is turned off and no interface is present to
query for the Mac address. I need a reliable way to get this Id.

It's crazy that there is no standard way of doing this. There are a
couple of Ids that can be used (IMEI, device Id, etc...) they are just
not accessible from the NDK level.

I even tried to launch an android service that listens on a port and
all it does is retrieve the device Id and then shuts down. This fails
since you shouldn't fork or exec from the NDK layer.

It's almost like the NDK is a hobby API for google.

Thanks again,
terr

David Turner

unread,
Jun 15, 2010, 10:00:46 AM6/15/10
to andro...@googlegroups.com
On Tue, Jun 15, 2010 at 6:42 AM, terr <sayk...@gmail.com> wrote:

Thanks for the reply.

I have already tried the Mac address.  The downside is that if wifi is
off the wireless chip is turned off and no interface is present to
query for the Mac address.  I need a reliable way to get this Id.

It's crazy that there is no standard way of doing this.  There are a
couple of Ids that can be used (IMEI, device Id, etc...) they are just
not accessible from the NDK level.

I even tried to launch an android service that listens on a port and
all it does is retrieve the device Id and then shuts down.  This fails
since you shouldn't fork or exec from the NDK layer.

It's almost like the NDK is a hobby API for google.

No, it's just that it is not designed to provide full platform native APIs for Android,
just the ability to embed native code for performance reason. If you want to access
most of the system's features, you will have to use Java.

 
Thanks again,
terr

On Jun 14, 7:23 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi,
>
> I can imagine an ugly way of obtaining that information through the
> "iphonesubinfo" service from the native layer. But it will be a
> solution based on the private API so it won't be good.
>
> Other than that, you can make JNI calls to obtain that information,
> which will keep everything in the native layer if you have a JNIEnv
> available.
>
> I would suggest using the MAC address of the wireless interface as the
> ID, but it will probably take same amount of effort to obtain.
>
> Regards,
>
> -onur
>
> ---www.zdo.com

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Onur Cinar

unread,
Jun 16, 2010, 4:30:15 AM6/16/10
to android-ndk

Hi,

There is also the ANDROID_ID. Based on the description on:

http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID

A 64-bit number (as a hex string) that is randomly generated on the
device's first boot and should remain constant for the lifetime of the
device. (The value may change if a factory reset is performed on the
device.)

It is located in the /data/data/com.android.providers.settings/
databases/settings.db database file. I didn't check the permissions
yet.

But this number can be changed.

Regards,

-onur




---
www.zdo.com

terr

unread,
Jun 16, 2010, 2:05:42 PM6/16/10
to android-ndk
Hi Onur.

I guess I can open the db file up and do direct access to it but I
think that completely breaks the NDK way of doing things.

Thanks,
terr

On Jun 16, 1:30 am, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi,
>
> There is also the ANDROID_ID. Based on the description on:
>
> http://developer.android.com/reference/android/provider/Settings.Secu...

Onur Cinar

unread,
Jun 16, 2010, 5:36:42 PM6/16/10
to android-ndk

Hi Terr,

Looks like there isn't any NDK way to do this yet. But instead of
trying to extract that information, it will be much more portable to
have it implemented through JNI, so that you can still keep things in
the native code.

Regards,

-onur
---
www.zdo.com

mike.b

unread,
Jun 25, 2010, 5:38:14 AM6/25/10
to android-ndk
Everything what can be done in Android Java can be done also in Native
code using JNI. The code just looks difficult and is harder to write,
but at least things are possible.
To get device IMEI, here's code:

JNIEnv &env = ...
jobject activity = ... (android.app.Activity object)
//TelephonyManager telephony_manager =
getSystemService(Context.TELEPHONY_SERVICE);
jmethodID mid = env.GetMethodID(env.GetObjectClass(activity),
"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject telephony_manager = env.CallObjectMethod(activity, mid,
env.NewStringUTF("phone"));
//String s_imei = tm.getDeviceId();
mid = env.GetMethodID(env.GetObjectClass(telephony_manager),
"getDeviceId", "()Ljava/lang/String;");
jstring s_imei = (jstring)env.CallObjectMethod(telephony_manager,
mid);
if(env.ExceptionCheck()){
env.ExceptionClear();
return false;
}
const jchar *imei = env.GetStringCritical(s_imei, NULL);
// copy imei string anywhere you need
env.ReleaseStringCritical(s_imei, imei);

This assumes that your app is standard Android android.app.Activity,
and native code is called from this.

On Jun 16, 11:36 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Terr,
>
> Looks like there isn't anyNDKway to do this yet. But instead of
Reply all
Reply to author
Forward
0 new messages