HTC Hero 的蓝牙问题

发布于 2024-12-17 09:42:30 字数 153 浏览 0 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心小果奶 2024-12-24 09:42:30

你可以这样做:

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。

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.

迷爱 2024-12-24 09:42:30

如果我没记错的话,HTC 手机在某个 API 级别(可能是 2.1 及以下?)有问题。解决办法就是反思。

参考

在 Android 中断开蓝牙套接字

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

解决方案

不要使用

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

use

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;
}

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;
}
终陌 2024-12-24 09:42:30

你可以这样做:

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

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

mSocket.connect();

去做就对了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文