Overcoming Android bluetooth “blues” with Reflection method
Introduction
It is an Android problem: bluetooth does not function in a satisfactory manner. Having recently started to “hack” into Android devices, and learning while doing, I was immediately confronted with the real world of something that does not fully work (bluetooth in Android devices) at least not satisfactorily as with J2ME devices I worked with before (Nokia and Blackberry). It is worse if you do, as I did, in buying an Android smartphone in China (A3 Star, HTC T8585 clone) . You will be immediately confronted with the merchants that are insensitive to your requests for technical support. Effectively you learn immediately that there is nobody out there to talk to.
Android bluetoooth “blues”
When I connected my Android phone to external devices, in my case a bluetooth module, the immediate response was an “unable to connect” toast. “Googling“ made me desperate when the enormity of information confused me even more. How to approach the problem, what is wrong with my cheap chinese Android phone? MediaTek the chipset manufacturer of the device’s processor does not “leak” out any source codes to be able to hack bluetooth libraries for example.
Reflection method – solution to connection problem
If you seek quality technical responses I advise you Stock Overflow. That is where I finally learnt a few things such as the problem of connection is overcome with Reflection method.
Technically-speaking the reason is that the widely used connection method CreateInsecureRfcommSocketToServiceRecord() was only included starting with Android API Level 10. When targeting an API lower than 10 (ex 2.3.3 or Gingerbread), the method is not publicly accessible. The Reflection method overcomes that.
So in my chinese HTC clone “A3 Star” I have now a fully working Android market’sBlueTerm, a free app that I use for my engineering tests with external devices. I had to hack into BlueTerm´s source code (it is publicly available at http://pymasde.es/blueterm/) in order to change the method of connection to the Reflection one.
Looking into libraries of my phone’s processor MTK6573, I noted that they were made in 2008, and were never updated. Perhaps in China (the primary target of MTK processors) they do not use SPP at all, and are insensible to “techie“ learners like me wanting to use SPP connection in bluetooth.
On Reflection method among others, you can read more at Stock Overflow site
The code mod made to BlueTerm app
Additionally a special credit is due to Teholabs and their source code for BlueScripts app and the connection method of Reflection. You need to open in your adequate Eclipse environment BluetoothSerialService.java and add at the begining of the file:
Just before:
import java.util.UUID;
Add:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
And down in the file find:
try {
tmp = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
And substitute with
Method m;
try {
m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class });
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (SecurityException e) {
Log.e(TAG, "create() failed", e);
} catch (NoSuchMethodException e) {
Log.e(TAG, "create() failed", e);
} catch (IllegalArgumentException e) {
Log.e(TAG, "create() failed", e);
} catch (IllegalAccessException e) {
Log.e(TAG, "create() failed", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
You are now ready to compile and download the BlueTerm app into your Andorid device, hopefully without any errors.
The Results
You need to first of all to pair your Android phone to the external module. So I assume you have paired your phone to external bluetooth module. Now we are ready to start the BlueTerm app. This is the first screen you see when you start BlueTerm app:
We next find the paired device pressing the menu button of your Android phone and choose “Connect Device“. You will see the paired devices including the module you have just paired:
Choose the device and immediately BlueTerm will show the message at the top right of the screen “connecting…”.
If connection is successful the folowing screen will show with virtual keyboard to send commands:
To leave the application you have to press the menu button on your device and you will now have to disconnect. It is advisable to disconnect:
UPDATE: For non-techincal users of Android devices I have made available a ready to install BlueTerm app at my most recent post. Before downloading the apk, please read my disclaimer on the same page.
Bugs and Improvements
This mod is quite fresh and needs to be tested further. I have had to reboot once my HTC clone as the app froze when I tried to disconnect. Any improvements and code sharing from visitors to this blog will be appreciated.
Moral of history
If you want a fully working smartphone do not buy in China, and if you really want to take the risk, check beforehand on the reliability of your supplier.
However I confess, I have an inside feeling of gratitude to the chinese, because if everything was working fine, I would have had no challenges to overcome, and would have not learnt much about Android bluetooth. The chinese manufacturers do not make available source codes, while non-chinese manufacturers do, or have “leaked” source codes.
Source codes mean opportunities to mod and to improve and a chinese manufacturers do not seem to “leak” these codes.
Conclusions
Just as Bluescripts does the next step for me will be to connect in default way, and then in alternate way (update: I have already made a ready to install modded app of Blueterm – check above).
From now on, I will be able to write new apps for my chinese HTC clone, using Reflection method. The Reflection method is only valid for situations similar to mine. I hope that sharing this work of mine will help others with similar error messages “unable to connect” in their Android devices.
No comments:
Post a Comment