FileDocCategorySizeDatePackage
BluetoothAudioGateway.javaAPI DocAndroid 1.5 API7876Wed May 06 22:41:54 BST 2009android.bluetooth

BluetoothAudioGateway

public class BluetoothAudioGateway extends Object
Listen's for incoming RFCOMM connection for the headset / handsfree service. This class is planned for deletion, in favor of a generic Rfcomm class.
hide

Fields Summary
private static final String
TAG
private static final boolean
DBG
private int
mNativeData
private BluetoothDevice
mBluetooth
private int
mHandsfreeAgRfcommChannel
private int
mHeadsetAgRfcommChannel
private String
mConnectingHeadsetAddress
private int
mConnectingHeadsetRfcommChannel
private int
mConnectingHeadsetSocketFd
private String
mConnectingHandsfreeAddress
private int
mConnectingHandsfreeRfcommChannel
private int
mConnectingHandsfreeSocketFd
private int
mTimeoutRemainingMs
public static final int
DEFAULT_HF_AG_CHANNEL
public static final int
DEFAULT_HS_AG_CHANNEL
private Thread
mConnectThead
private volatile boolean
mInterrupted
private static final int
SELECT_WAIT_TIMEOUT
private android.os.Handler
mCallback
public static final int
MSG_INCOMING_HEADSET_CONNECTION
public static final int
MSG_INCOMING_HANDSFREE_CONNECTION
Constructors Summary
public BluetoothAudioGateway(BluetoothDevice bluetooth)


       
        this(bluetooth, DEFAULT_HF_AG_CHANNEL, DEFAULT_HS_AG_CHANNEL);
    
public BluetoothAudioGateway(BluetoothDevice bluetooth, int handsfreeAgRfcommChannel, int headsetAgRfcommChannel)

        mBluetooth = bluetooth;
        mHandsfreeAgRfcommChannel = handsfreeAgRfcommChannel;
        mHeadsetAgRfcommChannel = headsetAgRfcommChannel;
        initializeNativeDataNative();
    
Methods Summary
private static native voidclassInitNative()

private native voidcleanupNativeDataNative()

protected voidfinalize()

        try {
            cleanupNativeDataNative();
        } finally {
            super.finalize();
        }
    
private native voidinitializeNativeDataNative()

private static voidlog(java.lang.String msg)

        Log.d(TAG, msg);
    
private native booleansetUpListeningSocketsNative()

public synchronized booleanstart(android.os.Handler callback)


         

        if (mConnectThead == null) {
            mCallback = callback;
            mConnectThead = new Thread(TAG) {
                    public void run() {
                        if (DBG) log("Connect Thread starting");
                        while (!mInterrupted) {
                            //Log.i(TAG, "waiting for connect");
                            mConnectingHeadsetRfcommChannel = -1;
                            mConnectingHandsfreeRfcommChannel = -1;
                            if (waitForHandsfreeConnectNative(SELECT_WAIT_TIMEOUT) == false) {
                                if (mTimeoutRemainingMs > 0) {
                                    try {
                                        Log.i(TAG, "select thread timed out, but " + 
                                              mTimeoutRemainingMs + "ms of waiting remain.");
                                        Thread.sleep(mTimeoutRemainingMs);
                                    } catch (InterruptedException e) {
                                        Log.i(TAG, "select thread was interrupted (2), exiting");
                                        mInterrupted = true;
                                    }
                                }
                            }
                            else {
                                Log.i(TAG, "connect notification!");
                                /* A device connected (most likely just one, but 
                                   it is possible for two separate devices, one 
                                   a headset and one a handsfree, to connect
                                   simultaneously. 
                                */
                                if (mConnectingHeadsetRfcommChannel >= 0) {
                                    Log.i(TAG, "Incoming connection from headset " + 
                                          mConnectingHeadsetAddress + " on channel " + 
                                          mConnectingHeadsetRfcommChannel);
                                    Message msg = Message.obtain(mCallback);
                                    msg.what = MSG_INCOMING_HEADSET_CONNECTION;
                                    msg.obj = 
                                        new IncomingConnectionInfo(
                                            mBluetooth, 
                                            mConnectingHeadsetAddress,
                                            mConnectingHeadsetSocketFd,
                                            mConnectingHeadsetRfcommChannel);
                                    msg.sendToTarget();
                                }
                                if (mConnectingHandsfreeRfcommChannel >= 0) {
                                    Log.i(TAG, "Incoming connection from handsfree " + 
                                          mConnectingHandsfreeAddress + " on channel " + 
                                          mConnectingHandsfreeRfcommChannel);
                                    Message msg = Message.obtain();
                                    msg.setTarget(mCallback);
                                    msg.what = MSG_INCOMING_HANDSFREE_CONNECTION;
                                    msg.obj = 
                                        new IncomingConnectionInfo(
                                            mBluetooth,
                                            mConnectingHandsfreeAddress,
                                            mConnectingHandsfreeSocketFd,
                                            mConnectingHandsfreeRfcommChannel);
                                    msg.sendToTarget();
                                }
                            }
                        }
                        if (DBG) log("Connect Thread finished");
                    }
                };

            if (setUpListeningSocketsNative() == false) {
                Log.e(TAG, "Could not set up listening socket, exiting");
                return false;
            }

            mInterrupted = false;
            mConnectThead.start();
        }

        return true;
    
public synchronized voidstop()

        if (mConnectThead != null) {
            if (DBG) log("stopping Connect Thread");
            mInterrupted = true;
            try {
                mConnectThead.interrupt();
                if (DBG) log("waiting for thread to terminate");
                mConnectThead.join();
                mConnectThead = null;
                mCallback = null;
                tearDownListeningSocketsNative();
            } catch (InterruptedException e) {
                Log.w(TAG, "Interrupted waiting for Connect Thread to join");
            }
        }
    
private native voidtearDownListeningSocketsNative()

private native booleanwaitForHandsfreeConnectNative(int timeoutMs)