gpt4 book ai didi

安卓蓝牙连接被拒绝

转载 作者:行者123 更新时间:2023-11-29 01:56:23 26 4
gpt4 key购买 nike

我正在与 Android 2.3.6 和蓝牙的疯狂作斗争。在 Android 蓝牙设置中,我链接了另一台设备。到目前为止,一切都很好。问题是当我尝试通过 Android 代码连接设备时,出现错误:connection refused

Example code summary:

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

Log Error:

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**

最佳答案

这就是我在 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 的文档,它在类概述中说:

You can then open a BluetoothSocket for communication with the remote device, using createRfcommSocketToServiceRecord(UUID).

它并没有解释太多,但 UUID 是您为应用程序生成的东西(例如使用 uuidgen 命令行工具),我的直觉是 BluetoothSocketcreateRfcommSocketToServiceRecord 返回的内容在连接过程中以某种方式使用,即使在此示例中我丢弃了结果。

无论如何,文档清楚地表明您需要调用此方法,并且由于我在您的代码中没有看到它,所以这可能是您缺少的部分。

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

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

关于安卓蓝牙连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14906721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com