All long live.
The situation is as follows: Depending on certain conditions, an intent is sent from time to time with the BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE. Because the user must confirm the action, and the phone may be blocked / lay on the side, etc. I added an alert. The whole method is as follows:
private void ensureBluetoothDiscoverability(Context ctx) { if (bluetoothAdapter == null) return; if(bluetoothAdapter.getScanMode()!= BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Log.e(LOG_TAG, "Device was not in discoverable mode"); nm.cancelAll(); Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 30); discoverableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, discoverableIntent, PendingIntent.FLAG_CANCEL_CURRENT); Notification.Builder builder = new Notification.Builder(ctx) .setContentIntent(contentIntent) .setTicker("Требуется действие пользователя") .setContentTitle("Требуется действие пользователя") .setContentText("Необходимо подтвердить включение обнаружения устройства Bluetooth.") .setSmallIcon(R.mipmap.ic_launcher_main) .setWhen(System.currentTimeMillis()) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setAutoCancel(true); Notification notification = builder.getNotification(); nm.notify(NOTIFY_ID, notification); Log.d(LOG_TAG, "Device set up as discoverable"); } else{ Log.e(LOG_TAG, "Device already discoverable"); } } Actually, now a few questions:
- How to display notifications only on a locked device, in the active mode it is enough, for example, a sound alert, and when calling the method, check if a notification already exists?