FileDocCategorySizeDatePackage
ScoSocket.javaAPI DocAndroid 1.5 API6663Wed May 06 22:41:54 BST 2009android.bluetooth

ScoSocket

public class ScoSocket extends Object
The Android Bluetooth API is not finalized, and *will* change. Use at your own risk. Simple SCO Socket. Currently in Android, there is no support for sending data over a SCO socket - this is managed by the hardware link to the Bluetooth Chip. This class is instead intended for management of the SCO socket lifetime, and is tailored for use with the headset / handsfree profiles.
hide

Fields Summary
private static final String
TAG
private static final boolean
DBG
private static final boolean
VDBG
public static final int
STATE_READY
public static final int
STATE_ACCEPT
public static final int
STATE_CONNECTING
public static final int
STATE_CONNECTED
public static final int
STATE_CLOSED
private int
mState
private int
mNativeData
private android.os.Handler
mHandler
private int
mAcceptedCode
private int
mConnectedCode
private int
mClosedCode
private android.os.PowerManager.WakeLock
mWakeLock
Constructors Summary
public ScoSocket(android.os.PowerManager pm, android.os.Handler handler, int acceptedCode, int connectedCode, int closedCode)

  // held while in STATE_CONNECTING

     
        classInitNative();
    
        initNative();
        mState = STATE_READY;
        mHandler = handler;
        mAcceptedCode = acceptedCode;
        mConnectedCode = connectedCode;
        mClosedCode = closedCode;
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ScoSocket");
        mWakeLock.setReferenceCounted(false);
        if (VDBG) log(this + " SCO OBJECT CTOR");
    
Methods Summary
public synchronized booleanaccept()
Accept incoming SCO connections. Does not block.

        if (VDBG) log("accept() " + this);
        if (mState != STATE_READY) {
            if (DBG) log("Bad state");
            return false;
        }
        if (acceptNative()) {
            mState = STATE_ACCEPT;
            return true;
        } else {
            mState = STATE_CLOSED;
            return false;
        }
    
private native booleanacceptNative()

private voidacquireWakeLock()

        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
            if (VDBG) log("mWakeLock.acquire() " + this);
        }
    
private static native voidclassInitNative()

public synchronized voidclose()

        if (DBG) log(this + " SCO OBJECT close() mState = " + mState);
        acquireWakeLock();
        mState = STATE_CLOSED;
        closeNative();
        releaseWakeLock();
    
private native voidcloseNative()

public synchronized booleanconnect(java.lang.String address)
Connect this SCO socket to the given BT address. Does not block.

        if (VDBG) log("connect() " + this);
        if (mState != STATE_READY) {
            if (DBG) log("connect(): Bad state");
            return false;
        }
        acquireWakeLock();
        if (connectNative(address)) {
            mState = STATE_CONNECTING;
            return true;
        } else {
            mState = STATE_CLOSED;
            releaseWakeLockNow();
            return false;
        }
    
private native booleanconnectNative(java.lang.String address)

private native voiddestroyNative()

protected voidfinalize()

        try {
            if (VDBG) log(this + " SCO OBJECT DTOR");
            destroyNative();
            releaseWakeLockNow();
        } finally {
            super.finalize();
        }
    
public synchronized intgetState()

        return mState;
    
private native voidinitNative()

private voidlog(java.lang.String msg)

        Log.d(TAG, msg);
    
private synchronized voidonAccepted(int result)

        if (VDBG) log("onAccepted() " + this);
        if (mState != STATE_ACCEPT) {
            if (DBG) log("Strange state " + this);
            return;
        }
        if (result >= 0) {
            mState = STATE_CONNECTED;
        } else {
            mState = STATE_CLOSED;
        }
        mHandler.obtainMessage(mAcceptedCode, mState, -1, this).sendToTarget();
    
private synchronized voidonClosed()

        if (DBG) log("onClosed() " + this);
        if (mState != STATE_CLOSED) {
            mState = STATE_CLOSED;
            mHandler.obtainMessage(mClosedCode, mState, -1, this).sendToTarget();
            releaseWakeLock();
        }
    
private synchronized voidonConnected(int result)

        if (VDBG) log(this + " onConnected() mState = " + mState + " " + this);
        if (mState != STATE_CONNECTING) {
            if (DBG) log("Strange state, closing " + mState + " " + this);
            return;
        }
        if (result >= 0) {
            mState = STATE_CONNECTED;
        } else {
            mState = STATE_CLOSED;
        }
        mHandler.obtainMessage(mConnectedCode, mState, -1, this).sendToTarget();
        releaseWakeLockNow();
    
private voidreleaseWakeLock()

        if (mWakeLock.isHeld()) {
            // Keep apps processor awake for a further 2 seconds.
            // This is a hack to resolve issue http://b/1616263 - in which
            // we are left in a 80 mA power state when remotely terminating a
            // call while connected to BT headset "HTC BH S100 " with A2DP and
            // HFP profiles.
            if (VDBG) log("mWakeLock.release() in 2 sec" + this);
            mWakeLock.acquire(2000);
        }
    
private voidreleaseWakeLockNow()

        if (mWakeLock.isHeld()) {
            if (VDBG) log("mWakeLock.release() now" + this);
            mWakeLock.release();
        }