BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth } else { if (!mBluetoothAdapter.isEnabled()) { // Bluetooth is not enable :) } }
public boolean isBluetoothEnabled() { return bluetoothAdapter != null && bluetoothAdapter.isEnabled(); }
//Disable bluetooth BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.disable(); }
public static boolean setBluetooth(boolean enable) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); boolean isEnabled = bluetoothAdapter.isEnabled(); if (enable && !isEnabled) { return bluetoothAdapter.enable(); } else if(!enable && isEnabled) { return bluetoothAdapter.disable(); } // No need to change bluetooth state return true; }
if (btAdapter.isEnabled()) { SharedPreferences prefs_btdev = getSharedPreferences("btdev", 0); String btdevaddr=prefs_btdev.getString("btdevaddr","?");
/** * Check if Bluetooth LE is supported by this Android device, and if so, make sure it is enabled. * * @return false if it is supported and not enabled * @throws BleNotAvailableException if Bluetooth LE is not supported. (Note: The Android emulator will do this) */ @TargetApi(18) public boolean checkAvailability() throws BleNotAvailableException { if (!isBleAvailableOrSimulated()) { throw new BleNotAvailableException("Bluetooth LE not supported by this device"); } return ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().isEnabled(); }
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter(); if (blueAdapter != null) { if (blueAdapter.isEnabled()) { Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();
@Test public void testAdapterDefaultsDisabled() { assertThat(bluetoothAdapter.isEnabled()).isFalse(); }
@Test public void canEnable_withAndroidApi() throws Exception { bluetoothAdapter.enable(); assertThat(bluetoothAdapter.isEnabled()).isTrue(); }
@Test public void testAdapterCanBeEnabled_forTesting() { shadowBluetoothAdapter.setEnabled(true); assertThat(bluetoothAdapter.isEnabled()).isTrue(); }
@RequiresApi(api = Build.VERSION_CODES.O) void stopAndroidOBackgroundScan() { try { final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null) { LogManager.w(TAG, "Failed to construct a BluetoothAdapter"); } else if (!bluetoothAdapter.isEnabled()) { LogManager.w(TAG, "BluetoothAdapter is not enabled"); } else { BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); if (scanner != null) { scanner.stopScan(getScanCallbackIntent()); } } } catch (SecurityException e) { LogManager.e(TAG, "SecurityException stopping Android O background scanner"); } catch (RuntimeException e) { // Needed to stop a crash caused by internal Android throw. See issue #701 LogManager.e(TAG, "Unexpected runtime exception stopping Android O background scanner", e); } }
@Test public void canDisable_withAndroidApi() throws Exception { shadowBluetoothAdapter.setEnabled(true); bluetoothAdapter.disable(); assertThat(bluetoothAdapter.isEnabled()).isFalse(); }
Toast.makeText(this, "Device Does not Support Bluetooth", Toast.LENGTH_LONG).show(); else if (!myBt.isEnabled()) {
if (bluetoothAdapter == null) { LogManager.w(TAG, "Failed to construct a BluetoothAdapter"); } else if (!bluetoothAdapter.isEnabled()) { LogManager.w(TAG, "Failed to start background scan on Android O: BluetoothAdapter is not enabled"); } else {
/** * Check for Bluetooth. * @return True if Bluetooth is available. */ public static boolean isBluetoothAvailable() { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return (bluetoothAdapter != null && bluetoothAdapter.isEnabled()); }
@Override public synchronized void initAndCheckEligibility() throws ScannerException { if (Build.VERSION.SDK_INT < ScannerFactory.OS_VERSION_LOLLIPOP) { throw new ScannerException(ScannerException.Type.NOT_SUPPORTED); } if (!Validate.hasBluetoothPermission(context)) { throw new ScannerException(ScannerException.Type.PERMISSION_DENIED); } if (!Validate.hasLocationPermission(context)) { throw new ScannerException(ScannerException.Type.PERMISSION_DENIED); } bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { throw new ScannerException(ScannerException.Type.DISABLED); } bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner == null) { throw new ScannerException(ScannerException.Type.UNKNOWN_ERROR); } }
try { if (getBluetoothAdapter() != null) { if (getBluetoothAdapter().isEnabled()) { if (mBluetoothCrashResolver != null && mBluetoothCrashResolver.isRecoveryInProgress()) { LogManager.w(TAG, "Skipping scan because crash recovery is in progress.");
if (mScanning) { if (getBluetoothAdapter() != null) { if (getBluetoothAdapter().isEnabled()) {
/** * Request for enable the device's bluetooth asynchronously. * Throw a NullPointerException if the device doesn't have a bluetooth module. */ public void requestEnableBt() { if (mBluetoothAdapter == null) { throw new NullPointerException(DEVICE_HAS_NOT_BLUETOOTH_MODULE); } if (!mBluetoothAdapter.isEnabled()) mBluetoothAdapter.enable(); }