Android蓝牙不显示配对对话框

问题描述 投票:5回答:2

我正在尝试将我的HTC myTouch 3G与蓝牙设备配对,蓝牙设备将通过SPP将数据传输到手机。我查看了聊天示例,发现它们缺少我需要的东西,因为我需要高数据速率,而Chat示例会在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

android android-bluetooth pairing
2个回答
4
投票

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

我在开发蓝牙服务时使用了Nexus S(我实际上使用的是listenUsingRfcommWithServiceRecord方法),它在手机上工作正常。也适用于SonyEricsson Xperia ARC和SonyEricsson X10 Mini Pro。它不适用于HTC Wildfire(2.2.1),HTC Legend(2.2)和三星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 });

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.