如何在以编程方式在Android上的手机上编写nfc标签之前设置新的密码

时间:2016-05-11 05:19:09

标签: android nfc

嗨我正在做一个应用程序,它是nfc阅读和编写器应用程序在这个应用程序我能够从标签读取数据并将自定义标签与数据相关联。我陷入了这就是如何设置所需的密码写入标签以防止覆盖,同时我如何要求用户输入密码以读取相同的nfc标签。请有人帮助我让我离开这个。

 adapter = NfcAdapter.getDefaultAdapter(this);
 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};





  btnWrite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mytag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
            //Toast.makeText(ctx,"taggggg"+mytag,Toast.LENGTH_LONG).show();
            if (mytag == null) {
                Toast.makeText(ctx, ctx.getString(R.string.error_detected), Toast.LENGTH_LONG).show();
            } else {


                    try {
                        Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG).show();
                        protect(mytag);
                    }catch (Exception e)
                    {


                }

         }
     });







         private void protect(Tag tag1) {
    MifareUltralight nfc=MifareUltralight.get(tag1);
    try {
        byte[] response = nfc.transceive(new byte[]{
                (byte) 0x1B, // PWD_AUTH
                pwd[0], pwd[1], pwd[2], pwd[3]
        });
        if ((response != null) && (response.length >= 2)) {
            byte[] pack = Arrays.copyOf(response, 2);
            // TODO: verify PACK to confirm that tag is authentic (not really,
            // but that whole PWD_AUTH/PACK authentication mechanism was not
            // really meant to bring much security, I hope; same with the
            // NTAG signature btw.)
        }
    }catch (Exception e)
    {

    }
    try {
        byte[] response = nfc.transceive(new byte[] {
                (byte) 0xA2, // WRITE
                (byte) 39,   // page address
                pwd[0], pwd[1], pwd[2], pwd[3]
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        byte[] response = nfc.transceive(new byte[] {
                (byte) 0xA2, // WRITE
                (byte) 40,   // page address
                pack[0], pack[1],   // bytes 0-1 are PACK value
                (byte) 0, (byte) 0  // other bytes are RFU and must be written as 0
        });
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        byte[] response = new byte[0];
        response = nfc.transceive(new byte[] {
                (byte) 0x30, // READ
                (byte) 38    // page address
        });

            try {
                if ((response != null) && (response.length >= 16)) {  // read always returns 4 pages
                    boolean prot = false;  // false = PWD_AUTH for write only, true = PWD_AUTH for read and write
                    int authlim = 0; // value between 0 and 7
                    response = nfc.transceive(new byte[]{
                            (byte) 0xA2, // WRITE
                            (byte) 38,   // page address
                            (byte) ((response[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
                            response[1], response[2], response[3]  // keep old value for bytes 1-3, you could also simply set them to 0 as they are currently RFU and must always be written as 0 (response[1], response[2], response[3] will contain 0 too as they contain the read RFU value)
                    });
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        write(txt.getText().toString(), mytag);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }

}

 How to achieve below output:

[new Password image to protect the nfc tag][1]


[1]: http://i.stack.imgur.com/uOEOD.png



In manifest file i included these permissions

  <uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.NFC" />
<action android:name="com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true"
    />



   <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

 Please help me

0 个答案:

没有答案