简体   繁体   中英

Suppress Bluetooth discovery popup

I'm building a bluetooth app for android. I have a UI button, and when I press it, I want to engage bluetooth discovery mode for 30 seconds. The code I have right now does this perfectly, however it generates a popup to ask if I want to allow bluetooth discovery. This popup kinda ruins the flow of my application, so is there a way to bypass it?

Yes. You can check if it's enabled and enable it programmatically,

BluetoothManager btm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bta = btm.getAdapter();
if (!bta.isEnabled()) {
  boolean ret = bta.enable();
  if (!ret) {
    // enable failed!
  }
}

You need to have the BLUETOOTH_ADMIN permission,

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

No you can't bypass it. The dynamic permissions were introduced with Android 6. This allows the user to specify the permissions he want to grant to an App more precisely.

For an Android user it doesn't ruin the flow. It is normal for him. This is how Android works (and iOS, too btw).

See: https://developer.android.com/guide/topics/permissions/requesting.html

The BLUETOOTH_ADMIN is considered as a normal permission. But for a scan, you also need ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION . These are classified as dangerous permissions and that's why you need to ask the user once.

also see: BluetoothLeScanner.startScan()

There are several blog posts on how to handle permissions. James Montemagno released a plugin for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM