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:com.morgan.sunrisealarm.MainActivity.java

@Override
public void onStart() {
    super.onStart();
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    } else if (mBluetoothClient == null) {
        setupClient();//ww  w  .  j  a v  a 2  s.  c  o  m
    }
}

From source file:com.djit.mixfader.sample.BaseActivity.java

/**
 * Handles permissions and features needed by the app
 */// ww w  . j a va 2  s. c  om
@Override
protected void onResume() {
    super.onResume();

    // Asks for Bluetooth activation is needed
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // Asks for permissions if needed
    final int checkCoarseLocation = ContextCompat.checkSelfPermission(this,
            "android.permission.ACCESS_COARSE_LOCATION");
    final int checkFineLocation = ContextCompat.checkSelfPermission(this,
            "android.permission.ACCESS_FINE_LOCATION");
    if (checkCoarseLocation == PackageManager.PERMISSION_DENIED
            && checkFineLocation == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                MY_PERMISSIONS_REQUEST_COARSE_LOCATION);
    } else {
        // Ask for location service if needed
        if (needToEnableLocation(this)) {
            final Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(viewIntent);
        }
    }
}

From source file:com.example.android.adp.MainActivity.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 (mConnectionService == null) {
        setupConnection();/*from   ww  w .  j  av  a2  s .  co  m*/
    }
}

From source file:org.jraf.android.bikey.app.heartrate.bluetooth.BleScanListFragment.java

private boolean ensureBluetoothEnabled() {
    BluetoothManager bluetoothManager = (BluetoothManager) getActivity()
            .getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_ENABLE_BT);
        return false;
    }//from ww  w. j a  v  a 2s .co  m
    return true;
}

From source file:com.bruce.study.demo.github.bluetooth_demo1.HelpFragment.java

public void startbt() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        mkmsg("????");
        return;/*from  w w  w.  j  av  a 2  s . c o m*/
    }
    if (!mBluetoothAdapter.isEnabled()) {
        mkmsg("??");
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableIntent);
    } else {
        mkmsg("??");
        querypaired();
    }
}

From source file:com.ibm.bluelist.MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SensoroManager sensoroManager = SensoroManager.getInstance(getApplicationContext());
    blApplication = (MessengerApplication) getApplication();
    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();//w ww  .  j  a v  a  2  s .co  m
    }
    myIntent = new Intent(getApplicationContext(), PostTrackingNotifier.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if (sensoroManager.isBluetoothEnabled()) {
        /**
         * Enable cloud service (upload sensor data, including battery status, UMM, etc.)Without setup, it keeps in closed status as default.
         **/
        sensoroManager.setCloudServiceEnable(true);
        /**
         * Enable SDK service
         **/
        try {
            sensoroManager.startService();
        } catch (Exception e) {
            e.printStackTrace(); // Fetch abnormal info
        }
    } else {
        Intent bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(bluetoothIntent, REQUEST_ENABLE_BT);
    }
    BeaconManagerListener beaconManagerListener = new BeaconManagerListener() {
        ArrayList<String> foundSensors = new ArrayList<String>();

        @Override
        public void onUpdateBeacon(ArrayList<Beacon> beacons) {
            // Refresh sensor info
            for (Beacon beacon : beacons) {
                if (true) {
                    if (beacon.getMovingState() == Beacon.MovingState.DISABLED) {
                        // Disable accelerometer
                        System.out.println(beacon.getSerialNumber() + "Disabled");
                        Log.d("Main", beacon.getSerialNumber() + "Disabled");

                    } else if (beacon.getMovingState() == Beacon.MovingState.STILL) {
                        // Device is at static
                        Log.d("Main", beacon.getSerialNumber() + "static");
                        Log.d("Main", beacon.getProximityUUID());
                        if (!foundSensors.contains(beacon.getProximityUUID())) {
                            //String messageDisplay = "";
                            Log.d("Main", "getting Beacon Info");
                            foundSensors.add(beacon.getProximityUUID());
                            IBMCloudCode.initializeService();
                            IBMCloudCode myCloudCodeService = IBMCloudCode.getService();
                            String id = beacon.getProximityUUID();
                            String url = "/getNotification?id=" + id;
                            myCloudCodeService.get(url).continueWith(new Continuation<IBMHttpResponse, Void>() {

                                @Override
                                public Void then(Task<IBMHttpResponse> task) throws Exception {
                                    if (task.isCancelled()) {
                                        Log.e(CLASS_NAME,
                                                "Exception : Task" + task.isCancelled() + "was cancelled.");
                                    } else if (task.isFaulted()) {
                                        Log.e(CLASS_NAME, "Exception : " + task.getError().getMessage());
                                    } else {
                                        InputStream is = task.getResult().getInputStream();
                                        try {
                                            BufferedReader in = new BufferedReader(new InputStreamReader(is));
                                            String responseString = "";
                                            String myString = "";
                                            while ((myString = in.readLine()) != null)
                                                responseString += myString;

                                            in.close();
                                            Log.i(CLASS_NAME, "Response Body: " + responseString);
                                            JSONObject obj = new JSONObject(responseString);
                                            final String name = obj.getString("message");
                                            final String meets = obj.getString("meetups");

                                            /*Notification*/

                                            myIntent.putExtra("response", responseString);
                                            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                                                    getApplicationContext(), 5, myIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
                                            final NotificationCompat.Builder builder = new NotificationCompat.Builder(
                                                    getApplicationContext());
                                            builder.setSmallIcon(R.drawable.logo);
                                            builder.setContentTitle("IBM Beacons Messenger");
                                            builder.setContentText(name);
                                            builder.setContentIntent(pendingNotificationIntent);
                                            NotificationManager notificationManager = (NotificationManager) getSystemService(
                                                    Context.NOTIFICATION_SERVICE);
                                            notificationManager.notify(5, builder.build());

                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }

                                        Log.i(CLASS_NAME, "Response Status from login: "
                                                + task.getResult().getHttpResponseCode());
                                    }

                                    return null;
                                }

                            });

                        }
                        Log.d("Main", beacon.getMajor().toString());
                        Log.d("Main", beacon.getMinor().toString());
                        //Log.d("Main",beacon.getRssi().toString());
                        System.out.println(beacon.getSerialNumber() + "static");

                    } else if (beacon.getMovingState() == Beacon.MovingState.MOVING) {
                        // Device is moving
                        Log.d("Main", beacon.getSerialNumber() + "moving");
                        Log.d("Main", beacon.getProximityUUID());
                        System.out.println(beacon.getSerialNumber() + "moving");
                    }
                }
            }
        }

        @Override
        public void onNewBeacon(Beacon beacon) {
            // New sensor found
            System.out.println(beacon.getSerialNumber());
            Log.d("Main", beacon.getSerialNumber() + "Got New");
        }

        @Override
        public void onGoneBeacon(Beacon beacon) {
            // A sensor disappears from the range
            System.out.println(beacon.getSerialNumber());

        }
    };
    sensoroManager.setBeaconManagerListener(beaconManagerListener);
}

From source file:cloudbank.ble.activity.DeviceScanActivity.java

@Override
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);
        }//from  ww  w. ja v  a 2 s .co m
    }

}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // TODO when bluetooth is off, concurrently the onCreate keeps running.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();/*w ww.  j  av a2 s.  c  o m*/
        return;
    }

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

    // TODO onActivityResult, check if Cancelled or not, then continue or leave
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        // REQUEST_ENABLE_BT will be returned as requestCode from onActivityResult
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // TODO change this if onActivity Result is used
    // Continues scanning if enabled, else toast and finish
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    // TODO lookup the correct idiom for this, look at onRequestPermissionsResult
    // We need this for Android 6.0+
    if (checkLocationPermission()) {

    } else {
        ActivityCompat.requestPermissions(this,
                new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, 1);
    }

    if (mBluetoothAdapter != null) {
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    }
    ScanFilter.Builder filterBuilder = new ScanFilter.Builder();
    mScanFilter = filterBuilder.setDeviceName("Smart Humigadget").build();
    ScanSettings.Builder settingsBuilder = new ScanSettings.Builder();
    mScanSettings = settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            //                .setMatchMode(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            //                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
            .build();
    mScanFilters = new ArrayList<ScanFilter>();
    mScanFilters.add(mScanFilter);

}

From source file:com.techjoynt.android.nxt.activity.RemoteControl.java

@Override
protected void onStart() {
    super.onStart();
    if (mBluetoothAdapter == null) {
        noBluetoothOnDevice().show();//from  w  ww .  j  a v a2  s  .  c  om
    } else if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

From source file:com.example.android.amplacenta.GuestFragment.java

@Override
public void onStart() {
    super.onStart();
    // If BT is not on, request that it be enabled.
    // joinParty() 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 (guestService == null) {
        joinParty();/*from   ww  w  . j  a v  a  2s  . c om*/
    }
}