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

LocalBluetoothDeviceManager

public class LocalBluetoothDeviceManager extends Object
LocalBluetoothDeviceManager manages the set of remote Bluetooth devices.

Fields Summary
private static final String
TAG
final LocalBluetoothManager
mLocalManager
final List
mCallbacks
final List
mDevices
Constructors Summary
public LocalBluetoothDeviceManager(LocalBluetoothManager localManager)


       
        mLocalManager = localManager;
        mCallbacks = localManager.getCallbacks();
        readPairedDevices();
    
Methods Summary
private voidcheckForDeviceRemoval(LocalBluetoothDevice device)

        if (device.getBondState() == BluetoothDevice.BOND_NOT_BONDED &&
                !device.isVisible()) {
            // If device isn't paired, remove it altogether
            mDevices.remove(device);
            dispatchDeviceDeleted(device);
        }            
    
private voiddispatchDeviceAdded(LocalBluetoothDevice device)

        synchronized (mCallbacks) {
            for (Callback callback : mCallbacks) {
                callback.onDeviceAdded(device);
            }
        }
        
        // TODO: divider between prev paired/connected and scanned
    
private voiddispatchDeviceDeleted(LocalBluetoothDevice device)

        synchronized (mCallbacks) {
            for (Callback callback : mCallbacks) {
                callback.onDeviceDeleted(device);
            }
        }
    
public synchronized LocalBluetoothDevicefindDevice(java.lang.String address)

        
        for (int i = mDevices.size() - 1; i >= 0; i--) {
            LocalBluetoothDevice device = mDevices.get(i);
            
            if (device.getAddress().equals(address)) {
                return device;
            }
        }
        
        return null;
    
public synchronized java.util.ListgetDevicesCopy()

        return new ArrayList<LocalBluetoothDevice>(mDevices);
    
public java.lang.StringgetName(java.lang.String address)
Attempts to get the name of a remote device, otherwise returns the address.

param
address The address.
return
The name, or if unavailable, the address.

        LocalBluetoothDevice device = findDevice(address);
        return device != null ? device.getName() : address;
    
voidonBluetoothStateChanged(boolean enabled)

        if (enabled) {
            readPairedDevices();
        }
    
public synchronized voidonBondingError(java.lang.String address, int reason)
Called when there is a bonding error.

param
address The address of the remote device.
param
reason The reason, one of the error reasons from BluetoothDevice.UNBOND_REASON_*

        mLocalManager.showError(address, R.string.bluetooth_error_title,
                (reason == BluetoothDevice.UNBOND_REASON_AUTH_FAILED) ?
                        R.string.bluetooth_pairing_pin_error_message :
                        R.string.bluetooth_pairing_error_message);
    
public synchronized voidonBondingStateChanged(java.lang.String address, int bondState)

        LocalBluetoothDevice device = findDevice(address);
        if (device == null) {
            if (!readPairedDevices()) {
                Log.e(TAG, "Got bonding state changed for " + address +
                        ", but we have no record of that device.");
            }
            return;
        }

        device.refresh();

        if (bondState == BluetoothDevice.BOND_BONDED) {
            // Auto-connect after pairing
            device.connect();
        }
    
public synchronized voidonBtClassChanged(java.lang.String address)

        LocalBluetoothDevice device = findDevice(address);
        if (device != null) {
            device.refreshBtClass();
        }
    
public synchronized voidonConnectingError(java.lang.String address)

        LocalBluetoothDevice device = findDevice(address);
        if (device == null) return;
        
        /*
         * Go through the device's delegate so we don't spam the user with
         * errors connecting to different profiles, and instead make sure the
         * user sees a single error for his single 'connect' action.
         */
        device.showConnectingError();
    
public synchronized voidonDeviceAppeared(java.lang.String address, short rssi)

        boolean deviceAdded = false;
        
        LocalBluetoothDevice device = findDevice(address);
        if (device == null) {
            device = new LocalBluetoothDevice(mLocalManager.getContext(), address);
            mDevices.add(device);
            deviceAdded = true;
        }
        
        device.setRssi(rssi);
        device.setVisible(true);
        
        if (deviceAdded) {
            dispatchDeviceAdded(device);
        }
    
public synchronized voidonDeviceDisappeared(java.lang.String address)

        LocalBluetoothDevice device = findDevice(address);
        if (device == null) return;
        
        device.setVisible(false);
        checkForDeviceRemoval(device);
    
public synchronized voidonDeviceNameUpdated(java.lang.String address)

        LocalBluetoothDevice device = findDevice(address);
        if (device != null) {
            device.refreshName();
        }
    
public synchronized voidonProfileStateChanged(java.lang.String address, boolean transientState)

        LocalBluetoothDevice device = findDevice(address);
        if (device == null) return;
        
        if (!transientState) {
            device.onProfileStateChanged();
        }
        device.refresh();
    
public synchronized voidonScanningStateChanged(boolean started)

        if (!started) return;
        
        // If starting a new scan, clear old visibility
        for (int i = mDevices.size() - 1; i >= 0; i--) {
            LocalBluetoothDevice device = mDevices.get(i);
            device.setVisible(false);
            checkForDeviceRemoval(device);
        }
    
private synchronized booleanreadPairedDevices()

        BluetoothDevice manager = mLocalManager.getBluetoothManager();
        String[] bondedAddresses = manager.listBonds();
        if (bondedAddresses == null) return false;
        
        boolean deviceAdded = false;
        for (String address : bondedAddresses) {
            LocalBluetoothDevice device = findDevice(address);
            if (device == null) {
                device = new LocalBluetoothDevice(mLocalManager.getContext(), address);
                mDevices.add(device);
                dispatchDeviceAdded(device);
                deviceAdded = true;
            }
        }
        
        return deviceAdded;