Java Code Examples for android.bluetooth.BluetoothAdapter#ACTION_REQUEST_ENABLE

The following examples show how to use android.bluetooth.BluetoothAdapter#ACTION_REQUEST_ENABLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceScanActivity.java    From connectivity-samples with Apache License 2.0 6 votes vote down vote up
@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()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // Initializes list view adapter.
    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
    scanLeDevice(true);
}
 
Example 2
Source File: MainActivity.java    From Android-nRF-BLE-Joiner with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void prepareForScan(){
    if(isBleSupported()) {
        final ParcelUuid uuid = NODE_CONFIGURATION_SERVICE;
        mScanFilterList = new ArrayList<>();
        mScanFilterList.add(new ScanFilter.Builder().setServiceUuid(uuid).build());
        mScanner = BluetoothLeScannerCompat.getScanner();

        if (checkIfVersionIsMarshmallowOrAbove()) {
            startLocationModeChangeReceiver();
            connectToGoogleApiClient();
        } else {
            if (!isBleEnabled()) {
                final Intent bluetoothEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(bluetoothEnable, REQUEST_ENABLE_BT);
            } else {
                startLeScan();
            }
        }
    } else {
        showError(getString(R.string.ble_not_supported), false);
    }
}
 
Example 3
Source File: HelpFragment.java    From bluetooth with Apache License 2.0 6 votes vote down vote up
public void startbt() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
        logthis("This device does not support bluetooth");
        return;
    }
    //make sure bluetooth is enabled.
    if (!mBluetoothAdapter.isEnabled()) {
        logthis("There is bluetooth, but turned off");
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        logthis("The bluetooth is ready to use.");
    }
}
 
Example 4
Source File: DeviceScanActivity.java    From android-midisuite with Apache License 2.0 6 votes vote down vote up
@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);
        }
    }

    // Initializes list view adapter.
    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
    scanLeDevice(true);
}
 
Example 5
Source File: ListActivity.java    From myo_AndoridEMG with MIT License 6 votes vote down vote up
public void scanDevice() {
    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        deviceNames.clear();
        // Scanning Time out by Handler.
        // The device scanning needs high energy.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBluetoothAdapter.stopLeScan(ListActivity.this);

                adapter.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(), "Stop Device Scan", Toast.LENGTH_SHORT).show();

            }
        }, SCAN_PERIOD);
        mBluetoothAdapter.startLeScan(ListActivity.this);
    }
}
 
Example 6
Source File: DeviceScanActivity.java    From GizwitsBLE with Apache License 2.0 6 votes vote down vote up
@Override
protected void onResume() {
	super.onResume();
	registerReceiver(mBleReceiver, BleService.getIntentFilter());

	// 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 (mBle != null && !mBle.adapterEnabled()) {
		Intent enableBtIntent = new Intent(
				BluetoothAdapter.ACTION_REQUEST_ENABLE);
		startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
	}

	// Initializes list view adapter.
	mLeDeviceListAdapter = new LeDeviceListAdapter();
	setListAdapter(mLeDeviceListAdapter);
	scanLeDevice(true);
}
 
Example 7
Source File: BleManager.java    From EasyBle with Apache License 2.0 6 votes vote down vote up
/**
 * Turn on local bluetooth, calling the method will show users a request dialog
 * to grant or reject,so you can get the result from Activity#onActivityResult()
 *
 * @param activity    activity, note that to get the result whether users have granted
 *                    or rejected to enable bluetooth, you should handle the method
 *                    onActivityResult() of this activity
 * @param requestCode enable bluetooth request code
 */
public static void enableBluetooth(Activity activity, int requestCode) {
    if (activity == null || requestCode < 0) {
        return;
    }
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null && !adapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(intent, requestCode);
    }
}
 
Example 8
Source File: LinkDeviceFragment.java    From SmartOrnament with Apache License 2.0 6 votes vote down vote up
private boolean openBtDevice() {
        // 获得蓝牙匹配器
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 蓝牙设备不被支持
        if (mBluetoothAdapter == null) {
            Toast.makeText(getActivity(), "该设备没有蓝牙设备", Toast.LENGTH_LONG).show();
            return false;
        }

        // 蓝牙如果没打开,直接提示需要打开蓝牙
        if (!mBluetoothAdapter.isEnabled()) {
            // 隐式Intent
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUES_BT_ENABLE_CODE);  //在onActivityResult有打开服务端线程的操作哦
        } else {
            // 如果蓝牙在运行本程序前已经人为打开,那么直接启动服务器端线程
            Toast.makeText(getActivity(), "蓝牙已经打开!	", Toast.LENGTH_LONG).show();
            //暂时不启动服务端线程,因为手机充当的总是客服端
//            mAcceptThread = new AcceptThread(mBluetoothAdapter,mHandler); // // // // // // // // // // // // // // // // // // // // // // // // //
//            mAcceptThread.start(); // // // // // // // // // // // // // // // //蓝牙提前打开就启动服务端线程? // // // // // //
        }
        return true;
    }
 
Example 9
Source File: AntiTheftAty.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
private void startBTAntiTheft() {
	//取得适配器
	if (mBluetoothAdapter == null){
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	}
	if (mBluetoothAdapter == null){
		showMsg("蓝牙不可用");
		tvMsg.setText("蓝牙不可用");
	}else{
		if (!mBluetoothAdapter.isEnabled()) {
			showMsg("正在请求打开蓝牙");
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }else{
        	handleBondBTDevice();
        }
	}
}
 
Example 10
Source File: SensorTagApplicationClass.java    From SensorTag-CC2650 with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {

    // 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_LONG)
                .show();
        mBleSupported = false;
    }

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

    // Checks if Bluetooth is supported on the device.
    if (mBtAdapter == null) {
        Toast.makeText(this, R.string.bt_not_supported, Toast.LENGTH_LONG).show();
        mBleSupported = false;
        return;
    }

    mFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, mFilter);

    if (!mBtAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(enableIntent);
    }

    startBluetoothLeService();

    super.onCreate();

}
 
Example 11
Source File: ScanDeviceFragment.java    From ESeal with Apache License 2.0 5 votes vote down vote up
@Override
public void onRefresh() {
    if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
        scanDevice();
    } else {
        Intent enableBtIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}
 
Example 12
Source File: MainActivity.java    From Android-nRF-UART with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume");
    if (!mBtAdapter.isEnabled()) {
        Log.i(TAG, "onResume - BT not enabled yet");
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }
 
}
 
Example 13
Source File: MainActivity.java    From physical-web with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures Bluetooth is available on the beacon and it is enabled. If not,
 * displays a dialog requesting user permission to enable Bluetooth.
 */
private void checkPermissions(BluetoothAdapter bluetoothAdapter) {
  // Acquire lock
  PermissionCheck.getInstance().setCheckingPermissions(true);
  if (!bluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    return;
  }
  ensureLocationPermissionIsEnabled();
}
 
Example 14
Source File: Ble.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
/**
 * 打开蓝牙(默认模式--带系统弹出框)
 *
 * @param activity 上下文对象
 */
public void turnOnBlueTooth(Activity activity) {
    // 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 (!isBleEnable()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}
 
Example 15
Source File: SwapWorkflowActivity.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The process for setting up bluetooth is as follows:
 * <ul>
 * <li>Assume we have bluetooth available (otherwise the button which allowed us to start
 * the bluetooth process should not have been available)</li>
 * <li>Ask user to enable (if not enabled yet)</li>
 * <li>Start bluetooth server socket</li>
 * <li>Enable bluetooth discoverability, so that people can connect to our server socket.</li>
 * </ul>
 * Note that this is a little different than the usual process for bluetooth _clients_, which
 * involves pairing and connecting with other devices.
 */
public void startBluetoothSwap() {
    if (bluetoothAdapter != null) {
        if (bluetoothAdapter.isEnabled()) {
            Utils.debugLog(TAG, "Bluetooth enabled, will check if device is discoverable with device.");
            ensureBluetoothDiscoverableThenStart();
        } else {
            Utils.debugLog(TAG, "Bluetooth disabled, asking user to enable it.");
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_BLUETOOTH_ENABLE_FOR_SWAP);
        }
    }
}
 
Example 16
Source File: MainActivity.java    From Android-nRF-BLE-Joiner with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.action_start_scan:
            if (checkIfVersionIsMarshmallowOrAbove()) {
                if (mLocationServicesRequestApproved)
                    checkForLocationPermissionsAndScan();
                else {
                    createLocationRequestForResult();
                }
            } else {
                if (!isBleEnabled()) {
                    final Intent bluetoothEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(bluetoothEnable, REQUEST_ENABLE_BT);
                } else {
                    startLeScan();
                }
            }
            return true;
        case R.id.action_stop_scan:
            stopLeScan();
            return true;
        case R.id.action_configure_wifi:
            if(mScanning)
                stopLeScan();
            final Intent configureWifi = new Intent(this, ConfigureWifiActivity.class);
            startActivity(configureWifi);
            return true;
        case R.id.action_settings:
            if(mScanning)
                stopLeScan();
            final Intent settings = new Intent(this, AboutActivity.class);
            startActivity(settings);
            return true;
        case android.R.id.home:
            onBackPressed();
            return true;
    }

    return super.onOptionsItemSelected(item);
}
 
Example 17
Source File: PairingActivity.java    From microbit with Apache License 2.0 4 votes vote down vote up
/**
 * Starts activity to enable bluetooth.
 */
private void enableBluetooth() {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, RequestCodes.REQUEST_ENABLE_BT);
}
 
Example 18
Source File: MobileActivity.java    From BluetoothCameraAndroid with MIT License 4 votes vote down vote up
@Override
public void enableBluetooth() {
    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, BT_ENABLE_REQUEST);
}
 
Example 19
Source File: BluetoothUtils.java    From AndroidBleManager with Apache License 2.0 4 votes vote down vote up
public void askUserToEnableBluetoothIfNeeded() {
    if (isBluetoothLeSupported() && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {
        final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        mActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}
 
Example 20
Source File: BluetoothUtils.java    From AndroidBleManager with Apache License 2.0 2 votes vote down vote up
/**
 * open system setting to open bluetooth
 * <p>Notification of the result of this activity is posted using the
 * {@link android.app.Activity#onActivityResult} callback. The
 * <code>resultCode</code>
 * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
 * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
 * has rejected the request or an error has occurred.
 * @param mActivity
 */
public static void openBlueToothSetting(Activity mActivity){
    final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    mActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}