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.

12 comments:

Charles Duffy said...

As I understand it, Android 1.1 closes this hole.

Anonymous said...

[url=http://www.blingforfun.com]hip hop jewelry[/url],[url=http://www.blingforfun.com/pendants/cat_9.html]hip hop pendants[/url],hip hop watches,[url=http://blingforfun.com/belts/cat_18.html]bling bling[/url] ,hip hop,[url=http://blingforfun.com/chains/cat_7.html]hip hop chains[/url],hip hop bling,[url=http://blingforfun.com/chains/cat_7.html]iced out chains[/url],[url=http://www.blingforfun.com/chains/cat_7.html]wholesale chains[/url]
hip hop jewelry
wholesale hip hop watches
hip hop rings

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/main/index.php

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=1

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=22

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=4

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=7

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=8

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=9

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=15

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=16

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=17

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=19

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=20

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=saleItems

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=2

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=21

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=18

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=20

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=16

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=24

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=78

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=63

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=65

jim said...

http://www.smartbargainsus.com/main/index.php

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=1

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=22

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=4

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=7

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=8

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=9

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=15

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=16

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=17

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=19

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=20

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=saleItems

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=2

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=21

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=18

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=20

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=16

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=24

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=78

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=63

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=65

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...

saw this super hot picture... check it out

[url=http://babenudity.com/view/jailbait-tits-boobs/]Jailbait[/url]

Anonymous said...

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

Anonymous said...

Great website, looks very clean and organized. Keep up the good work! antibacterial

Anonymous said...

You wrote a very interesting article. And I agree with you. hair loss Read a useful article about tramadol tramadol

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.