Android 蓝牙不显示配对对话框

发布于 2024-11-14 15:06:48 字数 2573 浏览 4 评论 0原文

我正在尝试将我的 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

I am trying to pair my HTC myTouch 3G with a Bluetooth device that will stream data via SPP to the phone. I have looked at the Chat examples and found them lacking for what I need due to the fact of the high data rates I will need and the Chat example does block on the UI thread. But that said my main problem is when I try to connect a device that isn't currently paired, the Bluetooth API says that it will popup a dialog automatically if the device requires a pairing code. This never happens. How do I make sure that it does? Here is my code...

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

Here is the error I get too:

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

One weird thing about this which seems like a bug, is that if I run this code and it fails then I turn off the Bluetooth and turn it back on the device shows up as paired in the stack. From what I understand the Bluetooth chip on the myTouch is 2.1 and the chip we are trying to connect to is 1.2

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

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

发布评论

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

评论(2

爱情眠于流年 2024-11-21 15:06:48

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

我在开发蓝牙服务时使用了 Nexus S(我实际上使用的是 ListenUsingRfcommWithServiceRecord 方法),它在该手机上运行良好。在索尼爱立信 Xperia ARC 和索尼爱立信 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 });

I'm currently having some troubles with Bluetooth (using SPP) on some phones. One thing you could try, is to use reflection when creating the socket.

I've used a Nexus S while developing my Bluetooth service (I'm actually using the listenUsingRfcommWithServiceRecord-method) it works fine on that phone. Also works fine on SonyEricsson Xperia ARC and SonyEricsson X10 Mini Pro. It didn't work on HTC Wildfire (2.2.1), HTC Legend (2.2) nor on Samsung Galaxy S (2.2.1).

I should also mention that the device I'm receiving data from uses Bluetooth 1.2 too, like yours, so that should not be a problem.

When I tried using reflection I suddenly succeeded on the Wildfire, unfortunately still no progress on Legend nor on the Galaxy S. This is where I'm stuck. Many forums claim some manufacturers have a proprietary Bluetooth stack, so I guess this is what's causing these problems. Anyway, good luck!

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 });
巡山小妖精 2024-11-21 15:06:48

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

Again this seems to be a bug in the bluetooth for that phone, other phones with the same BT chip and version don't have that problem

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