下面列出了android.bluetooth.BluetoothAdapter#ACTION_REQUEST_ENABLE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
}
@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);
}
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;
}
/**
* 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);
}
}
@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);
}
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);
}
}
@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);
}
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.");
}
}
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);
}
}
/**
* 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();
}
@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);
}
}
@Override
public void onRefresh() {
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
scanDevice();
} else {
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
@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();
}
/**
* 打开蓝牙(默认模式--带系统弹出框)
*
* @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);
}
}
/**
* 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);
}
}
}
@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);
}
/**
* Starts activity to enable bluetooth.
*/
private void enableBluetooth() {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, RequestCodes.REQUEST_ENABLE_BT);
}
@Override
public void enableBluetooth() {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, BT_ENABLE_REQUEST);
}
public void askUserToEnableBluetoothIfNeeded() {
if (isBluetoothLeSupported() && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {
final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
/**
* 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);
}