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.mattprecious.notisync.service.PrimaryService.java

private Notification buildRunningNotification(boolean nullIfNoChange) {
    Bundle notificationBundle = new Bundle();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setPriority(NotificationCompat.PRIORITY_MIN);
    builder.setSmallIcon(R.drawable.ic_stat_logo);
    builder.setContentTitle(getString(R.string.app_name));

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);

    String contentText = null;/*w  w w. j  ava 2  s . com*/
    Set<String> connectedNames = Sets.newHashSet();

    if (bluetoothAdapter.isEnabled()) {
        synchronized (bluetoothServices) {
            for (String address : bluetoothServices.keySet()) {
                BluetoothService service = bluetoothServices.get(address);
                if (service != null && service.getState() == BluetoothService.STATE_CONNECTED) {
                    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
                    connectedNames.add(device.getName());
                }
            }
        }

        if (connectedNames.size() != Preferences.getDevices(this).size()) {
            PendingIntent reconnectIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_RECONNECT),
                    0);
            builder.addAction(R.drawable.ic_action_refresh, getString(R.string.noti_action_connect),
                    reconnectIntent);

            notificationBundle.putBoolean("connect_action", true);
        }

        if (connectedNames.size() > 0) {
            contentText = getString(R.string.noti_connected_to, notificationJoiner.join(connectedNames));
        } else {
            contentText = getString(R.string.noti_not_connected);
        }
    } else {
        contentText = getString(R.string.noti_bt_not_enabled);

        PendingIntent bluetoothIntent = PendingIntent.getActivity(this, 0,
                new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 0);
        builder.addAction(R.drawable.ic_action_bluetooth, getString(R.string.noti_action_enable_bt),
                bluetoothIntent);

        notificationBundle.putBoolean("bt_enable_action", true);
    }

    builder.setContentText(contentText);
    notificationBundle.putString("text", contentText);

    if (nullIfNoChange && bundleEquals(notificationBundle, lastNotificationBundle)) {
        return null;
    }

    lastNotificationBundle = notificationBundle;

    return builder.build();
}

From source file:com.hardcopy.retroband.MainActivity.java

/**
 * Initialization / Finalization/*from w ww.ja v a 2s .  c  o m*/
 */
private void initialize() {
    Logs.d(TAG, "# Activity - initialize()");
    mService.setupService(mActivityHandler);

    // If BT is not on, request that it be enabled.
    // RetroWatchService.setupBT() will then be called during
    // onActivityResult
    if (!mService.isBluetoothEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, Constants.REQUEST_ENABLE_BT);
    }

    // Load activity reports and display
    if (mRefreshTimer != null) {
        mRefreshTimer.cancel();
    }

    // Use below timer if you want scheduled job
    // mRefreshTimer = new Timer();
    // mRefreshTimer.schedule(new RefreshTimerTask(), 5*1000);
}

From source file:com.google.android.gcm.demo.app.BluetoothHDPActivity.java

@Override
protected void onStart() {
    super.onStart();
    // If Bluetooth is not on, request that it be enabled.
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    } else {//from www.ja va 2s .co  m
        initialize();
    }
}

From source file:de.bogutzky.psychophysiocollector.app.MainActivity.java

private void checkBtEnabled() {
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        Toast.makeText(this, getString(R.string.bluetooth_not_supported), Toast.LENGTH_LONG).show();
    } else if (!bluetoothAdapter.isEnabled()) {
        AlertDialog dialog;//from www  .j ava  2s .  c om
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setCancelable(true);
        builder.setTitle(getString(R.string.activate_bluetooth));
        builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        });
        builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }
}

From source file:com.shenqu.jlplayer.nRFUARTv2.MainActivity.java

private void onConnectDisconnect() {
    if (!mBtAdapter.isEnabled()) {
        Log.i(TAG, "onClick - BT not enabled yet");
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    } else {/*from   ww w . j  av a 2 s. c o m*/
        if (btnConnectDisconnect.getText().equals("Connect")) {
            //Connect button pressed, open DeviceList-Activity class, with popup windows that scan for devices
            Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class);
            startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
        } else {
            //Disconnect button pressed
            if (mDevice != null) {
                mService.disconnect();
            }
        }
    }
}

From source file:activities.GatewayActivity.java

@Override
public void WATCHiTSwitchClicked(boolean on) {
    if (on) {/*from  w  w w . j  a v a2s. c  om*/
        if (sApp.currentActiveSpace == null) {
            showToast(getString(R.string.register_event_first));
            configFragment.updateWATCHiTView(false);
            return;
        }
        if (!btAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
            sApp.isWATChiTOn = false;
            return;
        } else {
            sApp.service.start();
            //sApp.isWATChiTOn = true;
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
            arrayAdapter = new ArrayList<String>();
            devices = new ArrayList<BluetoothDevice>();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    arrayAdapter.add("Name: " + device.getName() + "\n" + getString(R.string.adress) + " :"
                            + device.getAddress());
                    devices.add(device);
                    //sApp.arrayAdapter.add()
                }
                sApp.bluetoothDevices = devices;
            }
            Bundle b = new Bundle();
            b.putStringArrayList("adapter", arrayAdapter);
            ChooseBlueToothDeviceDialog dialog = new ChooseBlueToothDeviceDialog();
            dialog.setArguments(b);
            dialog.show(getSupportFragmentManager(), "pairedbluetoothdevices");
        }
    }
    if (!on) {
        if (sApp.service.isRunning())
            sApp.service.stop();
        showToast(getString(R.string.toast_stopped_watchit_service));
        sApp.isWATChiTOn = false;
    }
    configFragment.updateWATCHiTView(sApp.isWATChiTOn);
}

From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.NearbyBeaconsFragment.java

private void startScanningDisplay(long scanStartTime, boolean hasResults) {

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        //Bluetooth not available
        Toast.makeText(getActivity(), getResources().getString(R.string.bluetoothNotAvailable),
                Toast.LENGTH_LONG);/*  w w  w  .  ja v  a2s .c o m*/
    } else if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BLUETOOTH);
    } else {
        // Start the scanning animation only if we don't haven't already been scanning
        // for long enough
        Log.d(TAG, "startScanningDisplay " + scanStartTime + " " + hasResults);
        long elapsedMillis = new Date().getTime() - scanStartTime;
        if (arrowDownLayout != null) {
            arrowDownLayout.setVisibility(View.VISIBLE);
        }
        if (elapsedMillis < FIRST_SCAN_TIME_MILLIS
                || (elapsedMillis < SECOND_SCAN_TIME_MILLIS && !hasResults)) {
            mScanningAnimationTextView.setAlpha(1f);
            mScanningAnimationDrawable.start();
            if (getListView() != null) {
                getListView().setVisibility(View.INVISIBLE);
            }
        } else {
            showListView();
        }

        // Schedule the timeouts
        // We delay at least 50 milliseconds to give the discovery service a chance to
        // give us cached results.
        mSecondScanComplete = false;
        long firstDelay = Math.max(FIRST_SCAN_TIME_MILLIS - elapsedMillis, 50);
        long secondDelay = Math.max(SECOND_SCAN_TIME_MILLIS - elapsedMillis, 50);
        long thirdDelay = Math.max(THIRD_SCAN_TIME_MILLIS - elapsedMillis, 50);
        mHandler.postDelayed(mFirstScanTimeout, firstDelay);
        mHandler.postDelayed(mSecondScanTimeout, secondDelay);
        mHandler.postDelayed(mThirdScanTimeout, thirdDelay);
    }
}

From source file:com.javadog.bluetoothproximitylock.BluetoothFragment.java

/**
 * Handles starting of the service if all necessary conditions are met.
 *//*from   www.  j a v  a  2  s . co  m*/
protected void startBtService() {
    if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        Intent startIntent = new Intent(getActivity().getApplicationContext(), SignalReaderService.class);
        getActivity().getApplicationContext().startService(startIntent);
    } else {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_CODE_ENABLE_BT);
        updateBtServiceUI();
    }
}

From source file:com.buddi.client.dfu.DfuActivity.java

private void showBLEDialog() {
    final Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}

From source file:com.example.android.lightcontrol.MainActivity.java

@Override
public void onStart() {
    super.onStart();

    if (mBluetoothAdapter.isEnabled()) {
        try {//from   w w  w. jav  a 2  s  .  c om
            Thread.sleep(100);
        } catch (InterruptedException ignored) {
        }
        if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
            openOptionsDialog();
        }
    } else {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }
}