私はアンドロイドBluetoothの例を研究しました。
私が混乱しているのは、他のデバイスが接続要求を送信しており、以前の接続が存在していた場合、サーバーが受け入れられずに長い間ブロックされていることです。
サーバーソケットを実行し、1 つの接続だけでなく多くの接続を受け入れることができる Android デバイスは可能ですか? サーバーがaccept()によってブロックされる原因となる理由はありますか?たとえば、UUIDが正しくないなどの理由はありますか?
私は次のように操作を行います、
- 最初の接続では、サーバーソケットを作成し、接続を待ちます
- クライアント B が接続要求を送信する
- サーバーソケット受け入れ
- I/O ストリームを処理する
- 同じサーバーソケットを使用して、再度接続を待ちます (サーバーソケットを閉じません)
- クライアント C が接続要求を送信する
- サーバーソケットは、受け入れられるのではなく、ブロックされ、ブロックされ、ブロックされます...
最初の接続が成功し、2 番目の接続で UUID を変更しないためです。したがって、UUIDは重要ではないと思います。最初の接続が確立されると、サーバーソケットは別の接続を待ちます。悲しいことに、最初の接続として受け入れず、待機してブロックするだけです。
public void run() {
if (D) Log.d(TAG, "BEGIN ServerSocketThread" + this);
BluetoothSocket socket = null;
while(true) {
try {
Log.i(TAG, "[ServerSocketThread] Enter while loop");
socket = mmServerSocket.accept();
Log.i(TAG, "[ServerSocketThread] Got client socket");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
if (socket!=null) {
synchronized (BluetoothConnService.this) {
Log.i(TAG, "[ServerSocketThread] "+socket.getRemoteDevice()+" is connected.");
ConnectedThread tmpThread = new ConnectedThread(socket);
tmpThread.start();
break;
}
}
}
BluetoothConnService.this.startSession();
}
これは、サーバー ソケット スレッドの一部です。2 番目の接続を作成するときはいつでも、logcat はログ「[ServerSocketThread] Enter while loop. 」の後に停止することを示しています。操作はaccept()でブロックされているようですが、serversocket は mmServerSocket の IOException をスローしませんでした。受け入れる()。
2番目の接続を作成したい場合、ソケットが受け入れられず、例外がスローされず、そこでブロックされている理由がわかりません。考えられる理由や提案があれば幸いです。
アプリケーションを実行したときのカタログを次に示します。問題の特定に役立つことを願っています。
パート A : 正常な接続と最初の接続です
12-12 22:42:05.358: DEBUG/BluetoothSppPort(889): Creating a BluetoothSpp proxy object
12-12 22:42:05.378: DEBUG/BluetoothSppService(122): createPort called!
12-12 22:42:05.378: DEBUG/BluetoothSppService(122): createPort checking uuid
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort UUID=00001101-0000-1000-8000-00805f9b34fb auth=true encrypt=true
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort enforcing bluetooth perm
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort creating a jbtlspp object
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort checking if the btl spp object is valid
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort try to create an spp container
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort try to create security params
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort Set Security L2
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort spp port create
12-12 22:42:05.418: DEBUG/JBtlSpp(122): create: Entered
12-12 22:42:05.418: DEBUG/JBtlSpp(122): Calling NativeJBtlSpp_Create
12-12 22:42:05.418: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Entered
12-12 22:42:05.418: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Calling BTL_SPP_Remote_Create
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: BTL_SPP_Remote_Create returned 0, context:f
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Setting context value in jContext out parm
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Calling Java setValue(0xf) in context's class
12-12 22:42:05.598: DEBUG/JBtlProfileContext(122): setValue: setValue called, value:15
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): create_spp_port_data: will use context struct 0 for the port 15
12-12 22:42:05.608: DEBUG/JBtlSppNative(122): create_spp_port_data: spp port context 0 added
12-12 22:42:05.608: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create:Exiting Successfully
12-12 22:42:05.778: DEBUG/JBtlSpp(122): After NativeJBtlSpp_Create, status=SUCCESS, Context = 15
12-12 22:42:05.778: DEBUG/JBtlRbtlServices(122): addUser: Entered, userRefCount = 1
12-12 22:42:05.778: DEBUG/BluetoothSppService(122): port create returned status SUCCESS
12-12 22:42:05.778: DEBUG/JBtlSpp(122): enable: Entered
12-12 22:42:05.778: DEBUG/JBtlSpp(122): enable: UUID=00001101-0000-1000-8000-00805f9b34fb
12-12 22:42:05.778: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable: Entered
12-12 22:42:05.978: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable: BTL_SPP_Enable returned 0
12-12 22:42:05.978: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable:Exiting
12-12 22:42:05.978: DEBUG/JBtlSpp(122): After NativeJBtlSpp_Enable, status=SUCCESS
12-12 22:42:05.978: DEBUG/JBtlSpp(122): enable: Exiting
12-12 22:42:05.978: DEBUG/BluetoothSppService(122): port enable returned status SUCCESS
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN_IND: Entered
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN_IND: Exiting
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: Entered
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: status: 0 context:15
12-12 22:42:57.708: DEBUG/JBtlSpp(122): nativeCb_open: Entered from 00:10:60:56:83:28
12-12 22:42:57.718: DEBUG/JBtlSpp(122): nativeCb_open: Calling callback
12-12 22:42:57.718: DEBUG/BluetoothSppService(122): connected called!
12-12 22:42:57.718: DEBUG/BluetoothSocket(889): SppPort connected()
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: Exiting
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Entered
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Exiting
パート B : 2 回目の接続で、serversocket が受け入れられずにブロックされます。
パート A のようにBluetoothSppServiceを実行していなかったようですが、BluetoothSppServiceは andorid フレームワークの下位層であるため、処理方法がわかりません。
12-12 22:43:29.158: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Entered
12-12 22:43:29.168: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: context: 1, errCode: 0
12-12 22:43:29.168: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Calling Java Link Connect Indication callback
12-12 22:43:29.178: DEBUG/JBtlBmg(122): nativeLinkConnectInd
12-12 22:43:29.218: DEBUG/BluetoothService(122): Callback - linkConnectInd, btErrCode = NO_ERROR, bdAddr = C0:E4:22:18:ED:C1
12-12 22:43:29.258: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Entered
12-12 22:43:29.258: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Calling NativeJBtlBmg_GetKnownDeviceInfo
12-12 22:43:29.258: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo: Entered
12-12 22:43:29.258: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo: Calling BTL_BMG_GetKnownDeviceInfo
12-12 22:43:29.268: DEBUG/JBtlBmgJniKnownDeviceInfo(122): setValues: Entered
12-12 22:43:29.268: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo:Exiting
12-12 22:43:29.268: DEBUG/JBtlBmg(122): getKnownDeviceInfo: After NativeJBtlBmg_GetKnownDeviceInfo, status=SUCCESS
12-12 22:43:29.278: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Exiting
12-12 22:43:29.278: DEBUG/BluetoothService(122): onRemoteDeviceConnected, device C0:E4:22:18:ED:C1 is Paired
12-12 22:43:29.278: DEBUG/BluetoothService(122): Sending ACTION_ACL_CONNECTED intent, address = C0:E4:22:18:ED:C1
12-12 22:43:29.278: DEBUG/BluetoothA2dpService(122): Received intent with action: android.bluetooth.device.action.ACL_CONNECTED
12-12 22:43:29.298: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Exiting
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: Entered
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: context: 1, errCode: 19
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: Calling Java Link Disconnect callback
12-12 22:43:29.988: DEBUG/JBtlBmg(122): nativeLinkDisconnect
12-12 22:43:29.998: DEBUG/BluetoothService(122): Callback - linkDisconnect, btErrCode = USER_TERMINATED, bdAddr = C0:E4:22:18:ED:C1, status = NO_ERROR
12-12 22:43:30.018: DEBUG/BluetoothService(122): Sending ACTION_ACL_DISCONNECTED intent, address = C0:E4:22:18:ED:C1
パート C: 以前の既存の接続を切断すると、次のように実行されます。そしてパート A を走らせます。
BluetoothSppプロキシ オブジェクトを作成し、BluetoothSppServiceを実行します。
12-12 22:45:45.758: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Entered
12-12 22:45:45.758: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Exiting
12-12 22:45:45.788: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSE_IND: Entered
12-12 22:45:45.788: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSE_IND: Exiting
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: Entered
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: status: 0 context:15
12-12 22:45:45.798: DEBUG/JBtlSpp(122): nativeCb_closed: Entered
12-12 22:45:45.798: DEBUG/BluetoothSppService(122): close called!
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: Exiting
12-12 22:45:46.158: DEBUG/JBtlSppNative(122): NativeJBtlSpp_ReadNative:Exiting with 1
logcat はパート A のように見えるようで、conntion を確立できます。サーバーソケットが受け入れられずにブロックされた理由は、これらの操作が下位レベルの Android プラットフォームによって処理されるためだと思いました。しかし、 BluetoothSppService、JBtlSppNative、JBtlBmgなどのこのサービスを取得する方法や実行する方法がわかりません。
その経験はありますか?
はいの場合は、私と共有していただければ幸いです。または、以前の接続が存在する場合に、デバイスが別の接続を受け入れることができるようにする方法についてのヒントや提案を教えてください。