FileDocCategorySizeDatePackage
HeadsetBase.javaAPI DocAndroid 1.5 API9002Wed May 06 22:41:54 BST 2009android.bluetooth

HeadsetBase

public class HeadsetBase extends Object
The Android Bluetooth API is not finalized, and *will* change. Use at your own risk. The base RFCOMM (service) connection for a headset or handsfree device. In the future this class will be removed.
hide

Fields Summary
private static final String
TAG
private static final boolean
DBG
public static final int
RFCOMM_DISCONNECTED
public static final int
DIRECTION_INCOMING
public static final int
DIRECTION_OUTGOING
private final BluetoothDevice
mBluetooth
private final String
mAddress
private final int
mRfcommChannel
private int
mNativeData
private Thread
mEventThread
private volatile boolean
mEventThreadInterrupted
private android.os.Handler
mEventThreadHandler
private int
mTimeoutRemainingMs
private final int
mDirection
private final long
mConnectTimestamp
protected AtParser
mAtParser
private android.os.PowerManager.WakeLock
mWakeLock
Constructors Summary
public HeadsetBase(android.os.PowerManager pm, BluetoothDevice bluetooth, String address, int rfcommChannel)

        mDirection = DIRECTION_OUTGOING;
        mConnectTimestamp = System.currentTimeMillis();
        mBluetooth = bluetooth;
        mAddress = address;
        mRfcommChannel = rfcommChannel;
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetBase");
        mWakeLock.setReferenceCounted(false);
        initializeAtParser();
        // Must be called after this.mAddress is set.
        initializeNativeDataNative(-1);
    
public HeadsetBase(android.os.PowerManager pm, BluetoothDevice bluetooth, String address, int socketFd, int rfcommChannel, android.os.Handler handler)

        mDirection = DIRECTION_INCOMING;
        mConnectTimestamp = System.currentTimeMillis();
        mBluetooth = bluetooth;
        mAddress = address;
        mRfcommChannel = rfcommChannel;
        mEventThreadHandler = handler;
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetBase");
        mWakeLock.setReferenceCounted(false);
        initializeAtParser();
        // Must be called after this.mAddress is set.
        initializeNativeDataNative(socketFd);
    
Methods Summary
private synchronized voidacquireWakeLock()

        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
        }
    
private static native voidclassInitNative()

private native voidcleanupNativeDataNative()

public booleanconnect(android.os.Handler handler)

        if (mEventThread == null) {
            if (!connectNative()) return false;
            mEventThreadHandler = handler;
        }
        return true;
    
public booleanconnectAsync()

        return connectAsyncNative();
    
private native booleanconnectAsyncNative()

private native booleanconnectNative()

public voiddisconnect()

        if (mEventThread != null) {
            stopEventThread();
        }
        disconnectNative();
    
private native voiddisconnectNative()

protected voidfinalize()

  // held while processing an AT command

        
     
        classInitNative();
    
        try {
            cleanupNativeDataNative();
            releaseWakeLock();
        } finally {
            super.finalize();
        }
    
public java.lang.StringgetAddress()

        return mAddress;
    
public AtParsergetAtParser()

        return mAtParser;
    
public longgetConnectTimestamp()

        return mConnectTimestamp;
    
public intgetDirection()

        return mDirection;
    
private native intgetLastReadStatusNative()

public java.lang.StringgetName()

        return mBluetooth.getRemoteName(mAddress);
    
public intgetRemainingAsyncConnectWaitingTimeMs()

        return mTimeoutRemainingMs;
    
protected voidhandleInput(java.lang.String input)

        acquireWakeLock();
        long timestamp;

        if (DBG) timestamp = System.currentTimeMillis();
        AtCommandResult result = mAtParser.process(input);
        if (DBG) Log.d(TAG, "Processing " + input + " took " +
                       (System.currentTimeMillis() - timestamp) + " ms");

        if (result.getResultCode() == AtCommandResult.ERROR) {
            Log.i(TAG, "Error pocessing <" + input + ">");
        }

        sendURC(result.toString());

        releaseWakeLock();
    
protected voidinitializeAtParser()
Register AT commands that are common to all Headset / Handsets. This function is called by the HeadsetBase constructor.

        mAtParser = new AtParser();
        //TODO(): Get rid of this as there are no parsers registered. But because of dependencies,
        //it needs to be done as part of refactoring HeadsetBase and BluetoothHandsfree
    
private native voidinitializeNativeDataNative(int socketFd)

public booleanisConnected()

        return mEventThread != null;
    
private voidlog(java.lang.String msg)

        Log.d(TAG, msg);
    
private native java.lang.StringreadNative(int timeout_ms)

private synchronized voidreleaseWakeLock()

        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }
    
public synchronized booleansendURC(java.lang.String urc)

        if (urc.length() > 0) {
            boolean ret = sendURCNative(urc);
            return ret;
        }
        return true;
    
private native booleansendURCNative(java.lang.String urc)

public voidstartEventThread()

        mEventThread =
            new Thread("HeadsetBase Event Thread") {
                public void run() {
                    int last_read_error;
                    while (!mEventThreadInterrupted) {
                        String input = readNative(500);
                        if (input != null) {
                            handleInput(input);
                        }
                        else {
                            last_read_error = getLastReadStatusNative();
                            if (last_read_error != 0) {
                                Log.i(TAG, "headset read error " + last_read_error);
                                if (mEventThreadHandler != null) {
                                    mEventThreadHandler.obtainMessage(RFCOMM_DISCONNECTED)
                                            .sendToTarget();
                                }
                                disconnectNative();
                                break;
                            }
                        }
                    }
                }
            };
        mEventThreadInterrupted = false;
        mEventThread.start();
    
private voidstopEventThread()

        mEventThreadInterrupted = true;
        mEventThread.interrupt();
        try {
            mEventThread.join();
        } catch (java.lang.InterruptedException e) {
            // FIXME: handle this,
        }
        mEventThread = null;
    
public intwaitForAsyncConnect(int timeout_ms, android.os.Handler handler)

        int res = waitForAsyncConnectNative(timeout_ms);
        if (res > 0) {
            mEventThreadHandler = handler;
        }
        return res;
    
private native intwaitForAsyncConnectNative(int timeout_ms)