Example usage for android.bluetooth BluetoothAdapter enable

List of usage examples for android.bluetooth BluetoothAdapter enable

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter enable.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean enable() 

Source Link

Document

Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.

Usage

From source file:com.sonymobile.dronecontrol.activity.MainActivity.java

private boolean verifyBluetooth() {
    boolean result = false;
    final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        // Device does not support Bluetooth
    } else if (btAdapter.isEnabled()) {
        result = true;/*from   w  w w  . j a v  a  2 s.  c o  m*/
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Bluetooth").setMessage(R.string.DG_TURN_ON_BT)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        btAdapter.enable();
                        dialog.dismiss();
                    }
                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }
    return result;
}

From source file:com.android.launcher3.Utilities.java

public static boolean setBluetooth(boolean enable) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (enable && !isEnabled) {
        isBluetoothOn = true;//from   w  w  w. j  a  v  a 2 s  .  com
        return bluetoothAdapter.enable();
    } else if (!enable && isEnabled) {
        isBluetoothOn = false;
        return bluetoothAdapter.disable();
    }
    // No need to change bluetooth state
    return true;
}

From source file:com.evothings.BLE.java

private void reset(final CordovaArgs args, final CallbackContext cc) throws JSONException {
    mResetCallbackContext = null;// w w  w.ja v a2  s .c  o  m
    BluetoothAdapter a = BluetoothAdapter.getDefaultAdapter();
    if (mScanCallbackContext != null) {
        a.stopLeScan(this);
        mScanCallbackContext = null;
    }
    int state = a.getState();
    //STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF.
    if (state == BluetoothAdapter.STATE_TURNING_ON) {
        // reset in progress; wait for STATE_ON.
        mResetCallbackContext = cc;
        return;
    }
    if (state == BluetoothAdapter.STATE_TURNING_OFF) {
        // reset in progress; wait for STATE_OFF.
        mResetCallbackContext = cc;
        return;
    }
    if (state == BluetoothAdapter.STATE_OFF) {
        boolean res = a.enable();
        if (res) {
            mResetCallbackContext = cc;
        } else {
            cc.error("enable");
        }
        return;
    }
    if (state == BluetoothAdapter.STATE_ON) {
        boolean res = a.disable();
        if (res) {
            mResetCallbackContext = cc;
        } else {
            cc.error("disable");
        }
        return;
    }
    cc.error("Unknown state: " + state);
}