@Override
  protected void onResume() {
    super.onResume();

    // Repopulate (which isn't too bad since it's cached in the settings
    // bluetooth manager
    addDevices();

    if (mScreenType == SCREEN_TYPE_SETTINGS) {
      mEnabler.resume();
      mDiscoverableEnabler.resume();
      mNamePreference.resume();
    }

    mLocalManager.registerCallback(this);

    mDeviceList.setProgress(mLocalManager.getBluetoothAdapter().isDiscovering());
    mLocalManager.startScanning(false);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    registerReceiver(mReceiver, intentFilter);
    mLocalManager.setForegroundActivity(this);
  }
 private void onBluetoothStateChanged(int bluetoothState) {
   // When bluetooth is enabled (and we are in the activity, which we are),
   // we should start a scan
   if (bluetoothState == BluetoothAdapter.STATE_ON) {
     mLocalManager.startScanning(false);
   } else if (bluetoothState == BluetoothAdapter.STATE_OFF) {
     mDeviceList.setProgress(false);
   }
 }
  @Override
  protected void onPause() {
    super.onPause();

    mDevicePreferenceMap.clear();
    mDeviceList.removeAll();

    mLocalManager.setForegroundActivity(null);
    mDevicePreferenceMap.clear();
    mDeviceList.removeAll();
    unregisterReceiver(mReceiver);

    mLocalManager.unregisterCallback(this);
    if (mScreenType == SCREEN_TYPE_SETTINGS) {
      mNamePreference.pause();
      mDiscoverableEnabler.pause();
      mEnabler.pause();
    }
  }
 public void onScanningStateChanged(boolean started) {
   mDeviceList.setProgress(started);
 }
 public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
   BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
   if (preference != null) {
     mDeviceList.removePreference(preference);
   }
 }
 private void createDevicePreference(CachedBluetoothDevice cachedDevice) {
   BluetoothDevicePreference preference = new BluetoothDevicePreference(this, cachedDevice);
   mDeviceList.addPreference(preference);
   mDevicePreferenceMap.put(cachedDevice, preference);
 }