|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package android.bluetooth; |
|
|
|
import android.os.ParcelUuid; |
|
import android.os.ParcelFileDescriptor; |
|
import android.os.RemoteException; |
|
import android.util.Log; |
|
|
|
import java.io.Closeable; |
|
import java.io.FileDescriptor; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.OutputStream; |
|
import java.util.Locale; |
|
import java.util.UUID; |
|
import android.net.LocalSocket; |
|
import java.nio.ByteOrder; |
|
import java.nio.ByteBuffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final class BluetoothSocket implements Closeable { |
|
private static final String TAG = "BluetoothSocket"; |
|
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
|
private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); |
|
|
|
|
|
public static final int MAX_RFCOMM_CHANNEL = 30; |
|
|
|
|
|
static final int TYPE_RFCOMM = 1; |
|
static final int TYPE_SCO = 2; |
|
static final int TYPE_L2CAP = 3; |
|
|
|
static final int EBADFD = 77; |
|
static final int EADDRINUSE = 98; |
|
|
|
static final int SEC_FLAG_ENCRYPT = 1; |
|
static final int SEC_FLAG_AUTH = 1 << 1; |
|
|
|
private final int mType; |
|
private BluetoothDevice mDevice; |
|
private String mAddress; |
|
private final boolean mAuth; |
|
private final boolean mEncrypt; |
|
private final BluetoothInputStream mInputStream; |
|
private final BluetoothOutputStream mOutputStream; |
|
private final ParcelUuid mUuid; |
|
private ParcelFileDescriptor mPfd; |
|
private LocalSocket mSocket; |
|
private InputStream mSocketIS; |
|
private OutputStream mSocketOS; |
|
private int mPort; |
|
private int mFd; |
|
private String mServiceName; |
|
private static int PROXY_CONNECTION_TIMEOUT = 5000; |
|
|
|
private static int SOCK_SIGNAL_SIZE = 16; |
|
|
|
private enum SocketState { |
|
INIT, |
|
CONNECTED, |
|
LISTENING, |
|
CLOSED, |
|
} |
|
|
|
|
|
private volatile SocketState mSocketState; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, |
|
BluetoothDevice device, int port, ParcelUuid uuid) throws IOException { |
|
if (type == BluetoothSocket.TYPE_RFCOMM && uuid == null && fd == -1) { |
|
if (port < 1 || port > MAX_RFCOMM_CHANNEL) { |
|
throw new IOException("Invalid RFCOMM channel: " + port); |
|
} |
|
} |
|
if(uuid != null) |
|
mUuid = uuid; |
|
else mUuid = new ParcelUuid(new UUID(0, 0)); |
|
mType = type; |
|
mAuth = auth; |
|
mEncrypt = encrypt; |
|
mDevice = device; |
|
mPort = port; |
|
mFd = fd; |
|
|
|
mSocketState = SocketState.INIT; |
|
|
|
if (device == null) { |
|
|
|
mAddress = BluetoothAdapter.getDefaultAdapter().getAddress(); |
|
} else { |
|
|
|
mAddress = device.getAddress(); |
|
} |
|
mInputStream = new BluetoothInputStream(this); |
|
mOutputStream = new BluetoothOutputStream(this); |
|
} |
|
private BluetoothSocket(BluetoothSocket s) { |
|
mUuid = s.mUuid; |
|
mType = s.mType; |
|
mAuth = s.mAuth; |
|
mEncrypt = s.mEncrypt; |
|
mPort = s.mPort; |
|
mInputStream = new BluetoothInputStream(this); |
|
mOutputStream = new BluetoothOutputStream(this); |
|
mServiceName = s.mServiceName; |
|
} |
|
private BluetoothSocket acceptSocket(String RemoteAddr) throws IOException { |
|
BluetoothSocket as = new BluetoothSocket(this); |
|
as.mSocketState = SocketState.CONNECTED; |
|
FileDescriptor[] fds = mSocket.getAncillaryFileDescriptors(); |
|
if (DBG) Log.d(TAG, "socket fd passed by stack fds: " + fds); |
|
if(fds == null || fds.length != 1) { |
|
Log.e(TAG, "socket fd passed from stack failed, fds: " + fds); |
|
as.close(); |
|
throw new IOException("bt socket acept failed"); |
|
} |
|
as.mSocket = new LocalSocket(fds[0]); |
|
|
|
|
|
|
|
as.mPfd = new ParcelFileDescriptor(fds[0]); |
|
try { |
|
as.mSocket.closeExternalFd(); |
|
} catch (IOException e) { |
|
Log.e(TAG, "closeExternalFd failed"); |
|
} |
|
as.mSocketIS = as.mSocket.getInputStream(); |
|
as.mSocketOS = as.mSocket.getOutputStream(); |
|
as.mAddress = RemoteAddr; |
|
as.mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(RemoteAddr); |
|
as.mPort = mPort; |
|
return as; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address, |
|
int port) throws IOException { |
|
this(type, fd, auth, encrypt, new BluetoothDevice(address), port, null); |
|
} |
|
|
|
|
|
@Override |
|
protected void finalize() throws Throwable { |
|
try { |
|
close(); |
|
} finally { |
|
super.finalize(); |
|
} |
|
} |
|
private int getSecurityFlags() { |
|
int flags = 0; |
|
if(mAuth) |
|
flags |= SEC_FLAG_AUTH; |
|
if(mEncrypt) |
|
flags |= SEC_FLAG_ENCRYPT; |
|
return flags; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
public BluetoothDevice getRemoteDevice() { |
|
return mDevice; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InputStream getInputStream() throws IOException { |
|
return mInputStream; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public OutputStream getOutputStream() throws IOException { |
|
return mOutputStream; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isConnected() { |
|
return mSocketState == SocketState.CONNECTED; |
|
} |
|
|
|
void setServiceName(String name) { |
|
mServiceName = name; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void connect() throws IOException { |
|
if (mDevice == null) throw new IOException("Connect is called on null device"); |
|
|
|
try { |
|
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
|
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
|
if (bluetoothProxy == null) throw new IOException("Bluetooth is off"); |
|
mPfd = bluetoothProxy.connectSocket(mDevice, mType, |
|
mUuid, mPort, getSecurityFlags()); |
|
synchronized(this) |
|
{ |
|
if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd); |
|
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
|
if (mPfd == null) throw new IOException("bt socket connect failed"); |
|
FileDescriptor fd = mPfd.getFileDescriptor(); |
|
mSocket = new LocalSocket(fd); |
|
mSocketIS = mSocket.getInputStream(); |
|
mSocketOS = mSocket.getOutputStream(); |
|
} |
|
int channel = readInt(mSocketIS); |
|
if (channel <= 0) |
|
throw new IOException("bt socket connect failed"); |
|
mPort = channel; |
|
waitSocketSignal(mSocketIS); |
|
synchronized(this) |
|
{ |
|
if (mSocketState == SocketState.CLOSED) |
|
throw new IOException("bt socket closed"); |
|
mSocketState = SocketState.CONNECTED; |
|
} |
|
} catch (RemoteException e) { |
|
Log.e(TAG, Log.getStackTraceString(new Throwable())); |
|
throw new IOException("unable to send RPC: " + e.getMessage()); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
int bindListen() { |
|
int ret; |
|
if (mSocketState == SocketState.CLOSED) return EBADFD; |
|
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
|
if (bluetoothProxy == null) { |
|
Log.e(TAG, "bindListen fail, reason: bluetooth is off"); |
|
return -1; |
|
} |
|
try { |
|
mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, |
|
mUuid, mPort, getSecurityFlags()); |
|
} catch (RemoteException e) { |
|
Log.e(TAG, Log.getStackTraceString(new Throwable())); |
|
return -1; |
|
} |
|
|
|
|
|
try { |
|
synchronized(this) { |
|
if (DBG) Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + |
|
mPfd); |
|
if(mSocketState != SocketState.INIT) return EBADFD; |
|
if(mPfd == null) return -1; |
|
FileDescriptor fd = mPfd.getFileDescriptor(); |
|
if (DBG) Log.d(TAG, "bindListen(), new LocalSocket "); |
|
mSocket = new LocalSocket(fd); |
|
if (DBG) Log.d(TAG, "bindListen(), new LocalSocket.getInputStream() "); |
|
mSocketIS = mSocket.getInputStream(); |
|
mSocketOS = mSocket.getOutputStream(); |
|
} |
|
if (DBG) Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS); |
|
int channel = readInt(mSocketIS); |
|
synchronized(this) { |
|
if(mSocketState == SocketState.INIT) |
|
mSocketState = SocketState.LISTENING; |
|
} |
|
if (DBG) Log.d(TAG, "channel: " + channel); |
|
if (mPort == -1) { |
|
mPort = channel; |
|
} |
|
ret = 0; |
|
} catch (IOException e) { |
|
if (mPfd != null) { |
|
try { |
|
mPfd.close(); |
|
} catch (IOException e1) { |
|
Log.e(TAG, "bindListen, close mPfd: " + e1); |
|
} |
|
mPfd = null; |
|
} |
|
Log.e(TAG, "bindListen, fail to get port number, exception: " + e); |
|
return -1; |
|
} |
|
return ret; |
|
} |
|
|
|
BluetoothSocket accept(int timeout) throws IOException { |
|
BluetoothSocket acceptedSocket; |
|
if (mSocketState != SocketState.LISTENING) throw new IOException("bt socket is not in listen state"); |
|
if(timeout > 0) { |
|
Log.d(TAG, "accept() set timeout (ms):" + timeout); |
|
mSocket.setSoTimeout(timeout); |
|
} |
|
String RemoteAddr = waitSocketSignal(mSocketIS); |
|
if(timeout > 0) |
|
mSocket.setSoTimeout(0); |
|
synchronized(this) |
|
{ |
|
if (mSocketState != SocketState.LISTENING) |
|
throw new IOException("bt socket is not in listen state"); |
|
acceptedSocket = acceptSocket(RemoteAddr); |
|
|
|
} |
|
return acceptedSocket; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int setSocketOpt(int optionName, byte [] optionVal, int optionLen) throws IOException { |
|
int ret = 0; |
|
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
|
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
|
if (bluetoothProxy == null) { |
|
Log.e(TAG, "setSocketOpt fail, reason: bluetooth is off"); |
|
return -1; |
|
} |
|
try { |
|
if(VDBG) Log.d(TAG, "setSocketOpt(), mType: " + mType + " mPort: " + mPort); |
|
ret = bluetoothProxy.setSocketOpt(mType, mPort, optionName, optionVal, optionLen); |
|
} catch (RemoteException e) { |
|
Log.e(TAG, Log.getStackTraceString(new Throwable())); |
|
return -1; |
|
} |
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getSocketOpt(int optionName, byte [] optionVal) throws IOException { |
|
int ret = 0; |
|
if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
|
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
|
if (bluetoothProxy == null) { |
|
Log.e(TAG, "getSocketOpt fail, reason: bluetooth is off"); |
|
return -1; |
|
} |
|
try { |
|
if(VDBG) Log.d(TAG, "getSocketOpt(), mType: " + mType + " mPort: " + mPort); |
|
ret = bluetoothProxy.getSocketOpt(mType, mPort, optionName, optionVal); |
|
} catch (RemoteException e) { |
|
Log.e(TAG, Log.getStackTraceString(new Throwable())); |
|
return -1; |
|
} |
|
return ret; |
|
} |
|
|
|
int available() throws IOException { |
|
if (VDBG) Log.d(TAG, "available: " + mSocketIS); |
|
return mSocketIS.available(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void flush() throws IOException { |
|
if (mSocketOS == null) throw new IOException("flush is called on null OutputStream"); |
|
if (VDBG) Log.d(TAG, "flush: " + mSocketOS); |
|
mSocketOS.flush(); |
|
} |
|
|
|
int read(byte[] b, int offset, int length) throws IOException { |
|
if (mSocketIS == null) throw new IOException("read is called on null InputStream"); |
|
if (VDBG) Log.d(TAG, "read in: " + mSocketIS + " len: " + length); |
|
int ret = mSocketIS.read(b, offset, length); |
|
if(ret < 0) |
|
throw new IOException("bt socket closed, read return: " + ret); |
|
if (VDBG) Log.d(TAG, "read out: " + mSocketIS + " ret: " + ret); |
|
return ret; |
|
} |
|
|
|
int write(byte[] b, int offset, int length) throws IOException { |
|
if (mSocketOS == null) throw new IOException("write is called on null OutputStream"); |
|
if (VDBG) Log.d(TAG, "write: " + mSocketOS + " length: " + length); |
|
mSocketOS.write(b, offset, length); |
|
|
|
if (VDBG) Log.d(TAG, "write out: " + mSocketOS + " length: " + length); |
|
return length; |
|
} |
|
|
|
@Override |
|
public void close() throws IOException { |
|
if (DBG) Log.d(TAG, "close() in, this: " + this + ", channel: " + mPort + ", state: " + mSocketState); |
|
if(mSocketState == SocketState.CLOSED) |
|
return; |
|
else |
|
{ |
|
synchronized(this) |
|
{ |
|
if(mSocketState == SocketState.CLOSED) |
|
return; |
|
mSocketState = SocketState.CLOSED; |
|
if (DBG) Log.d(TAG, "close() this: " + this + ", channel: " + mPort + ", mSocketIS: " + mSocketIS + |
|
", mSocketOS: " + mSocketOS + "mSocket: " + mSocket); |
|
if(mSocket != null) { |
|
if (DBG) Log.d(TAG, "Closing mSocket: " + mSocket); |
|
mSocket.shutdownInput(); |
|
mSocket.shutdownOutput(); |
|
mSocket.close(); |
|
mSocket = null; |
|
} |
|
if (mPfd != null) { |
|
mPfd.close(); |
|
mPfd = null; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void removeChannel() { |
|
} |
|
|
|
int getPort() { |
|
return mPort; |
|
} |
|
private String convertAddr(final byte[] addr) { |
|
return String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", |
|
addr[0] , addr[1], addr[2], addr[3] , addr[4], addr[5]); |
|
} |
|
private String waitSocketSignal(InputStream is) throws IOException { |
|
byte [] sig = new byte[SOCK_SIGNAL_SIZE]; |
|
int ret = readAll(is, sig); |
|
if (VDBG) Log.d(TAG, "waitSocketSignal read 16 bytes signal ret: " + ret); |
|
ByteBuffer bb = ByteBuffer.wrap(sig); |
|
bb.order(ByteOrder.nativeOrder()); |
|
int size = bb.getShort(); |
|
if(size != SOCK_SIGNAL_SIZE) |
|
throw new IOException("Connection failure, wrong signal size: " + size); |
|
byte [] addr = new byte[6]; |
|
bb.get(addr); |
|
int channel = bb.getInt(); |
|
int status = bb.getInt(); |
|
String RemoteAddr = convertAddr(addr); |
|
if (VDBG) Log.d(TAG, "waitSocketSignal: sig size: " + size + ", remote addr: " |
|
+ RemoteAddr + ", channel: " + channel + ", status: " + status); |
|
if(status != 0) |
|
throw new IOException("Connection failure, status: " + status); |
|
return RemoteAddr; |
|
} |
|
private int readAll(InputStream is, byte[] b) throws IOException { |
|
int left = b.length; |
|
while(left > 0) { |
|
int ret = is.read(b, b.length - left, left); |
|
if(ret <= 0) |
|
throw new IOException("read failed, socket might closed or timeout, read ret: " + ret); |
|
left -= ret; |
|
if(left != 0) |
|
Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) + |
|
", expect size: " + b.length); |
|
} |
|
return b.length; |
|
} |
|
|
|
private int readInt(InputStream is) throws IOException { |
|
byte[] ibytes = new byte[4]; |
|
int ret = readAll(is, ibytes); |
|
if (VDBG) Log.d(TAG, "inputStream.read ret: " + ret); |
|
ByteBuffer bb = ByteBuffer.wrap(ibytes); |
|
bb.order(ByteOrder.nativeOrder()); |
|
return bb.getInt(); |
|
} |
|
} |
|
|