简体   繁体   English

Android 蓝牙不显示配对对话框

[英]Android Bluetooth not Showing Pairing Dialog

I am trying to pair my HTC myTouch 3G with a Bluetooth device that will stream data via SPP to the phone.我正在尝试将我的 HTC myTouch 3G 与蓝牙设备配对,该设备将通过 SPP 将 stream 数据连接到手机。 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.我查看了聊天示例,发现它们缺少我需要的东西,因为我需要高数据速率,并且聊天示例确实阻塞了 UI 线程。 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.但这就是说我的主要问题是当我尝试连接当前未配对的设备时,蓝牙 API 表示如果设备需要配对代码,它将自动弹出一个对话框。 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据我了解,myTouch 上的蓝牙芯片是 2.1,而我们尝试连接的芯片是 1.2

I'm currently having some troubles with Bluetooth (using SPP) on some phones.我目前在某些手机上遇到蓝牙问题(使用 SPP)。 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.我在开发蓝牙服务时使用了 Nexus S(我实际上使用的是 listenUsingRfcommWithServiceRecord 方法),它在该手机上运行良好。 Also works fine on SonyEricsson Xperia ARC and SonyEricsson X10 Mini Pro.也适用于索尼爱立信 Xperia ARC 和索尼爱立信 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).它不适用于 HTC Wildfire (2.2.1)、HTC Legend (2.2) 和三星 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.我还应该提到,我从中接收数据的设备也使用蓝牙 1.2,就像你的一样,所以这应该不是问题。

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.当我尝试使用反射时,我突然在 Wildfire 上成功了,不幸的是,在 Legend 和 Galaxy S 上仍然没有进展。这就是我卡住的地方。 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 });

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再次,这似乎是该手机的蓝牙中的一个错误,其他具有相同 BT 芯片和版本的手机没有这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM