通过在上面发送密码来保护我的 Nfc 标签



我正在做一个可以读取 nfc 然后处理内容消息的应用程序,但我只希望某些人能够读取标签。因此,在某种程度上,我希望用户扫描标签并在能够读取标签之前被提示输入密码。这可能吗? 对任何想法持开放态度。也可以只保留读取模式,并将写入模式更改为在标签上写入密码以保护它。

btnWrite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
try {
if(myTag ==null) {
Toast.makeText(context, ERROR_DETECTED, Toast.LENGTH_LONG).show();
} else {
write(message.getText().toString(), myTag);
Toast.makeText(context, WRITE_SUCCESS, Toast.LENGTH_LONG ).show();
}
} catch (IOException e) {
Toast.makeText(context, WRITE_ERROR, Toast.LENGTH_LONG ).show();
e.printStackTrace();
} catch (FormatException e) {
Toast.makeText(context, WRITE_ERROR, Toast.LENGTH_LONG ).show();
e.printStackTrace();
}
}
});
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
}
readFromIntent(getIntent());
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected };
}
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
//        String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
tvNFCContent.setText(text+"n( taille de la trame: " + text.length () +")");
}
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
}
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
String lang       = "en";
byte[] textBytes  = text.getBytes();
byte[] langBytes  = lang.getBytes("US-ASCII");
int    langLength = langBytes.length;
int    textLength = textBytes.length;
byte[] payload    = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1,              langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,  NdefRecord.RTD_TEXT,  new byte[0], payload);
return recordNFC;
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
readFromIntent(intent);
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
@Override
public void onPause(){
super.onPause();
WriteModeOff();
}
@Override
public void onResume(){
super.onResume();
WriteModeOn();
}
private void WriteModeOn(){
writeMode = true;
nfcAdapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}
private void WriteModeOff(){
writeMode = false;
nfcAdapter.disableForegroundDispatch(this);
}
}

您可以在 readFromIntent 中使用密码实现,将buildTagViews(msgs);替换为checkPasswordDialog(msgs)

private void readFromIntent(Intent intent) {
.....
.... checkPasswordDialog(msgs)  
...

在checkPasswordDialog(NdefMessage[] msgs)中,你检查密码,如果它是正确的,然后继续buildTagViews(msgs);否则,显示错误。

或者在buildTagViews(msgs);本身中,在您显示消息之前,密码会确定您将显示的内容,NFC 消息或错误(非授权用户)。

假设我编写了一个名为checkForPasswordDailog()的函数,该函数要求用户输入密码。如果用户输入的密码正确,则继续显示消息,否则显示和未经授权的用户消息。即

protected void checkForPasswordDialog(NdefMessage[] msgs){
/*create a dialog view with either both usename 
and password or just a password if every user uses the same 
password*/

MaterialDialog(this).show{
// add a title
View passwordDialogView = customView(R.layout.password_dialog_view)
//when the user submits their inputs
Textview userNameView = passwordDialogView.findViewById(R.id.username)
Textview passwordView = passwordDialogView.findViewById(R.id.password)
Button submitButton = passwordDialogView.findViewById(R.id.submit_button) 

submitButton.setOnClickListener{
if(verifyUser(username.text, password.text)){
buildTagViews(msgs)
cancel()
}else{
showUnauthorizedUserError()
cancel()
} 
}

}
protected boolean verifyUser(String username, String password){
//Verify user input 
}
protected void showUnauthorizedUseError(){
//Display your message here Snackbar/Dialog e.t.c
}

并且您的 xml 文件应包括

<TextView id = "@+id/username"/>
<TextView id = "@+id/password"/>
<Button id = "@+id/submit_button"/>

希望这有帮助

最新更新