0

I'm developing an Android app that is thinked to run for long periods of time. At certain moments I need to communicate with some Blutooth devices (both 3.0 and BLE) so the idea is to switch ON the BlueTooth when I need and then turning it OFF for the rest of time.

My problem come when I turn the BT ON and an alert always pops us saying about the discoverability of my device, and I wish to remove this alert.

Searching around for a solution, I tried :

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivityForResult(discoverableIntent, GlobalConfiguration.REQUEST_DISCOVERABLE);

and

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Method method;
try {

   method = bluetoothAdapter.getClass().getMethod("setScanMode", int.class, int.class);
   method.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0);
}
catch(Exception e) {

   e.printStackTrace();
}

both unsuccessfully, standing that after BT state switch from ON -> OFF -> ON, the dialog about the discoverability comes up again.

There's a way to remove this alert forever, also after an ON/OFF of the BlueTooth?!

Thanks in advance

Lubron
  • 35
  • 2
  • 15
  • You shouldn't programmatically turn on or off Bluetooth. Remember that your app might not be the only one that uses Bluetooth on the device. That should be up to the user to do in the system settings. – Emil Dec 20 '19 at 21:54
  • The problem is exactly that: i'll let this app run for a long time (months) and the user won't be able to manage bluetooth settings, the app has no meaning to exists without BT, so I manage it following my needs; the decision to switch ON/OFF was taken for two reasons: 1) I don't think that leaving BlueTooth always ON is a best practice 2) Sometimes i had some weird behaviour leaving BT always ON, so I prefer to turn off and turn on again when needed My goal is to don't ask repeatedly to the user about BlueTooth enabling and discoverability, is that possible? Thanks :) – Lubron Dec 21 '19 at 07:41

0 Answers0