I've written an android application getting data from external sensors using Bluetooth. It's working fine on Sony Ericsson XPERIA but not on a HTC Hero (it finds external devices but it can't get any data from them) I'm wondering why. After some research on the net, I still haven't found any clue. Anyone had similar bluetooth issues on HTC?
Asked
Active
Viewed 3,350 times
3
-
What API level is your Hero running at? – Jack Nov 21 '11 at 16:42
-
Thanks for your response. This HTC runs Android 2.1 so it is API level of 7. I suppose I need to upgrate the platform. right? – user1058169 Nov 25 '11 at 08:56
3 Answers
2
you can make it like this:
private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid())
mSocket.connect();
Just do it.

dragonfly
- 1,151
- 14
- 35
2
If I remember correctly the HTC phones had or [have] an issue at a certain API level (maybe 2.1 and below?). The resolution is reflection.
Reference
Disconnect a bluetooth socket in Android
Service discovery failed exception using Bluetooth on Android
Solution
Instead of using
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
use
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
to get your BluetoothSocket on certain HTC phones using a certain API level.
Solution Expanded
I recently had an application where I had to account for this, and I did not like using this on non-HTC phones, so I had a conditional to check for HTC, if true then use reflection, otherwise dont.
public BTConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the given BluetoothDevice
if (isAnHTCDevice())
{
try
{
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
}
catch (Exception e)
{
Log.e(BCTAG, "Error at HTC/createRfcommSocket: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating htc socket: " + e));
}
}
else
{
try
{
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (Exception e)
{
Log.e(BCTAG, "Error at createRfcommSocketToServiceRecord: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating socket: " + e));
}
}
mmSocket = tmp;
}
isAnHTCDevice():
public boolean isAnHTCDevice()
{
String manufacturer = android.os.Build.MANUFACTURER;
if (manufacturer.toLowerCase().contains("htc"))
return true;
else
return false;
}
-
Thanks a lot for your answer Jack, Unfortunately I still can't open a socket connection, the code stuck on that part: tmp = (BluetoothSocket) m.invoke(device, 1); reflexion is obviously not working but I will upgrade the phone from 2.1 to the latest version. I'll keep you inform if it works. – user1058169 Nov 30 '11 at 16:09
-
1Maybe the '1' should be called as Integet.valueOf(1). Are you sure you want RFComm port 1? – Dani bISHOP Sep 21 '12 at 14:51
1
you can make it like this:
private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid());
mSocket.connect();
Just do it.

dragonfly
- 1,151
- 14
- 35