3

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?

user1058169
  • 31
  • 1
  • 2

3 Answers3

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;
}
Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • 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
  • 1
    Maybe 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