Java Code Examples for android.bluetooth.BluetoothAdapter#disable()

The following examples show how to use android.bluetooth.BluetoothAdapter#disable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OBConnectionManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public boolean setBluetooth (boolean enable)
{
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter != null)
    {
        boolean isEnabled = bluetoothAdapter.isEnabled();
        if (enable && !isEnabled)
        {
            return bluetoothAdapter.enable();
        }
        else if (!enable && isEnabled)
        {
            return bluetoothAdapter.disable();
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: BluetoothUtils.java    From SimpleSmsRemote with MIT License 6 votes vote down vote up
/**
 * set bluetooth state
 *
 * @param enabled bluetooth state
 * @throws Exception
 */
public static void SetBluetoothState(boolean enabled) throws Exception {
    BluetoothAdapter bluetoothAdapter = getDefaultBluetoothHandle();

    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (enabled && !isEnabled) {
        if (!bluetoothAdapter.enable())
            throw new Exception("enabling bluetooth failed");
    } else if (!enabled && isEnabled) {
        if (!bluetoothAdapter.disable())
            throw new Exception("disabling bluetooth failed");
    }
}
 
Example 3
Source File: BleUtils.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
ResetBluetoothAdapter(Context context, ResetBluetoothAdapterListener listener) {
    // mContext = context;
    mListener = listener;

    // Set receiver
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    context.registerReceiver(mBleAdapterStateReceiver, filter);

    // Reset
    BluetoothAdapter bleAdapter = BleUtils.getBluetoothAdapter(context);
    if (bleAdapter != null && bleAdapter.isEnabled()) {
        boolean isDisablingBle = bleAdapter.disable();
        if (isDisablingBle) {
            Log.w(TAG, "Reset ble adapter started. Waiting to turn off");
            // Now wait for BluetoothAdapter.ACTION_STATE_CHANGED notification
        } else {
            Log.w(TAG, "Can't disable bluetooth adapter");
            resetCompleted(context);
        }
    }
}
 
Example 4
Source File: BleManager.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
/**
 * Turn on or off local bluetooth directly without showing users a request
 * dialog.
 * Note that a request dialog may still show when you call this method, due to
 * some special Android devices' system may have been modified by manufacturers
 *
 * @param enable enable or disable local bluetooth
 */
public static void toggleBluetooth(boolean enable) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        return;
    }
    if (enable) {
        adapter.enable();
    } else {
        if (adapter.isEnabled()) {
            adapter.disable();
        }
    }
}
 
Example 5
Source File: ConnectivityServiceWrapper.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
private static void changeBluetoothState(Intent intent) {
    try {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        int labelResId;
        if (intent.hasExtra(AShortcut.EXTRA_ENABLE)) {
            if (intent.getBooleanExtra(AShortcut.EXTRA_ENABLE, false)) {
                btAdapter.enable();
                labelResId = R.string.bluetooth_on;
            } else {
                btAdapter.disable();
                labelResId = R.string.bluetooth_off;
            }
        } else {
            if (btAdapter.isEnabled()) {
                labelResId = R.string.bluetooth_off;
                btAdapter.disable();
            } else {
                btAdapter.enable();
                labelResId = R.string.bluetooth_on;
            }
        }
        if (intent.getBooleanExtra(AShortcut.EXTRA_SHOW_TOAST, false)) {
            Utils.postToast(mContext, labelResId);
        }
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 6
Source File: FloatingBallUtils.java    From RelaxFinger with GNU General Public License v2.0 5 votes vote down vote up
private static void switchBluetooth() {

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

        if (adapter.isEnabled()) {

            adapter.disable();

            MyApplication.getMainThreadHandler().post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
                }
            });

        } else {

            adapter.enable();
            MyApplication.getMainThreadHandler().post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "蓝牙已开启", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }
 
Example 7
Source File: SignalsActionService.java    From LibreTasks with Apache License 2.0 5 votes vote down vote up
/**
  * turn off the bluetooth.
  */
 private void turnOffBluetooth() {
   BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (mBluetoothAdapter.isEnabled()) {
	mBluetoothAdapter.disable(); 
}
   ResultProcessor.process(this, intent, ResultProcessor.RESULT_SUCCESS,
       getString(R.string.bluetooth_turned_off));
 }
 
Example 8
Source File: BluetoothUtils.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
public static boolean closeBluetooth() {
    BluetoothAdapter adapter = getBluetoothAdapter();
    if (adapter != null) {
        return adapter.disable();
    }
    return false;
}
 
Example 9
Source File: BluetoothUtils.java    From Aerlink-for-Android with MIT License 5 votes vote down vote up
public static void restartBluetooth(BluetoothAdapter bluetoothAdapter) {
    try {
        if (bluetoothAdapter != null) {
            if (bluetoothAdapter.isEnabled()) {
                bluetoothAdapter.disable();
            }
            bluetoothAdapter.enable();

            Log.d(LOG_TAG, "Disabling bluetooth");
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: BluetoothUtils.java    From Aerlink-for-Android with MIT License 5 votes vote down vote up
public static void disableBluetooth(BluetoothAdapter bluetoothAdapter) {
    try {
        if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.disable();

            Log.d(LOG_TAG, "Disabling bluetooth");
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: BluetoothUtil.java    From Rumble with GNU General Public License v3.0 5 votes vote down vote up
public static void disableBT() {
    BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
    if ( adapter == null)
        return;
    if (isEnabled())
        adapter.disable();
}
 
Example 12
Source File: GattServerTests.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void btOffOnWillClearServicesAndThatGattServerIfStartedWillRetunrAfterToggleMultipleTimesInQuickSuccession() throws InterruptedException {
    // should do nothing if already started
    final CountDownLatch cdl = new CountDownLatch(2);
    BluetoothAdapter adapter = new GattUtils().getBluetoothAdapter(mockContext);
    assertNotNull("adapter is null", adapter);
    AtomicBoolean isFirst = new AtomicBoolean(true);
    AtomicInteger countTest = new AtomicInteger(1);
    FitbitGatt.FitbitGattCallback cb = new NoOpGattCallback() {
        @Override
        public void onGattServerStarted(GattServerConnection serverConnection) {
            super.onGattServerStarted(serverConnection);
            if (isFirst.get()) {
                AddGattServerServiceTransaction transaction = new AddGattServerServiceTransaction(serverConnection, GattState.ADD_SERVICE_SUCCESS, liveData);
                serverConnection.runTx(transaction, result -> {
                    assertEquals(result.resultStatus, TransactionResult.TransactionResultStatus.SUCCESS);
                    countTest.incrementAndGet();
                    adapter.disable();
                    cdl.countDown();
                    isFirst.set(false);
                });
            } else if (countTest.get() >= 20) {
                BluetoothGattServer server = FitbitGatt.getInstance().getServer().getServer();
                List<BluetoothGattService> services = server.getServices();
                Assert.assertTrue(services.isEmpty());
                cdl.countDown();
            }
        }

        @Override
        public void onBluetoothOn() {
            super.onBluetoothOn();
            if (countTest.getAndIncrement() > 0 && countTest.getAndIncrement() <= 20) {
                adapter.disable();
            }
        }

        @Override
        public void onBluetoothOff() {
            super.onBluetoothOff();
            adapter.enable();
        }

    };

    FitbitGatt.getInstance().registerGattEventListener(cb);
    FitbitGatt.getInstance().startGattServer(mockContext);
    cdl.await(60, TimeUnit.SECONDS);
    Thread.sleep(2000);
    adapter.enable();
    if (cdl.getCount() != 0) {
        fail(String.format("Not all countdowns have been executed.Not Executed %d", cdl.getCount()));
    }
}
 
Example 13
Source File: BluetoothUtil.java    From bixolon-printer-example with GNU General Public License v2.0 4 votes vote down vote up
public static void stopBluetooth() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter.isEnabled()) {
        bluetoothAdapter.disable();
    }
}