FileDocCategorySizeDatePackage
BluetoothHeadset.javaAPI DocAndroid 1.5 API14532Wed May 06 22:41:54 BST 2009android.bluetooth

BluetoothHeadset

public class BluetoothHeadset extends Object
The Android Bluetooth API is not finalized, and *will* change. Use at your own risk. Public API for controlling the Bluetooth Headset Service. This includes both Bluetooth Headset and Handsfree (v1.5) profiles. The Headset service will attempt a handsfree connection first, and fall back to headset. BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC. Creating a BluetoothHeadset object will create a binding with the BluetoothHeadset service. Users of this object should call close() when they are finished with the BluetoothHeadset, so that this proxy object can unbind from the service. This BluetoothHeadset object is not immediately bound to the BluetoothHeadset service. Use the ServiceListener interface to obtain a notification when it is bound, this is especially important if you wish to immediately call methods on BluetootHeadset after construction. Android only supports one connected Bluetooth Headset at a time.
hide

Fields Summary
private static final String
TAG
private static final boolean
DBG
private IBluetoothHeadset
mService
private final android.content.Context
mContext
private final ServiceListener
mServiceListener
public static final int
STATE_ERROR
There was an error trying to obtain the state
public static final int
STATE_DISCONNECTED
No headset currently connected
public static final int
STATE_CONNECTING
Connection attempt in progress
public static final int
STATE_CONNECTED
A headset is currently connected
public static final int
AUDIO_STATE_DISCONNECTED
A SCO audio channel is not established
public static final int
AUDIO_STATE_CONNECTED
A SCO audio channel is established
public static final int
RESULT_FAILURE
public static final int
RESULT_SUCCESS
public static final int
RESULT_CANCELED
Connection canceled before completetion.
public static final int
PRIORITY_AUTO
Default priority for headsets that should be auto-connected
public static final int
PRIORITY_OFF
Default priority for headsets that should not be auto-connected
public static final boolean
DISABLE_BT_VOICE_DIALING
The voice dialer 'works' but the user experience is poor. The voice recognizer has trouble dealing with the 8kHz SCO signal, and it still requires visual confirmation. Disable for cupcake.
private android.content.ServiceConnection
mConnection
Constructors Summary
public BluetoothHeadset(android.content.Context context, ServiceListener l)
Create a BluetoothHeadset proxy object.


                         
       
                                               
          

                                                                   
          
    

              
         
        mContext = context;
        mServiceListener = l;
        if (!context.bindService(new Intent(IBluetoothHeadset.class.getName()), mConnection, 0)) {
            Log.e(TAG, "Could not bind to Bluetooth Headset Service");
        }
    
Methods Summary
public synchronized voidclose()
Close the connection to the backing service. Other public functions of BluetoothHeadset will return default error results once close() has been called. Multiple invocations of close() are ok.

        if (DBG) log("close()");
        if (mConnection != null) {
            mContext.unbindService(mConnection);
            mConnection = null;
        }
    
public booleanconnectHeadset(java.lang.String address)
Request to initiate a connection to a headset. This call does not block. Fails if a headset is already connecting or connected. Initiates auto-connection if address is null. Tries to connect to all devices with priority greater than PRIORITY_AUTO in descending order.

param
address The Bluetooth Address to connect to, or null to auto-connect to the last connected headset.
return
False if there was a problem initiating the connection procedure, and no further HEADSET_STATE_CHANGED intents will be expected.

        if (DBG) log("connectHeadset(" + address + ")");
        if (mService != null) {
            try {
                if (mService.connectHeadset(address)) {
                    return true;
                }
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;
    
public booleandisconnectHeadset()
Disconnects the current headset. Currently this call blocks, it may soon be made asynchornous. Returns false if this proxy object is not currently connected to the Headset service.

        if (DBG) log("disconnectHeadset()");
        if (mService != null) {
            try {
                mService.disconnectHeadset();
                return true;
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;
    
public static booleandoesClassMatch(int btClass)
Check class bits for possible HSP or HFP support. This is a simple heuristic that tries to guess if a device with the given class bits might support HSP or HFP. It is not accurate for all devices. It tries to err on the side of false positives.

return
True if this device might support HSP or HFP.

        // The render service class is required by the spec for HFP, so is a
        // pretty good signal
        if (BluetoothClass.Service.hasService(btClass, BluetoothClass.Service.RENDER)) {
            return true;
        }
        // Just in case they forgot the render service class
        switch (BluetoothClass.Device.getDevice(btClass)) {
        case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
        case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
        case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
            return true;
        default:
            return false;
        }
    
protected voidfinalize()

        try {
            close();
        } finally {
            super.finalize();
        }
    
public java.lang.StringgetHeadsetAddress()
Get the Bluetooth address of the current headset.

return
The Bluetooth address, or null if not in connected or connecting state, or if this proxy object is not connected to the Headset service.

        if (DBG) log("getHeadsetAddress()");
        if (mService != null) {
            try {
                return mService.getHeadsetAddress();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return null;
    
public intgetPriority(java.lang.String address)
Get priority of headset.

param
address Headset
return
non-negative priority, or negative error code on error.

        if (DBG) log("getPriority(" + address + ")");
        if (mService != null) {
            try {
                return mService.getPriority(address);
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return -1;
    
public intgetState()
Get the current state of the Bluetooth Headset service.

return
One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not connected to the Headset service.

        if (DBG) log("getState()");
        if (mService != null) {
            try {
                return mService.getState();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return BluetoothHeadset.STATE_ERROR;
    
public booleanisConnected(java.lang.String address)
Returns true if the specified headset is connected (does not include connecting). Returns false if not connected, or if this proxy object if not currently connected to the headset service.

        if (DBG) log("isConnected(" + address + ")");
        if (mService != null) {
            try {
                return mService.isConnected(address);
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;
    
private static voidlog(java.lang.String msg)


         
        Log.d(TAG, msg);
    
public booleansetPriority(java.lang.String address, int priority)
Set priority of headset. Priority is a non-negative integer. By default paired headsets will have a priority of PRIORITY_AUTO, and unpaired headset PRIORITY_NONE (0). Headsets with priority greater than zero will be auto-connected, and incoming connections will be accepted (if no other headset is connected). Auto-connection occurs at the following events: boot, incoming phone call, outgoing phone call. Headsets with priority equal to zero, or that are unpaired, are not auto-connected. Incoming connections are ignored regardless of priority if there is already a headset connected.

param
address Paired headset
param
priority Integer priority, for example PRIORITY_AUTO or PRIORITY_NONE
return
True if successful, false if there was some error.

        if (DBG) log("setPriority(" + address + ", " + priority + ")");
        if (mService != null) {
            try {
                return mService.setPriority(address, priority);
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;
    
public booleanstartVoiceRecognition()
Start BT Voice Recognition mode, and set up Bluetooth audio path. Returns false if there is no headset connected, or if the connected headset does not support voice recognition, or on error.

        if (DBG) log("startVoiceRecognition()");
        if (mService != null) {
            try {
                return mService.startVoiceRecognition();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;
    
public booleanstopVoiceRecognition()
Stop BT Voice Recognition mode, and shut down Bluetooth audio path. Returns false if there is no headset connected, or the connected headset is not in voice recognition mode, or on error.

        if (DBG) log("stopVoiceRecognition()");
        if (mService != null) {
            try {
                return mService.stopVoiceRecognition();
            } catch (RemoteException e) {Log.e(TAG, e.toString());}
        } else {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
        }
        return false;