简体   繁体   中英

NFC-tag password protection with ST25 android SDK

I'm working with ST25 tags , more specifically type5 tags ST25DV64K. The ST25 SDK for android has some interesting examples and tutorials in it. I'm still struggling to use the code example provided at the end of the doc here concerning password-protected data , which consist in those lines:

byte[] password;
int passwordNumber = type5Tag.getPasswordNumber(area);
tag.presentPassword(passwordNumber, password);
NDEFMsg ndefMsg = myTag.readNdefMessage(area);

first problem, when I instanciate a type5 tag i don't see those methods for Type5Tag class:

import com.st.st25sdk.type5.*;
Type5Tag tag5;
tag5.??

Then, it is not clear how we are supposed to set up a password in the first place. I can't find any examples of setting up a password for a specific area, and removing it, and what is the format of the password that we can use? Is it possible to do this from android or do we have to use the ST25 app? Examples welcome. Thanks.

In the ST25 SDK Zip file, you will find an example of a basic Android App using the ST25 SDK Library (it is in \integration\android\examples\ST25AndroidDemoApp).

This example uses a class called “TagDiscovery” which is able to identify any ST25 Tag and to instantiate the right object. In your case, if you are only using ST25DV64K Tags, you will probably want to do something simple.

Here is what I suggest you: In your android activity, I expect that you have subscribed to receive a notification every time an NFC tag is taped (in “ST25AndroidDemoApp” example, look at enableForegroundDispatch() in onResume() function). To identify if the Intent corresponds to an “NFC Intent”, we check if the Intent's Action is ACTION_NDEF_DISCOVERED, or ACTION_TECH_DISCOVERED or ACTION_TAG_DISCOVERED.

When this is the case, we know that it is an NFC Intent. We can then call this to get the instance of androidTag:

Tag androidTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

This object represents the current NFC tag in Android.

We're now going to instantiate a ST25DVTag object.

import com.st.st25sdk.type5.st25dv.ST25DVTag;

…

AndroidReaderInterface readerInterface = AndroidReaderInterface.newInstance(androidTag);
byte[] uid = androidTag.getId();
uid = Helper.reverseByteArray(uid);
ST25DVTag myST25DVTag = new ST25DVTag(readerInterface, uid);

You now have an object called myST25DVTag that can be used to communicate with the tag!

For example, if you want to use the passwords:

byte[] password = new byte[];      // TODO: Fill the password
int passwordNumber = myST25DVTag.getPasswordNumber(area);
myST25DVTag.presentPassword(passwordNumber, password);
NDEFMsg ndefMsg = myST25DVTag.readNdefMessage(area);

Before doing that, you need to check which password is associated to this area. The tag has 3 passwords that can be freely assigned to any area. By default no password is set so you should set one. Here is an example where I use the password 2 for Area1:

int AREA1 = 1;
int passwordChosen = 2;
myST25DVTag.setPasswordNumber(AREA1, passwordChosen);
                           

I suggest that you install the ”ST25 NFC Tap” Android App from Google Play: https://play.google.com/store/apps/details?id=com.st.st25nfc&hl=fr&gl=US If you tap you ST25DV and go to the “Areas Security Status” menu, you will be able to see: the number of areas, which ones are protected by password for read and/or write, which password is used…etc

If you are interested, the source code of this application is available here: https://www.st.com/en/embedded-software/stsw-st25001.html

Tell me if something is unclear.

Disclaimer: I am on of the development team for the ST25 SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM