【问题标题】:Vibrate after disconnect from bluetooth does not always work断开蓝牙后振动并不总是有效
【发布时间】:2015-04-24 00:43:28
【问题描述】:

我正在尝试让我的手机振动并在我的平板电脑通过蓝牙与手机断开连接时显示通知。如果我通过平板电脑的蓝牙菜单断开平板电脑与手机的连接,无论是在手机屏幕打开还是关闭时(我让手机保持睡眠半小时,然后将平板电脑与平板电脑的蓝牙菜单断开连接),它都可以工作它有效)。如果我通过手机的蓝牙菜单断开手机与平板电脑的连接,它也可以工作。如果我拿着手机离开平板电脑并保持手机屏幕关闭,它也可以工作。

但是,如果我在手机屏幕关闭的情况下离开平板电脑,则不会发生振动。但是,通知确实出现了,因为我在几分钟后检查了我的手机,并且通知带有正确的时间戳(所以通知不会只在我唤醒手机时出现)。我完全感到困惑。

这是我的相关代码:

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);
}
}

【问题讨论】:

  • 只是好奇您是否在其他 Android 手机上尝试过此操作?会不会是您的特定设置覆盖了您的应用程序?
  • 是的,我在 Oppo Find 7 和 Nexus 4 上也试过这个(我的主要设备是 OnePlus One)

标签: android bluetooth notifications vibration


【解决方案1】:

不要忘记启用铃声和振动设置 通知。转到设置-> 声音。勾选“发出振动声”。

https://stackoverflow.com/a/13905212/972311

同样不相关,但使用构建器模式:

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

【讨论】:

  • 这实际上是我最初的实现,它有完全相同的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多