Reputation: 1188
I'm fighting with the craziness of Android 2.3.6 and Bluetooth. From Android bluetooth settings I linked another device. So far, so good. The problem is when I try to connect the device via Android code, I get the error: 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**
Upvotes: 2
Views: 8679
Reputation: 124734
This is how I do it in BluetoothViewer, an open-source Bluetooth debugger app:
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;
The difference from your program is the call to createRfcommSocketToServiceRecord
. It may look strange that I throw away the result (overwritten 2 lines down), and it's a been a while I did this I don't recall the details.
If you take a look at the docs of BluetoothDevice
, it says in the class overview:
You can then open a BluetoothSocket for communication with the remote device, using createRfcommSocketToServiceRecord(UUID).
It does not explain much, but the UUID is something you generated for your application (for example with the uuidgen
command line tool), and my hunch is that the BluetoothSocket
returned by this createRfcommSocketToServiceRecord
is used somehow in the connection process, even though in this example I threw away the result.
In any case, the doc clearly says that you need to call this method, and since I don't see it in your code, probably this is the missing piece for you.
Btw, you can find the source code of the open source app here: https://github.com/janosgyerik/bluetoothviewer
Before you even start changing your code, you can do a sanity check using this app and see if it can connect to your Bluetooth device. If yes then you have a working example and source code. The app on Google Play: https://play.google.com/store/apps/details?id=net.bluetoothviewer
Upvotes: 1