Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 646590ef authored by Matthew Xie's avatar Matthew Xie Committed by Android (Google) Code Review
Browse files

Merge "Add public api BluetoothSocket.isConnected."

parents bade424f ceb6c9f5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -4130,6 +4130,7 @@ package android.bluetooth {
    method public java.io.InputStream getInputStream() throws java.io.IOException;
    method public java.io.OutputStream getOutputStream() throws java.io.IOException;
    method public android.bluetooth.BluetoothDevice getRemoteDevice();
    method public boolean isConnected();
  }
}
+32 −12
Original line number Diff line number Diff line
@@ -94,10 +94,16 @@ public final class BluetoothSocket implements Closeable {

    private int mPort;  /* RFCOMM channel or L2CAP psm */

    private enum SocketState {
        INIT,
        CONNECTED,
        CLOSED
    }

    /** prevents all native calls after destroyNative() */
    private boolean mClosed;
    private SocketState mSocketState;

    /** protects mClosed */
    /** protects mSocketState */
    private final ReentrantReadWriteLock mLock;

    /** used by native code only */
@@ -145,7 +151,7 @@ public final class BluetoothSocket implements Closeable {
        }
        mInputStream = new BluetoothInputStream(this);
        mOutputStream = new BluetoothOutputStream(this);
        mClosed = false;
        mSocketState = SocketState.INIT;
        mLock = new ReentrantReadWriteLock();
    }

@@ -195,13 +201,14 @@ public final class BluetoothSocket implements Closeable {
    public void connect() throws IOException {
        mLock.readLock().lock();
        try {
            if (mClosed) throw new IOException("socket closed");
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");

            if (mSdp != null) {
                mPort = mSdp.doSdp();  // blocks
            }

            connectNative();  // blocks
            mSocketState = SocketState.CONNECTED;
        } finally {
            mLock.readLock().unlock();
        }
@@ -216,7 +223,7 @@ public final class BluetoothSocket implements Closeable {
        // abort blocking operations on the socket
        mLock.readLock().lock();
        try {
            if (mClosed) return;
            if (mSocketState == SocketState.CLOSED) return;
            if (mSdp != null) {
                mSdp.cancel();
            }
@@ -229,7 +236,7 @@ public final class BluetoothSocket implements Closeable {
        // abortNative(), so this lock should immediately acquire
        mLock.writeLock().lock();
        try {
            mClosed = true;
            mSocketState = SocketState.CLOSED;
            destroyNative();
        } finally {
            mLock.writeLock().unlock();
@@ -266,6 +273,16 @@ public final class BluetoothSocket implements Closeable {
        return mOutputStream;
    }

    /**
     * Get the connection status of this socket, ie, whether there is an active connection with
     * remote device.
     * @return true if connected
     *         false if not connected
     */
    public boolean isConnected() {
        return (mSocketState == SocketState.CONNECTED);
    }

    /**
     * Currently returns unix errno instead of throwing IOException,
     * so that BluetoothAdapter can check the error code for EADDRINUSE
@@ -273,7 +290,7 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ int bindListen() {
        mLock.readLock().lock();
        try {
            if (mClosed) return EBADFD;
            if (mSocketState == SocketState.CLOSED) return EBADFD;
            return bindListenNative();
        } finally {
            mLock.readLock().unlock();
@@ -283,8 +300,11 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ BluetoothSocket accept(int timeout) throws IOException {
        mLock.readLock().lock();
        try {
            if (mClosed) throw new IOException("socket closed");
            return acceptNative(timeout);
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");

            BluetoothSocket acceptedSocket = acceptNative(timeout);
            mSocketState = SocketState.CONNECTED;
            return acceptedSocket;
        } finally {
            mLock.readLock().unlock();
        }
@@ -293,7 +313,7 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ int available() throws IOException {
        mLock.readLock().lock();
        try {
            if (mClosed) throw new IOException("socket closed");
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
            return availableNative();
        } finally {
            mLock.readLock().unlock();
@@ -303,7 +323,7 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ int read(byte[] b, int offset, int length) throws IOException {
        mLock.readLock().lock();
        try {
            if (mClosed) throw new IOException("socket closed");
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
            return readNative(b, offset, length);
        } finally {
            mLock.readLock().unlock();
@@ -313,7 +333,7 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ int write(byte[] b, int offset, int length) throws IOException {
        mLock.readLock().lock();
        try {
            if (mClosed) throw new IOException("socket closed");
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
            return writeNative(b, offset, length);
        } finally {
            mLock.readLock().unlock();