2013-02-16 114 views
2

我與Android 2.3.6和藍牙的瘋狂戰鬥。 從Android的藍牙設置,我鏈接了另一臺設備。 到目前爲止,這麼好。 問題是,當我嘗試連接使用Android代碼的設備,我得到的錯誤:連接被拒絕Android BLUETOOTH連接拒絕

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

回答

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; 

diff從您的程序中選擇電話createRfcommSocketToServiceRecord。看起來很奇怪,我扔掉了結果(重寫了2行),而且我已經有一段時間了,我不記得細節。

如果你看一看的BluetoothDevice的文檔,它說在類概述:

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

它並沒有解釋太多,但UUID是你的應用程序生成的東西(例如與uuidgen命令行工具),我的直覺是,這個createRfcommSocketToServiceRecord返回的BluetoothSocket以某種方式在連接過程中使用,即使在這個例子中我扔掉了結果。

在任何情況下,文檔清楚地表明您需要調用此方法,並且由於我沒有在您的代碼中看到它,所以可能這是您的缺失部分。

順便說一句,你可以找到開源應用在這裏的源代碼:https://github.com/janosgyerik/bluetoothviewer

之前,你甚至開始改變你的代碼,你可以使用這個程序做一個全面的檢查,看看它是否可以連接到您的藍牙設備。如果是,那麼你有一個工作的例子和源代碼。 Google Play上的應用程序: https://play.google.com/store/apps/details?id=net.bluetoothviewer

+0

HI,我添加了UUID final UUID applicationUUID = UUID.fromString(「00001101-0000-1000-8000-00805F9B34FB」);但我收到:02-16 11:36:14.757:W/System.err(2829):java.io.IOException:連接被拒絕 – toto 2013-02-16 14:06:59

+0

您可以安裝BluetoothViewer應用程序並查看是否可以使用它連接到您的設備?如果是的話,那麼你的應用程序的源代碼中包含所有你需要的信息,如果沒有,那麼我沒有你的答案。 – janos 2013-02-16 14:23:32

+0

你好,我安裝了程序BViewer 完全問題是遠程BT設備。 感謝您的幫助。 – toto 2013-02-16 15:24:27