Example usage for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE

List of usage examples for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE

Introduction

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

Prototype

String ACTION_REQUEST_ENABLE

To view the source code for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE.

Click Source Link

Document

Activity Action: Show a system activity that allows the user to turn on Bluetooth.

Usage

From source file:hongik.android.project.best.HistoryActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    backHandler = new BackPressCloseHandler(this);

    Intent intent = new Intent(this.getIntent());
    cid = intent.getStringExtra("CID");
    historyTable = (TableLayout) findViewById(R.id.history_table);

    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivity(enableBtIntent);//from w  w  w . ja  v  a 2s .co  m

    setCentralManager();
    drawHistory();
}

From source file:com.quadmeup.smartporttelemetrer.activity.BluetoothSelectActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_bluetooth_select);

    bluetoothManager = BluetoothManager.getInstance();

    deviceList = (ListView) findViewById(R.id.device_list);
    listAdapter = new DeviceListAdapter(this);
    deviceList.setAdapter(listAdapter);//from   w  ww .ja v  a2  s .com

    if (!bluetoothManager.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        return;
    }

    fillDeviceList();
}

From source file:com.android.bleserver.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.activity_main_title);

    if (savedInstanceState == null) {

        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

        // Is Bluetooth supported on this device?
        if (mBluetoothAdapter != null) {

            // Is Bluetooth turned on?
            if (mBluetoothAdapter.isEnabled()) {

                // Are Bluetooth Advertisements supported on this device?
                if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {

                    // Everything is supported and enabled, load the fragments.
                    setupFragments();// w w  w  .  j a  v a 2s . com

                } else {

                    // Bluetooth Advertisements are not supported.
                    showErrorText(R.string.bt_ads_not_supported);
                }
            } else {

                // Prompt user to turn on Bluetooth (logic continues in onActivityResult()).
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
            }
        } else {

            // Bluetooth is not supported.
            showErrorText(R.string.bt_not_supported);
        }
    }
}

From source file:uk.ac.horizon.ubihelper.service.channel.BluetoothDiscoveryChannel.java

/**
 * @param handler// w  ww . j  a  va 2 s.  c o m
 * @param name
 */
public BluetoothDiscoveryChannel(Service service, Handler handler, String name) {
    super(handler, name);
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    this.service = service;
    if (bluetooth == null)
        Log.w(TAG, "No BluetoothAdapter");
    if (!bluetooth.isEnabled()) {
        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        service.sendBroadcast(i);
    }
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
    this.service.registerReceiver(receiver, filter);
}

From source file:sample.hawk.com.mybasicappcomponents.hardware.bt_advertisements.BLE_AdvertisementsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bt_advertisement_activity);
    setTitle(R.string.btadv_activity_title);

    if (savedInstanceState == null) {

        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

        // Is Bluetooth supported on this device?
        if (mBluetoothAdapter != null) {

            // Is Bluetooth turned on?
            if (mBluetoothAdapter.isEnabled()) {

                // Are Bluetooth Advertisements supported on this device?
                if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {

                    // Everything is supported and enabled, load the fragments.
                    setupFragments();//from   w ww.ja v  a  2 s  .co m

                } else {

                    // Bluetooth Advertisements are not supported.
                    showErrorText(R.string.bt_ads_not_supported);
                }
            } else {

                // Prompt user to turn on Bluetooth (logic continues in onActivityResult()).
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, BLE_Constants.REQUEST_ENABLE_BT);
            }
        } else {

            // Bluetooth is not supported.
            showErrorText(R.string.bt_not_supported);
        }
    }
}

From source file:com.google.android.apps.forscience.whistlepunk.devicemanager.ScanDisabledFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_scan_disabled, container, false);
    Button button = (Button) view.findViewById(R.id.btn_enable_scan);

    final boolean hasPermission = hasScanPermission(getActivity());
    if (!hasPermission) {
        // Update the text of the button.
        button.setText(R.string.btn_request_location);
    }/*from   ww  w  .  j  a  v a  2 s  . c o  m*/

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!hasPermission) {
                // Note: this can fail if the user says "never ask again." but there is no
                // API access to know if this is the case.
                ActivityCompat.requestPermissions(getActivity(),
                        new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_FROM_BUTTON);
            } else {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                getActivity().startActivity(intent);
            }
        }
    });
    return view;
}

From source file:com.thiagorosa.robotita.manager.BluetoothManager.java

public void promptToEnable(Fragment fragment) {
    if (!isEnabled() && (fragment != null)) {
        fragment.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
                REQUEST_ENABLE_BLUETOOTH);
    }/*from w w  w. ja  v a  2 s.  c o  m*/
}

From source file:jdev.cordova.bluetooth.ble.BLEHelper.java

private void enable(CallbackContext callbackContext) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    activity.startActivityForResult(enableBtIntent, 0);
}

From source file:org.jmpm.ethbadge.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {//from w  w w .j a v  a2  s  .  com
        GlobalAppSettings.getInstance().initialize(getApplicationContext());

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, BluetoothConstants.REQUEST_ENABLE_BT);
        }

    } catch (Exception e) {
        Utils.sendFeedbackEmail(getApplicationContext(), (Exception) e);
        Toast.makeText(getApplicationContext(), GlobalAppConstants.UNEXPECTED_FATAL_EXCEPTION_MESSAGE,
                Toast.LENGTH_LONG).show();
    }

    try {
        final AppCompatActivity myself = this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Only ask for these permissions on runtime when running Android 6.0 or higher
            switch (ContextCompat.checkSelfPermission(getBaseContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {
            case PackageManager.PERMISSION_DENIED:
                ((TextView) new AlertDialog.Builder(this).setTitle("IMPORTANT").setMessage(Html.fromHtml(
                        "<p>This app requires Bluetooth permissions to work. In addition, some devices require access to device's location permissions to find nearby Bluetooth devices. Please click \"Allow\" on the following runtime permissions popup.</p>"
                                + "<p>For more info see <a href=\"http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id\">here</a>.</p>"))
                        .setNeutralButton("Okay", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (ContextCompat.checkSelfPermission(getBaseContext(),
                                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                    int i = 5;
                                    ActivityCompat.requestPermissions(myself,
                                            new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
                                }
                            }
                        }).show().findViewById(android.R.id.message))
                                .setMovementMethod(LinkMovementMethod.getInstance()); // Make the link clickable. Needs to be called after show(), in order to generate hyperlinks
                break;
            case PackageManager.PERMISSION_GRANTED:
                break;
            }
        }
    } catch (Exception e) {
        Utils.sendFeedbackEmail(getApplicationContext(), (Exception) e);
        Toast.makeText(getApplicationContext(), "Error: Could not set permissions", Toast.LENGTH_LONG).show();
    }
}

From source file:com.google.android.apps.forscience.whistlepunk.devicemanager.ScanDisabledDialogFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().setTitle(R.string.bluetooth_not_on_title);
    View view = inflater.inflate(R.layout.fragment_scan_disabled_dialog, container, false);
    Button button = (Button) view.findViewById(R.id.btn_enable_scan);

    final boolean hasPermission = hasScanPermission(getActivity());
    if (!hasPermission) {
        // Update the text of the button.
        button.setText(R.string.btn_request_location);
    }/*from www  .ja va  2s  .c  o m*/

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!hasPermission) {
                // TODO: is this doing the right thing if user says "never ask again"?
                PermissionUtils.tryRequestingPermission(getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION, REQUEST_FROM_BUTTON, true);
            } else {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                getActivity().startActivity(intent);
            }
            dismiss();
        }
    });
    return view;
}