2

私はAndroid2.3.6とBluetoothの狂気と戦っています。AndroidのBluetooth設定から、別のデバイスをリンクしました。ここまでは順調ですね。問題は、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**
4

1 に答える 1

1

これは、オープンソースのBluetoothデバッガーアプリである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、クラスの概要に次のように記載されています。

次に、createRfcommSocketToServiceRecord(UUID)を使用して、リモートデバイスと通信するためにBluetoothSocketを開くことができます。

あまり説明していませんが、UUIDはアプリケーション用に生成したものです(たとえば、コマンドラインツールを使用して)。この例では、UUIDをスローしたにもかかわらず、これによって返されたものが接続プロセスで何らかの形で使用されるuuidgenというのが私の勘です。結果を離れて。BluetoothSocketcreateRfcommSocketToServiceRecord

いずれにせよ、ドキュメントには、このメソッドを呼び出す必要があると明確に記載されています。コードに表示されていないため、おそらくこれは不足している部分です。

ところで、ここでオープンソースアプリのソースコードを見つけることができます:https ://github.com/janosgyerik/bluetoothviewer

コードの変更を開始する前に、このアプリを使用して健全性チェックを実行し、Bluetoothデバイスに接続できるかどうかを確認できます。はいの場合、実用的な例とソースコードがあります。Google Playのアプリ: https ://play.google.com/store/apps/details?id = net.bluetoothviewer

于 2013-02-16T07:31:52.847 に答える