Bluetooth issues on HTC hero

3.3k views Asked by At

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?

3

There are 3 answers

2
Jack On

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;
}
0
dragonfly On

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.

0
dragonfly On

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.