点击Forget的逻辑如下:
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
System.out.println("ran.zhou-DPS-L139-Click OK Button");
EditText deviceName = (EditText) mRootView.findViewById(R.id.name);
mCachedDevice.setName(deviceName.getText().toString());
break;
case DialogInterface.BUTTON_NEGATIVE:
System.out.println("ran.zhou-DPS-L144-Click Forget Button");
mCachedDevice.unpair();
com.android.settings.bluetooth.Utils.updateSearchIndex(getContext(),
BluetoothSettings.class.getName(), mCachedDevice.getName(),
getString(R.string.bluetooth_settings),
R.drawable.ic_settings_bluetooth, false);
break;
}
}
蓝牙扫描逻辑如下:
private void startScanning() {
System.out.println("ran.zhou-BS-L290-startScanning");
if (isUiRestricted()) {
return;
}
if (!mAvailableDevicesCategoryIsPresent) {
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
mAvailableDevicesCategoryIsPresent = true;
}
if (mAvailableDevicesCategory != null) {
setDeviceListGroup(mAvailableDevicesCategory);
removeAllDevices();
}
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
mAvailableDevicesCategory.removeAll();
mInitialScanStarted = true;
mLocalAdapter.startScanning(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mInitialScanStarted = false;
mInitiateDiscoverable = true;
mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
getListView().setEmptyView(mEmptyView);
mEmptyView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar);
mBluetoothEnabler.setupSwitchBar();
}
以上方法中的mInitialScanStarted属性设置有问题,此处写死了为false,所以导致在后面引用的时候有一个判断,重新走进了扫描的流程。
解决方案:
mInitialScanStarted = false; 改为mInitialScanStarted = (savedInstanceState != null);