简体   繁体   中英

How to send a file using NFC in android on touch mode

I am working on a project in which I create a local .csv file and I want to sent this file using NFC.

So far, this is my code in my activity.

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      if (nfcAdapter == null) {          
         return;  // NFC not available on this device
      }
      Uri uri1;
      File root ;
      root= Environment.getExternalStorageDirectory();
      //assign the file path to the uri
      uri1=Uri.fromFile(new File(root, "myFile.csv"));
      nfcAdapter.setBeamPushUris(new Uri[] {uri1}, this);
}

Unfortunately I dont have another device to check it at the moment but I will do so in the near future. My question is how can I sent the file to the other device when the two devices come together? Usually android has Listeners for these kind of functionalities. Is there anything similar for the NFC?

I am testing the app in 4.2.2 on a Nexus 4

From another answer :

"For Android Beam, you need to provide URIs to the files with the data using setBeamPushUris() (or setBeamPushUrisCallback() if the data is not fixed). For S-Beam, I am not aware of any API that can be used. AFAICT, S-Beam only works with the built-in apps for pictures, video and music."

You can try this .

If your intention is to "solely" transfer the content (either large in size or not) via Android Beam, and in particular NFC, then I think none of the suggested methods would do what you want. These are helper methods to provide better user experience by enabling faster transfer via alternative mechanisms like Bluetooth or WiFi direct. Per Android development documentations, if the device does not support alternate transports such as Bluetooth or WiFI, calling these method does nothing. So, given the specific question you raised, you would better take other routes such as setNdefPushMessage() or setNdefPushMessageCallback() if using NFC is necessary.

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