NFC:蓝牙 LE OOB 配对 + Android 应用程序启动

NFC: Bluetooth LE OOB Pairing + Android application launch

我有一个包含 MCU + 蓝牙 LE 接口 + NFC 接口的物联网设备。

在 NFC 标签中,我编写了一条 NDEF 消息以使用带外 (OOB) 数据进行蓝牙配对。通过这种方式,如果有人将他的智能手机放在 NFC 标签上,他可以通过蓝牙 LE 自动连接到 IOT 设备。

我现在想知道如何在智能手机上启动与物联网设备通信以显示数据的应用程序。

在 NFC 标签中,我可以使用 AAR 记录,但我已经有了 NDEF 记录。将 2 条记录放入 NDEF 是可能的,但我怀疑它是否有效。我希望 Android 询问处理哪一个。

还有其他解决办法吗?

如果我只使用 NFC 标签中的蓝牙配对记录,配对就会完成,我应该找到启动我的应用程序的方法。在我的应用程序中,我可以使用后台服务和广播接收器,每次蓝牙连接时都会收到通知。我还没有尝试过,但我认为这可能是一种在连接蓝牙设备时唤醒我的应用程序的方法。它将检查设备具有哪个配置文件。如果它是预期的配置文件,它将继续并显示数据。我不知道这是不是个好主意...

我对蓝牙 LE 不熟悉。有没有办法告诉 Android 每次连接具有给定配置文件的蓝牙 LE 设备时都应该启动我的应用程序?

感谢您的建议

首先,当您的 NDEF 有两条记录时,android 将查看您的第一条记录。 你的问题的答案你可以通过编辑你的清单来做到这一点。 回答会有很大帮助。

每次连接蓝牙设备(具有特殊名称)时,我都能启动我的应用程序。 为此,我使用广播接收器监视事件 BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {

        int extraConnectionState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, -1);
        BluetoothDevice bluetoothDevice = intent.getExtras().getParcelable(BluetoothDevice.EXTRA_DEVICE);

        String deviceName = bluetoothDevice.getName();
        String deviceAddress = bluetoothDevice.getAddress();

        Log.d(TAG, "ACTION_CONNECTION_STATE_CHANGED Device: " + deviceName + ", Addr: " + deviceAddress + ", State: " + extraConnectionState);

        if (extraConnectionState == BluetoothAdapter.STATE_CONNECTED) {
            // We don't want to start our application everytimes a Bluetooth Device is connected.
            // Start our application only if the device has the expected name
            if (deviceName.equals(EXPECTED_DEVICE_NAME)) {
                // Start my application
                final Intent intent2 = new Intent(context, XXXXX.class);
                intent2.putExtra(EXTRAS_DEVICE_NAME, deviceName);
                intent2.putExtra(EXTRAS_DEVICE_ADDRESS, deviceAddress);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        
                startActivity(intent2);
            }
        }
    }
}

这有效,但前提是设备具有 HID 配置文件(我不知道为什么它不适用于其他配置文件)。