Tuesday, December 9, 2008

Hacking the "Private" Android API

So Android doesn't allow you to interact with Bluetooth. The functionality exists, however developers can't access it... or can they?

There are many apps on the Android Market that toggle Bluetooth. So if Google doesn't give developers the ability how do they do it? The answer is an advanced Java concept called Reflection (http://java.sun.com/docs/books/tutorial/reflect/).

The idea is that even if you don't know what object you are using, you can find out information about it and then use that information to call methods that you otherwise shouldn't know about it.

The way this works is that Google is doing something strange.
In order access most services on Android you call the Context.getSystemService() method passing in a string signifying the service you want to access. You won't find this information in the API but if you pass the string, "bluetooth" you are returned an object whose class isn't in the standard API. This is the Bluetooth Manager. However the class is generic, it's of type Object.

But now you can use Reflection to find it's methods.

Object manager = getSystemService("bluetooth");
Class c = manager.getClass();
Method enable = c.getMethod("enable");
enable.setAccessible(true);
enable.invoke(manager);

... and then you've enabled Bluetooth.

Instead of "enable" you can put the name of any other method of the Bluetooth Manager class. You can get a list of all the methods by calling getMethods().

It's not an efficient way to program, it definitely doesn't produce nice and clear code. But it's a very valuable tool to know.

6 comments:

Unknown said...

As I understand it, Android 1.1 closes this hole.

viagra online said...

this kind of topic is good for people to learn more about it, and that people should be every day less ignorant, and medicine for this is the reading of issues like this

jim said...

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=84

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=83

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=82

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=81

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=292

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=80

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=79

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=78

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=77

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=76

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=75

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=92

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=191

http://www.smartbargainsus.com/store/index.php?_a=viewProd&productId=74

Anonymous said...

Rihanna nude is a North American R&B/Soul singer. Most of the worldly population would also classify her as a traditional dancehall singer; who has splurged onto the entertainment scene unexpectedly.
rihanna naked pics
Rihanna is a lot of things, she has produced many great albums and songs in her short career, done photo shoots part of every celebrities life, and not to mention the paparazzi photos as well, and we can't forget the celebrity sex tapes and pictures, there have been many scandals relating around Rihanna life for many months as Perez Hilton and TMZ and many other celebrity gossip blogs have been reporting over the months. To many people's surprise, the rihanna naked pictures
rihanna naked

Anonymous said...

i'm new... hope to despatch round more oftentimes!

columbus circle hotel said...

Gorgeous! It's hard to pick a favorite. This is the greatest blog post I have ever read. Thanks for sharing this wisdom.