安卓蓝牙未显示配对对话框

6

我正在尝试将我的HTC myTouch 3G与一个通过SPP向手机流数据的蓝牙设备配对。我查看了聊天示例,发现它们不适合我所需的高数据速率,并且聊天示例会在UI线程上阻塞。但是,我的主要问题是当我尝试连接一个当前未配对的设备时,蓝牙API会自动弹出一个对话框,如果设备需要配对码。这从未发生过。我该如何确保它发生?以下是我的代码...

BluetoothSocket btSocket;
String macAddress = data.getStringExtra("mac");
Log.d(TAG, "Found Device " + macAddress);

// Get the Bluetooth adapter on the device
BluetoothAdapter bta = ((MyApplication)this.getApplication()).getBtState();
BluetoothDevice btDevice = bta.getRemoteDevice(macAddress);
BluetoothSocket tmp = null;
try {
    tmp = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
    e.printStackTrace();
}
if (tmp != null) {
    btSocket = tmp;
    bta.cancelDiscovery();

    try {
        btSocket.connect();
    } catch (IOException e) {
        try {
            Log.e(TAG, "------------- Close IOException");
            btSocket.close();
        } catch (IOException e2) {
            Log.e(TAG, "unable to close() socket during connection failure", e2);
        }
    }
}   

我也遇到了以下错误:

ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Adapter:DeviceCreated from /org/bluez/14284/hci0
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/14284/hci0
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
DEBUG/BluetoothService(149): updateDeviceServiceChannelCache(00:02:5B:00:A5:0B)
DEBUG/BluetoothService(149):     uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1
DEBUG/BluetoothService(149): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1
VERBOSE/BluetoothEventRedirector(13691): Received android.bleutooth.device.action.UUID
ERROR/MainApp(14272): ------------- Close IOException
ERROR/BluetoothService.cpp(149): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
VERBOSE/BluetoothEventRedirector(13691): Received android.bleutooth.device.action.UUID

在这个问题中有一件奇怪的事情,看起来像是一个错误,就是如果我运行这段代码并且失败了,然后我关闭蓝牙再重新打开它,设备会显示为已配对。据我所知,myTouch上的蓝牙芯片是2.1,我们尝试连接的芯片是1.2。

2个回答

4

我目前在一些手机上遇到了蓝牙(使用SPP)的一些问题。你可以尝试的一件事是,在创建套接字时使用反射。

在开发我的蓝牙服务时,我使用了Nexus S(实际上我正在使用listenUsingRfcommWithServiceRecord方法),它在那部手机上运行良好。在SonyEricsson Xperia ARC和SonyEricsson X10 Mini Pro上也很好用。但是在HTC Wildfire(2.2.1)、HTC Legend(2.2)和Samsung Galaxy S(2.2.1)上无法正常工作。

我还应该提到,我从中接收数据的设备也使用了蓝牙1.2,就像你的设备一样,所以这不应该是一个问题。

当我尝试使用反射时,在Wildfire上突然成功了,但是在Legend和Galaxy S上仍然没有进展。这就是我卡住的地方。许多论坛声称一些制造商有专有的蓝牙堆栈,所以我想这可能是导致这些问题的原因。无论如何,祝你好运!

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

Method m = mAdapter.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { UUID.class });
tmpSocket = (BluetoothServerSocket) m.invoke(mAdapter, new Object[] { MY_UUID });

专有的蓝牙堆栈在许多设备中确实经常实现不佳,非常遗憾。非常正确。 - Terrance
将三星Galaxy S升级到2.3.3版本后,该手机的问题得到了解决,而且没有更改应用程序中的任何内容。我想这证明了专有Bt堆栈存在的问题。 - Fredricus
这是很好的信息,谢谢大家。Fredricus,你还记得你的设备之前是哪个版本吗? - TacB0sS

1

这似乎是该手机蓝牙中的一个错误,使用相同BT芯片和版本的其他手机没有这个问题


嗨,我遇到了同样的问题,你确定这是设备特定的问题吗?我在多个设备上都遇到了这个问题,还有这个问题:http://stackoverflow.com/questions/13520575/android-4-0-bluetooth-connection-error-to-an-embedded-device-permission-denie - TacB0sS
1
我明白你的意思,我相信这与设备、操作系统以及蓝牙芯片有关。这完全取决于制造商如何解释蓝牙规范。我在一些三星、摩托罗拉、LG等品牌的手机上看到过这种情况,同一家制造商的不同手机也会出现这种情况,但它们都可以正常工作。这真的是赌运气。所以,如果可能的话,我会跳过所有这些并使用不安全的连接来绕过它。 - JPM
你看到我帖子里的解决方案了吗?那可能是你的问题所在吗?你用哪种方式在你的应用程序中启用蓝牙? - TacB0sS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接