3

我编写了一个 android 应用程序,使用蓝牙从外部传感器获取数据。它在索尼爱立信 XPERIA 上运行良好,但在 HTC Hero 上运行良好(它可以找到外部设备,但无法从中获取任何数据)我想知道为什么。在网上查了一些资料,还是没有找到任何线索。HTC有人遇到过类似的蓝牙问题吗?

4

3 回答 3

2

如果我没记错的话,HTC 手机在某个 API 级别(可能是 2.1 及以下?)有或[有]问题。分辨率是反射。

参考

断开Android中的蓝牙插座

在 Android 上使用蓝牙的服务发现失败异常

解决方案

而不是使用

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

利用

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

使用特定 API 级别在某些 HTC 手机上获取您的 BluetoothSocket。

解决方案扩展

我最近有一个应用程序,我必须考虑这一点,我不喜欢在非 HTC 手机上使用它,所以我有一个条件来检查 HTC,如果为真则使用反射,否则不要。

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;
}
于 2011-11-25T18:47:21.947 回答
2

你可以这样:

private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid            
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid())    
mSocket.connect();

去做就对了。

于 2012-09-29T03:16:13.820 回答
1

你可以这样:

private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid

mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid());

mSocket.connect();

去做就对了。

于 2012-09-29T03:11:36.693 回答