Vibration after disconnecting from Bluetooth does not always work

I am trying to get my phone to vibrate and display a notification if my tablet disconnects from my phone over Bluetooth. It works if I disconnect my tablet from my phone through the Bluetooth tablet menu, both with the phone screen on and off (I slept with the phone for half an hour and then disconnected the tablet from the bluetooth tablet. And it works). It also works if I disconnect my phone from the tablet via the Bluetooth menu on the phone. It also works if I walk away from my tablet by holding my phone and capturing the phone screen.

However, if I leave the tablet with the phone screen turned off, vibration will not occur. However, the notification pops up when I check my phone after a few minutes, and the notification contains the correct timestamp (so the notification doesn't just pop up when my phone wakes up). I am completely confused.

Here's my relevant code:

public class BluetoothService extends Service {

public final BroadcastReceiver BluetoothScanner = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String trueAction = intent.getAction();
        if(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(trueAction)){

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
                dcNotify(deviceName);

                Toast.makeText(context, deviceName + " has disconnected", Toast.LENGTH_LONG).show();

        }

    }

};


public void dcNotify(String s) {
    Log.d("status", "commenced");
    int notificationId = 1;
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Device Disconnected")
                    .setSmallIcon(R.mipmap.ic_launcher);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    String contentText = "Disconnected from " + s;

        notificationBuilder.setContentText(contentText);

        long[] pattern = { 0, 100, 500, 100, 500, 100, 500};
        Notification notification = notificationBuilder.build();
        notification.vibrate = pattern;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(notificationId, notification);
}
}

      

+3


source to share


1 answer


Don't forget to enable vibration setting for ringtone and notification. Go to Settings → Sound. Check "Vibration Sound".

fooobar.com/questions/2217440 / ...



Also unrelated, but use the builder pattern:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setContentTitle("Device Disconnected")
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentText(contentText)
    .setVibrate(pattern)
    .setDefaults(Notification.DEFAULT_VIBRATE);

      

0


source







All Articles