Android BLUETOOTH连接遭拒绝

时间:2013-02-16 03:49:20

标签: android core-bluetooth

我正在与Android 2.3.6和蓝牙的疯狂斗争。 从Android蓝牙设置我链接另一个设备。 到现在为止还挺好。 问题是当我尝试通过Android代码连接设备时,出现错误:连接被拒绝

  

示例代码摘要:

BluetoothAdapter mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device =  mBluetoothAdapter.getRemoteDevice("the mac address here");
BluetoothSocket socket;
Method m;
m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 1);
mBluetoothAdapter.cancelDiscovery();
socket.connect();
  

记录错误:

02-16 01:31:30.617: D/BluetoothSocket(29864): create BluetoothSocket: type = 1, fd = -1, uuid = [null], port = 1
02-16 01:31:30.617: D/BTL_IFC_WRP(29864): wrp_wsock_create: BTS
02-16 01:31:30.625: D/BTL_IFC_WRP(29864): wrp_alloc_new_sock: wrp_alloc_new_sock sub 16
02-16 01:31:30.625: D/BTL_IFC_WRP(29864): wrp_wsock_create: 50
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): btsk_alloc_add: success
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): btsk_dump_list:  fd (-1:47), bta -1, rc 1, wflags 0x100, cflags 0x4, port 0
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): btsk_dump_list:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_setsockopt:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_setsockopt:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.625: D/BLZ20_ASOCKWRP(29864): asocket_init
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_fcntl:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_fcntl:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_fcntl: transparant fcntl
02-16 01:31:30.625: D/BLZ20_ASOCKWRP(29864): asocket_connect
02-16 01:31:30.625: D/BLZ20_WRAPPER(29864): blz20_wrp_connect:  fd (-1:50), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
02-16 01:31:30.632: D/BLZ20_WRAPPER(29864): blz20_wrp_connect: success
02-16 01:31:30.632: D/BLZ20_WRAPPER(29864): blz20_wrp_poll: pending connect fd (-1:50), bta -1, rc 1, wflags 0x0, cflags 0x1, port 0
02-16 01:31:30.632: D/BLZ20_WRAPPER(29864): btlif_wait_response: id(s) |BTLIF_BTS_RFC_CON_RSP|BTLIF_BTS_RFC_DISC_IND|
02-16 01:31:31.218: D/BLZ20_WRAPPER(29864): btlif_signal_event:  fd (-1:50), bta -1, rc 1, wflags 0x900, cflags 0x4, port 0
02-16 01:31:31.218: D/BLZ20_WRAPPER(29864): btlif_signal_event: event BTLIF_BTS_RFC_DISC_IND matched
02-16 01:31:31.218: D/BLZ20_WRAPPER(29864): btlif_wait_response: unblocked fd (-1:50), bta -1, rc 1, wflags 0x100, cflags 0x4, port 0
02-16 01:31:31.218: D/BLZ20_WRAPPER(29864): blz20_wrp_poll: set errno 111 (Connection refused) l.2089 
**02-16 01:31:31.218: W/System.err(29864): java.io.IOException: Connection refused**

1 个答案:

答案 0 :(得分:1)

这就是我在开源蓝牙调试器应用程序BluetoothViewer中的做法:

BluetoothSocket tmp = null;
try {
    tmp = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
    Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    tmp = (BluetoothSocket) m.invoke(mDevice, 1);
} catch (IOException e) {
    Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;

与您的计划的不同之处在于对createRfcommSocketToServiceRecord的调用。我扔掉结果可能看起来很奇怪(覆盖了2行),而且我已经做了一段时间了,我不记得细节了。

如果您查看BluetoothDevice的文档,则会在课程概述中说明:

  

然后,您可以打开BluetoothSocket以与遥控器进行通信   device,using createRfcommSocketToServiceRecord(UUID)。

它没有解释,但UUID是您为应用程序生成的(例如使用uuidgen命令行工具),而我的预感是此BluetoothSocket返回的createRfcommSocketToServiceRecord 1}}在连接过程中以某种方式使用,即使在这个例子中我扔掉了结果。

在任何情况下,文档都清楚地说你需要调用这个方法,因为我没有在你的代码中看到它,这可能是你缺少的部分。

顺便说一下,您可以在此处找到开源应用的源代码:https://github.com/janosgyerik/bluetoothviewer

在开始更改代码之前,您可以使用此应用程序进行完整性检查,看看它是否可以连接到您的蓝牙设备。如果是,那么您有一个工作示例和源代码。 Google Play上的应用: https://play.google.com/store/apps/details?id=net.bluetoothviewer