情况如下:根据某些条件不时发送意图BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE。因为用户必须确认操作并且电话可能被暂停/搁置等等。我添加了警报。整个方法如下:
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");
}
}
实际上,现在有几个问题: 1.如何查看手机的状态?有必要仅在锁定的设备上撤销通知,在活动模式下就足够了,例如,音频警报。 2.如果我每隔五秒调用一次该方法,那么每次开发时都会看到通知。是否可以检查以及通知是否不会创建新通知? 如果有人告诉我如何在没有用户干预的情况下打开蓝牙发现,我将非常感激。我翻遍所有堆栈溢出,但没有任何效果。 :(