Skip to content

Commit 652678a

Browse files
zzyMatthew Xie
authored andcommitted
timeout support for accept() and added check for signal size
bug 7592240 Change-Id: Ide1868da669c190cdfed90f7af3f739ec9da690b
1 parent cdc6d5c commit 652678a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

core/java/android/bluetooth/BluetoothSocket.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ public void connect() throws IOException {
300300
if (mDevice == null) throw new IOException("Connect is called on null device");
301301

302302
try {
303-
// TODO(BT) derive flag from auth and encrypt
304303
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
305304
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
306305
if (bluetoothProxy == null) throw new IOException("Bluetooth is off");
@@ -349,7 +348,6 @@ public void connect() throws IOException {
349348
mUuid, mPort, getSecurityFlags());
350349
} catch (RemoteException e) {
351350
Log.e(TAG, Log.getStackTraceString(new Throwable()));
352-
// TODO(BT) right error code?
353351
return -1;
354352
}
355353

@@ -388,17 +386,20 @@ public void connect() throws IOException {
388386
/*package*/ BluetoothSocket accept(int timeout) throws IOException {
389387
BluetoothSocket acceptedSocket;
390388
if (mSocketState != SocketState.LISTENING) throw new IOException("bt socket is not in listen state");
391-
// TODO(BT) wait on an incoming connection
389+
if(timeout > 0) {
390+
Log.d(TAG, "accept() set timeout (ms):" + timeout);
391+
mSocket.setSoTimeout(timeout);
392+
}
392393
String RemoteAddr = waitSocketSignal(mSocketIS);
394+
if(timeout > 0)
395+
mSocket.setSoTimeout(0);
393396
synchronized(this)
394397
{
395398
if (mSocketState != SocketState.LISTENING)
396399
throw new IOException("bt socket is not in listen state");
397400
acceptedSocket = acceptSocket(RemoteAddr);
398401
//quick drop the reference of the file handle
399402
}
400-
// TODO(BT) rfcomm socket only supports one connection, return this?
401-
// return this;
402403
return acceptedSocket;
403404
}
404405

@@ -451,7 +452,6 @@ public void close() throws IOException {
451452
mPfd.detachFd();
452453
}
453454
}
454-
// TODO(BT) unbind proxy,
455455
}
456456

457457
/*package */ void removeChannel() {
@@ -471,6 +471,8 @@ private String waitSocketSignal(InputStream is) throws IOException {
471471
ByteBuffer bb = ByteBuffer.wrap(sig);
472472
bb.order(ByteOrder.nativeOrder());
473473
int size = bb.getShort();
474+
if(size != SOCK_SIGNAL_SIZE)
475+
throw new IOException("Connection failure, wrong signal size: " + size);
474476
byte [] addr = new byte[6];
475477
bb.get(addr);
476478
int channel = bb.getInt();
@@ -487,7 +489,7 @@ private int readAll(InputStream is, byte[] b) throws IOException {
487489
while(left > 0) {
488490
int ret = is.read(b, b.length - left, left);
489491
if(ret <= 0)
490-
throw new IOException("read failed, socket might closed, read ret: " + ret);
492+
throw new IOException("read failed, socket might closed or timeout, read ret: " + ret);
491493
left -= ret;
492494
if(left != 0)
493495
Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) +

0 commit comments

Comments
 (0)