FileDocCategorySizeDatePackage
LocalBluetoothManager.javaAPI DocAndroid 1.5 API8929Wed May 06 22:42:48 BST 2009com.android.settings.bluetooth

LocalBluetoothManager

public class LocalBluetoothManager extends Object
LocalBluetoothManager provides a simplified interface on top of a subset of the Bluetooth API.

Fields Summary
private static final String
TAG
static final boolean
V
private static final String
SHARED_PREFERENCES_NAME
private static LocalBluetoothManager
INSTANCE
private static Object
INSTANCE_LOCK
Used when obtaining a reference to the singleton instance.
private boolean
mInitialized
private android.content.Context
mContext
private android.app.Activity
mForegroundActivity
If a BT-related activity is in the foreground, this will be it.
private android.app.AlertDialog
mErrorDialog
private android.bluetooth.BluetoothDevice
mManager
private LocalBluetoothDeviceManager
mLocalDeviceManager
private BluetoothEventRedirector
mEventRedirector
private android.bluetooth.BluetoothA2dp
mBluetoothA2dp
private int
mState
private List
mCallbacks
private static final int
SCAN_EXPIRATION_MS
private long
mLastScan
Constructors Summary
Methods Summary
private voiddispatchScanningStateChanged(boolean started)

        synchronized (mCallbacks) {
            for (Callback callback : mCallbacks) {
                callback.onScanningStateChanged(started);
            }
        }
    
public android.bluetooth.BluetoothDevicegetBluetoothManager()

        return mManager;
    
public intgetBluetoothState()

        
        if (mState == BluetoothError.ERROR) {
            syncBluetoothState();
        }
            
        return mState;
    
java.util.ListgetCallbacks()

        return mCallbacks;
    
public android.content.ContextgetContext()

        return mContext;
    
public android.app.ActivitygetForegroundActivity()

        return mForegroundActivity;
    
public static com.android.settings.bluetooth.LocalBluetoothManagergetInstance(android.content.Context context)

    
         
        synchronized (INSTANCE_LOCK) {
            if (INSTANCE == null) {
                INSTANCE = new LocalBluetoothManager();
            }
            
            if (!INSTANCE.init(context)) {
                return null;
            }
            
            return INSTANCE;
        }
    
public LocalBluetoothDeviceManagergetLocalDeviceManager()

        return mLocalDeviceManager;
    
public android.content.SharedPreferencesgetSharedPreferences()

        return mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    
private booleaninit(android.content.Context context)

        if (mInitialized) return true;
        mInitialized = true;
        
        // This will be around as long as this process is
        mContext = context.getApplicationContext();
        
        mManager = (BluetoothDevice) context.getSystemService(Context.BLUETOOTH_SERVICE);
        if (mManager == null) {
            return false;
        }
        
        mLocalDeviceManager = new LocalBluetoothDeviceManager(this);

        mEventRedirector = new BluetoothEventRedirector(this);
        mEventRedirector.start();

        mBluetoothA2dp = new BluetoothA2dp(context);

        return true;
    
voidonScanningStateChanged(boolean started)

param
started True if scanning started, false if scanning finished.

        // TODO: have it be a callback (once we switch bluetooth state changed to callback)
        mLocalDeviceManager.onScanningStateChanged(started);
        dispatchScanningStateChanged(started);
    
public voidregisterCallback(com.android.settings.bluetooth.LocalBluetoothManager$Callback callback)

        synchronized (mCallbacks) {
            mCallbacks.add(callback);
        }
    
public voidsetBluetoothEnabled(boolean enabled)

        boolean wasSetStateSuccessful = enabled
                ? mManager.enable()
                : mManager.disable();
                
        if (wasSetStateSuccessful) {
            setBluetoothStateInt(enabled
                ? BluetoothDevice.BLUETOOTH_STATE_TURNING_ON
                : BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF);
        } else {
            if (V) {
                Log.v(TAG,
                        "setBluetoothEnabled call, manager didn't return success for enabled: "
                                + enabled);
            }
            
            syncBluetoothState();
        }
    
voidsetBluetoothStateInt(int state)

        mState = state;
        if (state == BluetoothDevice.BLUETOOTH_STATE_ON ||
            state == BluetoothDevice.BLUETOOTH_STATE_OFF) {
            mLocalDeviceManager.onBluetoothStateChanged(state == BluetoothDevice.BLUETOOTH_STATE_ON);
        }
    
public voidsetForegroundActivity(android.app.Activity activity)

        if (mErrorDialog != null) {
            mErrorDialog.dismiss();
            mErrorDialog = null;
        }
        mForegroundActivity = activity;
    
public voidshowError(java.lang.String address, int titleResId, int messageResId)

        LocalBluetoothDevice device = mLocalDeviceManager.findDevice(address);
        if (device == null) return;

        String name = device.getName();
        String message = mContext.getString(messageResId, name);

        if (mForegroundActivity != null) {
            // Need an activity context to show a dialog
            mErrorDialog = new AlertDialog.Builder(mForegroundActivity)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(titleResId)
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, null)
                .show();
        } else {
            // Fallback on a toast 
            Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        }
    
public voidstartScanning(boolean force)

        if (mManager.isDiscovering()) {
            /*
             * Already discovering, but give the callback that information.
             * Note: we only call the callbacks, not the same path as if the
             * scanning state had really changed (in that case the device
             * manager would clear its list of unpaired scanned devices).
             */ 
            dispatchScanningStateChanged(true);
        } else {
            if (!force) {
                // Don't scan more than frequently than SCAN_EXPIRATION_MS,
                // unless forced
                if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) {
                    return;
                }

                // If we are playing music, don't scan unless forced.
                List<String> sinks = mBluetoothA2dp.listConnectedSinks();
                if (sinks != null) {
                    for (String address : sinks) {
                        if (mBluetoothA2dp.getSinkState(address) == BluetoothA2dp.STATE_PLAYING) {
                            return;
                        }
                    }
                }
            }
            
            if (mManager.startDiscovery(true)) {
                mLastScan = System.currentTimeMillis();
            }
        }
    
private voidsyncBluetoothState()

        setBluetoothStateInt(mManager.isEnabled()
                ? BluetoothDevice.BLUETOOTH_STATE_ON
                : BluetoothDevice.BLUETOOTH_STATE_OFF);
    
public voidunregisterCallback(com.android.settings.bluetooth.LocalBluetoothManager$Callback callback)

        synchronized (mCallbacks) {
            mCallbacks.remove(callback);
        }