最近遇到蓝牙接收文件的一个问题,先有一个手机给测试机发送文件,当快接收完的时候,第二个手机给测试机发送文件。
但是测试机没有收到第二个接收请求
抓取了这个过程中的log。
接受第一个文件:
07-04 15:24:34.528 6957 7052 D ObexServerSockets1: Accepted socket connection from: ServerSocket: Type: TYPE_L2CAP Channel: 4131
07-04 15:24:34.528 6957 7052 D ObexServerSockets1: onConnect() socket: android.bluetooth.BluetoothSocket@1307f14 mConAccepted = false
当第一个快接收完的时候,另一个手机给测试机发文件
07-04 15:24:49.468 6957 7052 D ObexServerSockets1: Accepted socket connection from: ServerSocket: Type: TYPE_L2CAP Channel: 4131
07-04 15:24:49.468 6957 7052 D ObexServerSockets1: onConnect() socket: android.bluetooth.BluetoothSocket@e557dd5 mConAccepted = true
07-04 15:24:49.468 6957 7052 I ObexServerSockets1: RemoteDevice is invalid - creating ObexRejectServer.
可以看到接第二个文件的时候,提示mConAccepted为true,说明当前已经存在一个链接。
结合源码中的解释,如下截图:
boolean isValid = ObexServerSockets.this.onConnect(device, connSocket);
if(isValid == false) {
/* Close connection if we already have a connection with another device
* by responding to the OBEX connect request.
*/
Log.i(TAG, "RemoteDevice is invalid - creating ObexRejectServer.");
BluetoothObexTransport obexTrans =
new BluetoothObexTransport(connSocket);
// Create and detach a selfdestructing ServerSession to respond to any
// incoming OBEX signals.
new ServerSession(obexTrans,
new ObexRejectServer(
ResponseCodes.OBEX_HTTP_UNAVAILABLE,
connSocket),
null);
如果已经有一个连接存在,那么就关闭将要建立的连接。
所以同时给测试机发送文件,在第一个没有接收完的情况下,第二个文件是不能被接受的,这个是蓝牙OBEX协议的规定。