0
votes

I have an IOT device containing a MCU + a Bluetooth LE interface + an NFC Interface.

In the NFC Tag, I write a NDEF message to do Bluetooth Pairing with Out Of Band (OOB) data. By this way, if someone puts his smartphone on the NFC tag, he can get connected to the IOT device automatically through Bluetooth LE.

I’m now wondering how I could launch an application on the Smartphone that would communicate with the IOT device to display the data.

In the NFC Tag, I could use an AAR Record but I already have a NDEF Record. Putting the 2 records in the NDEF is possible but I doubt that it would work. I expect Android to ask which one to process.

Is there another solution?

If I use only the Bluetooth Pairing record in the NFC tag, the pairing will be done and I should find a way to launch my application. In my app, I could use a background Service and a Broadcast Receiver that would be notified every times the Bluetooth gets connected. I haven’t tried yet but I think that it could be a way to wake up my application when a Bluetooth device is connected. It would check which profile the device has. If it is the expected profile, it would go on and display the data. I don't know if that's a good idea...

I’m not familiar with Bluetooth LE. Is there a way to tell to Android that my application should be launched every times a Bluetooth LE device with a given profile is connected?

Thanks for your advices

2

2 Answers

0
votes

first of all the android will look to your first record when your NDEF has two records. an Answer for your question you can do that by editing your Manifest. This answer could help alot.

0
votes

I have been able to launch my application every times a Bluetooth Device (with a special name) is connected. To do so, I use a Broadcast Receiver monitoring the event 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);
            }
        }
    }
}

This is working but only if the device has a HID profile (I don't know why it doesn't work for other profiles).