2

The situation is as follows: Depending on certain conditions from time to time sent with intent BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE. Because the user must confirm the action and the phone may be suspended / lying aside 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: 1. How to check the status of your phone? It is necessary to withdraw the notice only on a locked device, in the active mode is sufficient, for example, an audio alert. 2. If I call the method every five seconds, then I see a notification every time when developing it. Whether it is possible to check up and if the notification does not create a new one? 3. I would be immensely grateful if someone tell me a way to turn on the Bluetooth discovery without user intervention. I rummaged all stack overflow, but nothing has worked. :(

Jhaman Das
  • 1,094
  • 13
  • 28
madboy
  • 21
  • 3

0 Answers0