@Override protected void onPause() { if (mNearbyCapable) { mDeviceManager.stopSearchingForDevices(); } super.onPause(); }
@Override public void onResume() { super.onResume(); if (mNearbyCapable && PrefUtils.hasEnabledBle(this)) { mDeviceManager.startSearchingForDevices(); } }
// Updates the nearby button's text to be the number of devices found. If no devices have been // found, the text is set to "Nearby". private void updateNearbyButton() { int count = mDeviceManager.getAdapter().getCount(); final String text = (count == 0 ? "" : (String.valueOf(count) + " ")) + getResources().getString(R.string.map_nearby_button); if (mNearbyButton != null) { mNearbyButton.setText(text); mNearbyButton.setSelected(count > 0); } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private boolean initNearby() { PackageManager pm = getPackageManager(); if (pm != null && pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); BluetoothAdapter adapter = manager.getAdapter(); if (adapter != null) { MetadataResolver.initialize(this); mDeviceManager = new NearbyDeviceManager(this, adapter); mDeviceManager .getAdapter() .registerDataSetObserver( new DataSetObserver() { @Override public void onChanged() { updateNearbyButton(); } }); return true; } } return false; }
// Handles a click on the Nearby menu item. Recognizes several states: // 1. If the NearbyFragment is currently visible, does nothing. // 2. If the user has not enabled Nearby, presents them with a legalese screen asking them to // opt in. // 3. If the device's Bluetooth is disabled, ask the user to enable it. // 4. Otherwise, shows the fragment. private void handleNearbyClick() { final FragmentManager manager = getFragmentManager(); Fragment fragment = manager.findFragmentByTag(NEARBY_FRAGMENT_TAG); if (fragment != null && fragment.isVisible()) { return; } if (!PrefUtils.hasEnabledBle(this)) { // If the user has not enabled Nearby, present them with a legalese screen asking them // to do so. Intent intent = new Intent(this, NearbyEulaActivity.class); startActivityForResult(intent, REQUEST_ENABLE_NEARBY); return; } if (!mDeviceManager.isEnabled()) { // If the user's bluetooth is not enabled, ask them politely to turn it on. Beginning // to search for devices should happen in onStart? DialogFragment dialogFragment = new EnableBluetoothDialogFragment(); dialogFragment.show(getFragmentManager(), "EnableBluetoothDialogFragment"); return; } // Otherwise, the user has opted into BLE and their Bluetooth device is turned on! showNearbyFragment(NEARBY_FRAGMENT_TAG); }