Overcoming Android bluetooth “blues” with Reflection method


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 Stack 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’s BlueTerm, 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;


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:

Opening screen of BlueTerm App

First screen of BlueTerm App
showing “not connected”

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:

Screen showing paired device

Screen showing paired device

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:

Screen showing connected SPP

Screen showing connected SPP

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:

Screen showing disconnect device

Screen showing disconnect device

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.


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.

About Tayeb

Electronics engineer, part-time webmaster and owner of "Aliatron", a tech-oriented company registered in Portugal and Mozambique. Owner of "EU Halal", a trading and consulting company in Halal & Tayyib, 100% stun-free compliant.
This entry was posted in Mobile Apps, Programming and tagged , , , , , . Bookmark the permalink.

19 Responses to Overcoming Android bluetooth “blues” with Reflection method

  1. wailedhero says:

    Thats why I never purchased any chinese smartphone 😉
    But good to know that you learned something 🙂

  2. Pingback: BlueTerm app for most Android devices | RedAcacia

  3. Chao says:

    I download the source code for BlueScripts and I can’t find the file named BluetoothSerialService.java. Can you send me the source code for Blueterm_RM.apk?
    Thank you!

    • Tayeb says:

      Welcome to blog. You have to read the article. I show how to do itt. You also need source code of Blueterm’s original code. If you know how to compile in Eclipse you will know how to do it.

  4. alandubs says:

    Thanks for this info, I’m developing some Android BT – I haven’t seen these issues you mention but I’ve bookmarked this page in case I do run into issues. Btw – you mention and link to stockoverflow.com whereas I’m fairly certain you meant stackoverflow.com

    • Tayeb says:

      Yes you are right it is stackoverflow. I will edit the post and write correctly. It seems the bluetooh issue is still prevalent in some Android phones.

  5. Hitesh singh says:

    hey tayeb ,
    i have some issue with this code .. its not connected with my Spp device .. what to do .. my device is bar code scanner. please revert me solution on hiteshsingh1101@gmail.com
    thanks in advance

    • Tayeb says:

      Welcome to my blog. You will need to check into code and modify it. At the moment thee code only allows sending commands not to receive any data. So you will not be able to use with a scanner.

  6. basher051 says:

    Dear Tayeb,
    I am trying to connect my android device to RN42 module.The bluetooth connection works almost perfectly when my device is Samsung S2, but when I try it using iball 3G 7271(a chinese device) I see that my connection to the RN42 is very inconsistent.It throws exceptions like- Service Discovery failed,connection is not created(failed or aborted) to name a few.Whereas I never have seen this errors while running the same code on S2.

    Log cat for S2(working fine):
    03-13 09:46:38.188: I/AppFuntions(21103): iballAdapter -android.bluetooth.BluetoothAdapter@41fe9250
    03-13 09:46:38.188: I/AppFuntions(21103): RN42_Device – 00:06:66:49:57:60
    03-13 09:46:38.188: V/BluetoothSocket.cpp(21103): initSocketNative
    03-13 09:46:38.188: V/BluetoothSocket.cpp(21103): …fd 66 created (RFCOMM, lm = 26)
    03-13 09:46:38.188: V/BluetoothSocket.cpp(21103): initSocketFromFdNative
    03-13 09:46:38.193: I/BluetoothConn(21103): connecting to Socket
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): abortNative
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): …asocket_abort(53) complete
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): destroyNative
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): …asocket_destroy(53) complete
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): abortNative
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): …asocket_abort(66) complete
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): destroyNative
    03-13 09:46:38.193: V/BluetoothSocket.cpp(21103): …asocket_destroy(66) complete
    03-13 09:46:38.198: V/BluetoothSocket.cpp(21103): initSocketNative
    03-13 09:46:38.198: V/BluetoothSocket.cpp(21103): …fd 53 created (RFCOMM, lm = 26)
    03-13 09:46:38.198: V/BluetoothSocket.cpp(21103): initSocketFromFdNative
    03-13 09:46:38.203: D/BluetoothUtils(21103): isSocketAllowedBySecurityPolicy start : device null
    03-13 09:46:38.753: V/BluetoothSocket.cpp(21103): connectNative
    03-13 09:46:39.178: V/BluetoothSocket.cpp(21103): …connect(53, RFCOMM) = 0 (errno 115)
    03-13 09:46:39.183: I/AppFunctions(21103): DataOut -android.bluetooth.BluetoothOutputStream@41e8b688
    03-13 09:46:39.183: I/AppFunctions(21103): DataIn -android.bluetooth.BluetoothInputStream@41e95790

    Logcat for iball 3G 7271(one of the exceptions I get often) :

    03-13 09:51:39.724: I/AppFuntions(10598): iballAdapter – android.bluetooth.BluetoothAdapter@426ebb88
    03-13 09:51:39.725: I/AppFuntions(10598): RN42_Device – 00:06:66:49:57:60
    03-13 09:51:39.725: I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
    03-13 09:51:39.725: I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
    03-13 09:51:39.729: I/BluetoothConn(10598): connecting to Socket
    03-13 09:51:39.730: I/BluetoothSocket_MTK(10598): [JSR82] close
    03-13 09:51:39.730: I/BluetoothSocket_MTK(10598): [JSR82] readLock got.
    03-13 09:51:39.731: I/BluetoothSocket_MTK(10598): [JSR82] Bluetooth Socket Constructor
    03-13 09:51:39.731: I/BluetoothSocket_MTK(10598): [JSR82] type=1 fd=-1 auth=true encrypt=true port=-1
    03-13 09:51:39.733: I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP
    03-13 09:51:40.068: D/GcmSyncTest(10598): millisUntilFinished16962
    03-13 09:51:41.069: D/GcmSyncTest(10598): millisUntilFinished15961
    03-13 09:51:41.257: I/BluetoothSocket_MTK(10598): [JSR82] SdpHelper::onRfcommChannelFound: channel=1
    03-13 09:51:41.258: I/BluetoothSocket_MTK(10598): [JSR82] connect: do SDP done; mPort=1
    03-13 09:51:41.259: D/tharun(10598): inside try 1
    03-13 09:51:41.260: W/System.err(10598): java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
    03-13 09:51:41.261: W/System.err(10598): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:395)
    03-13 09:51:41.261: W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions.connect(AppFunctions.java:193)
    03-13 09:51:41.262: W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions.access$3(AppFunctions.java:176)
    03-13 09:51:41.263: W/System.err(10598): at com.example.guardmonitor.commons.AppFunctions$1.run(AppFunctions.java:109)
    03-13 09:51:41.263: W/System.err(10598): at java.util.Timer$TimerImpl.run(Timer.java:284)
    03-13 09:51:41.263: I/AppFunctions(10598): DataOut -null
    03-13 09:51:41.263: I/AppFunctions(10598): DataIn -null

    My doubts:

    1) What is the difference between Bluetooth_MDK and Bluetooth.cpp?
    2) What is SSP and SDP?
    3) Will my using the reflection method ensure that my connection is always successful in the latter device.
    I am searching a soln for this for almost a week but in vain,Pl reply/mail me as I need to deliver this project soon.Thanks !

    • Tayeb says:

      Welcome to my bog. I can state that you need to learn a bit more to learn about SPP. I have shown a method. If you pinpoint the problems I can perhaps help but without knowing what you are doing and emulating them I cannot say really what is happening.

  7. Mehmet AVŞAR says:

    I have 4.0.4 (API 15) on my tablet but I still need reflection to create RFCOMM socket.

    • Tayeb says:

      Welcome to my blog. What do you want to do?

      • Mehmet AVŞAR says:

        Just wanted to share my experience related with your article, that is, contrary to your saying, though I have a device with higher then API 10 i still needed to use reflection for creating RFCOMM socket. I mean,

        BluetoothSocket socket = mPrinter.createInsecureRfcommSocketToServiceRecord( UUID.fromString(“00001108-0000-1000-8000-00805F9B34FB”))

        failed to run. Whereas

        Method m = mPrinter.getClass().getMethod(“createRfcommSocket”, new Class[] {int.class});
        BluetoothSocket socket = (BluetoothSocket) m.invoke(mPrinter, 1);

        ran OK. And my manifest file has this tag:

        It would be great, if you know that why this reflection trick is still needed.

      • Tayeb says:

        If your Bluetooth works you do not need any technique to make it work. I have edited your comment and removed all that is not relevant

      • Mehmet AVŞAR says:

        Wish you removed all of the comments.

  8. Mehmet AVŞAR says:

    Ops, forgot to remove tags.

    <uses-sdk android:minSdkVersion=”14″ android:targetSdkVersion=”14″/>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s