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:mobilelecture.cdp12_app.RecoRangingActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reco_ranging);

    // //from w  w  w .j a va2  s.  c  o m

    //If a user device turns off bluetooth, request to turn it on.
    //?  ?? .
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
    }

    /**
     * In order to use RECO SDK for Android API 23 (Marshmallow) or higher,
     * the location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is required.
     * Please refer to the following permission guide and sample code provided by Google.
     *
     * ? API 23 ()?? , ?? RECO SDK  
     *   (ACCESS_COARSE_LOCATION ? ACCESS_FINE_LOCATION)?  .
     *  ? , ?  ?  ?.
     *
     * http://www.google.com/design/spec/patterns/permissions.html
     * https://github.com/googlesamples/android-RuntimePermissions
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.i("RecoRangingActivity",
                    "The location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is not granted.");
            this.requestLocationPermission();
        } else {
            Log.i("RecoRangingActivity",
                    "The location permission (ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION) is already granted.");
        }
    }

    // ??

    //mRecoManager will be created here. (Refer to the RECOActivity.onCreate())
    //mRecoManager ?  ??. RECOActivity.onCreate()  .

    //Set RECORangingListener (Required)
    //RECORangingListener  . ()
    mRecoManager.setRangingListener(this);

    /**
     * Bind RECOBeaconManager with RECOServiceConnectListener, which is implemented in RECOActivity
     * You SHOULD call this method to use monitoring/ranging methods successfully.
     * After binding, onServiceConenct() callback method is called.
     * So, please start monitoring/ranging AFTER the CALLBACK is called.
     *
     * RECOServiceConnectListener  RECOBeaconManager bind . RECOServiceConnectListener RECOActivity? ? .
     * monitoring ? ranging ?  , ?  "" ? .
     * bind?, onServiceConnect()   ?.    ? monitoring / ranging ?  ?.
     */
    mRecoManager.bind(this);
}

From source file:org.apache.cordova.sipkita.BluetoothPrinter.java

boolean findBT(CallbackContext callbackContext, String name) {
    try {/*w w  w .  j av  a 2  s. c  om*/
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothPort = BluetoothPort.getInstance();
        if (mBluetoothAdapter == null) {
            Log.e(LOG_TAG, "No bluetooth adapter available");
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            this.cordova.getActivity().startActivityForResult(enableBluetooth, 0);
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                // MP300 is the name of the bluetooth printer device
                if (device.getName().equalsIgnoreCase(name)) {
                    mmDevice = device;
                    return true;
                }
            }
        }
        Log.d(LOG_TAG, "Bluetooth Device Found: " + mmDevice.getName());
    } catch (Exception e) {
        String errMsg = e.getMessage();
        Log.e(LOG_TAG, errMsg);
        e.printStackTrace();
        callbackContext.error(errMsg);
    }
    return false;
}

From source file:ch.ethz.inf.vs.a1.fabischn.ble.MainActivity.java

protected void onResume() {
    super.onResume();

    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!mBluetoothAdapter.isEnabled()) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }/* www .j  a v  a2 s . co  m*/
    }
    // We need this for Android 6.0+
    if (checkLocationPermission()) {
        // TODO hooray
    } else {
        ActivityCompat.requestPermissions(this,
                new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 1);
    }

    // Initializes list view adapter.
    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
    scanLeDevice(true);
}

From source file:com.authorwjf.bounce.BluetoothChat.java

public void onStart() {
    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        // Otherwise, setup the chat session
    } else {/*from  www.jav a2s .co m*/
        if (mChatService == null)
            setupChat();
    }
}

From source file:com.easibeacon.examples.shop.MainActivity.java

private void scanBeacons() {
    // Check Bluetooth every time

    ibp = IBeaconProtocol.getInstance(this);

    // Filter based on default easiBeacon UUID, remove if not required
    ibp.setScanUUID(BUUID);/*ww w.j  a va  2 s  .  com*/

    if (!IBeaconProtocol.configureBluetoothAdapter(this)) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_BLUETOOTH_ENABLE);
    } else {
        ibp.setListener(this);
        if (ibp.isScanning())
            ibp.scanIBeacons(false);
        ibp.reset();
        ibp.scanIBeacons(true);
    }
}

From source file:io.github.msc42.masterthemaze.SearchDeviceActivity.java

private boolean startBluetooth() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.error).setMessage(R.string.noBluetoothExisting).setCancelable(false)
                .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();/*w w w  . ja  v  a2s. c  o m*/
                    }
                });

        builder.create().show();
        return false;
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
        // if request is satisfied, in onActivityResult startRepeatedDiscovery is called,
        // otherwise there is an alert dialog, which finishes the app when it is confirmed
        return false;
    }

    return true;
}

From source file:bluetoothchat.BluetoothChatFragment.java

private void EnableBluetooth() {
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
    } else if (mChatService == null) {
        setupChat();/*w  w w  .j  av a2s .  c  om*/
    }
}

From source file:it.prof.iotsemplicedemo.DeviceScanActivity.java

private void InizializeBluetooth() {

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }/*  ww  w  . j  a  v a2s  .  co m*/

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
}

From source file:com.mikroe.hexiwear_android.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listitem_device);

    mHandler = new Handler();
    mDeviceAddresses = new ArrayList<String>();
    mScanTitle = (TextView) findViewById(R.id.scanTitle);
    mDeviceAddresses.add(KWARP_ADDRESS);
    mDeviceAddresses.add(KWARP_ADDRESS_TWO);

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();/*w w  w.j a v a2 s  .  com*/
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!mBluetoothAdapter.isEnabled()) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }

    //checkNotificationEnabled();

    LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
    ComponentName name = startService(new Intent(DeviceScanActivity.this, NotificationService.class));

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

    scanLeDevice(true);

    Intent gattServiceIntent = new Intent(DeviceScanActivity.this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

From source file:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java

@Override
public void onStart() {
    super.onStart();
    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
    } else if (mFlightControllerService == null) {
        setupChat();//from w w  w.j a v  a2  s .co m
    }
}