Android NFC transceive() using NFCF tech (Sony Felica)

坚强是说给别人听的谎言 提交于 2019-12-22 09:15:19

问题


I am attempting to connect my Android tablet to a device using NFC and retrieve data from the device.

What I've Tried

Sending commands as it explains in the nfc_device_detection_1.01.pdf (Chapter 4)

The android java doc for transceive() mentions the

"Applications must not append the SoD (length) or EoD (CRC) to the payload, it will be automatically calculated"

I have therefore tried with and without the CRC, with and without the packet data length but the documentation is not clear on if I should leave it blank or if i should just not include it.

Another approach I have taken is following the diagram in chapter 2.2 of format_sequence_guidelines_1.1.pdf (Sync Code followed by Request) but same results.

The problem

I do not know what command (bytes) to send as an argument into the transceive() method.**

Questions

Does anyone:

  • have an example of NFCF communication?
  • have more information on the protocol/command that should be used?
  • know if the NFC Tag contain the bytes I need for the command?

Code

transceive() throws an IO Exception "Tag was lost".

I believe this is because my command bytes are not correct (I have used a range of different commands).

Last Note (I have also tired putting the transceive() in a while loop and closed and connected the communication each time)

    String action = intent.getAction();

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

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

        NfcF nfcf = NfcF.get(tag);

        nfcf.connect();

        byte[] command = new byte[] { (byte) 0x00, (byte) 0x00};

        byte[] response =  nfcf.transceive(command);

     }

Please comment if any additional information is required for your answers. Thank you.


回答1:


Here is an example function of how to send a "raw" command, given the target device (tag) IDm, the FeliCa command byte and the payload:

byte[] rawCmd(NfcF nfcF, byte[] IDm, byte felicaCmd, byte[] payload) throws IOException {
    final int len = payload != null ? payload.length : 0;

    final byte[] cmd = new byte[10 + len];
    cmd[0] = (byte) (10 + len);
    cmd[1] = felicaCmd;
    System.arraycopy(IDm, 0, cmd, 2, IDm.length);

    if (payload != null) {
        System.arraycopy(payload, 0, cmd, 10, payload.length);
    }

    nfcF.transceive(cmd);
}



回答2:


I hope this might help someone in the future.

byte[] transceiveProtocol(NfcF nfcF, int systemCode, int requestCode, int timeSlot) {

byte d0 = 6;
byte d1 = 0;
byte d2 = (byte)(systemCode>> 8 & 0xFF);
byte d3 = (byte)(systemCode>> 0 & 0xFF);
byte d4 = (byte)(requestCode & 0xFF);
byte d5 = (byte)(timeSlot & 0xFF);

byte[] command = { d0, d1, d2, d3, d4, d5 };

return nfcF.transceive(command);
}


来源:https://stackoverflow.com/questions/17990618/android-nfc-transceive-using-nfcf-tech-sony-felica

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!