Advertisement
Guest User

AndroidBeam_S

a guest
Jan 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.content.pm.PackageManager;
  4. import android.net.Uri;
  5. import android.nfc.NfcAdapter;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.provider.Settings;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13.  
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import it.stesp.news.R;
  20.  
  21. public class NFC extends Activity {
  22.  
  23.     public static final String TAG_LOG = NFC.class.getName();
  24.  
  25.     NfcAdapter mNfcAdapter;
  26.     boolean mAndroidBeamAvailable = false;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.nfc_);
  32.  
  33.         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) { // NFC isn't available on the device
  34.             Log.i(TAG_LOG, "No NFC");
  35.         } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // Android Beam file transfer isn't supported
  36.             Log.i(TAG_LOG, "No Android Beam");
  37.             mAndroidBeamAvailable = false;
  38.         } else { // Android Beam file transfer is available, continue
  39.             mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  40.             mAndroidBeamAvailable = true;
  41.         }
  42.  
  43.         Button btn_nfc = (Button) findViewById(R.id.btn_nfc);
  44.         btn_nfc.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View v) {
  47.                 sendFile();
  48.             }
  49.         });
  50.     }
  51.  
  52.     private void sendFile() {
  53.         if (mAndroidBeamAvailable) {
  54.             // Check whether NFC is enabled on device
  55.             if (!mNfcAdapter.isEnabled()) {
  56.                 // NFC is disabled, show the settings UI
  57.                 // to enable NFC
  58.                 Toast.makeText(this, "Please enable NFC.",
  59.                         Toast.LENGTH_SHORT).show();
  60.                 startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
  61.             }
  62.             // Check whether Android Beam feature is enabled on device
  63.             else if (!mNfcAdapter.isNdefPushEnabled()) {
  64.                 // Android Beam is disabled, show the settings UI
  65.                 // to enable Android Beam
  66.                 Toast.makeText(this, "Please enable Android Beam.",
  67.                         Toast.LENGTH_SHORT).show();
  68.                 startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
  69.             } else {
  70.                 // NFC and Android Beam both are enabled
  71.                 File fileToTransfer = getDummyFile();
  72.  
  73.                 mNfcAdapter.setBeamPushUris(
  74.                         new Uri[]{Uri.fromFile(fileToTransfer)}, this);
  75.                 Log.i(TAG_LOG, "Uri push");
  76.             }
  77.         }
  78.     }
  79.  
  80.     private File getDummyFile() {
  81.         File dir = getExternalFilesDir(null);
  82.         if (!dir.exists()) dir.mkdirs();
  83.  
  84.         String filename = "dummy.txt";
  85.         File file = new File(dir, filename);
  86.  
  87.         try {
  88.             String text = "prova";
  89.  
  90.             FileOutputStream out = new FileOutputStream(file);
  91.             out.write(text.getBytes());
  92.         } catch (FileNotFoundException e) {
  93.             e.printStackTrace();
  94.         } catch (IOException e) {
  95.             e.printStackTrace();
  96.         }
  97.  
  98.         return file;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement