Android 断开蓝牙连接后振动并不总是有效
如果我的平板电脑通过蓝牙与手机断开连接,我会尝试让手机振动并显示通知。如果我通过平板电脑的蓝牙菜单断开平板电脑与手机的连接,无论是在手机屏幕打开还是关闭的情况下(我让手机保持睡眠半小时,然后将平板电脑与平板电脑的蓝牙菜单断开,它都可以工作)。如果我通过手机的蓝牙菜单断开手机与平板电脑的连接,它也可以工作。如果我拿着手机离开平板电脑,不让手机屏幕出现在屏幕上,它也会起作用 但是,如果我在手机屏幕关闭的情况下离开平板电脑,则不会发生振动。然而,当我在几分钟后检查我的手机时,通知确实会出现,并且通知带有正确的时间戳(因此通知不仅仅在我唤醒手机时出现)。我完全不知所措 这是我的相关代码:Android 断开蓝牙连接后振动并不总是有效,android,bluetooth,notifications,vibration,Android,Bluetooth,Notifications,Vibration,如果我的平板电脑通过蓝牙与手机断开连接,我会尝试让手机振动并显示通知。如果我通过平板电脑的蓝牙菜单断开平板电脑与手机的连接,无论是在手机屏幕打开还是关闭的情况下(我让手机保持睡眠半小时,然后将平板电脑与平板电脑的蓝牙菜单断开,它都可以工作)。如果我通过手机的蓝牙菜单断开手机与平板电脑的连接,它也可以工作。如果我拿着手机离开平板电脑,不让手机屏幕出现在屏幕上,它也会起作用 但是,如果我在手机屏幕关闭的情况下离开平板电脑,则不会发生振动。然而,当我在几分钟后检查我的手机时,通知确实会出现,并且通知带
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);
}
}
不要忘记为铃声和铃声启用振动设置
通知。进入设置->声音。检查“发出振动声”
也不相关,但使用生成器模式:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Device Disconnected")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(contentText)
.setVibrate(pattern)
.setDefaults(Notification.DEFAULT_VIBRATE);
只是好奇你是否用不同的安卓手机尝试过?是的,我也曾尝试过Oppo Find 7和Nexus 4(我的主要设备是OnePlus 1),这实际上是我最初的实现,它也有完全相同的问题