daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 99 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener
  2. {
  3.     override fun onCreate(savedInstanceState: Bundle?)
  4.     {
  5.         super.onCreate(savedInstanceState)
  6.         setContentView(R.layout.activity_main)
  7.        
  8.         val nfc = NfcAdapter.getDefaultAdapter(this)
  9.         nfc.setNdefPushMessageCallback(NfcHelper, this)
  10.         nfc.setOnNdefPushCompleteCallback(NfcHelper, this)
  11.     }
  12.  
  13.     override fun onNewIntent(intent: Intent)
  14.     {
  15.         // onResume gets called after this to handle the intent
  16.         setIntent(intent)
  17.         Log.d("nfcdev", "onNewIntent")
  18.     }
  19.  
  20.     override fun onResume()
  21.     {
  22.         super.onResume()
  23.  
  24.         Log.d("nfcdev", "onResume")
  25.  
  26.         // Check to see that the Activity started due to an Android Beam
  27.         if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action)
  28.         {
  29.             val rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
  30.             // only one message sent during the beam
  31.             val msg = rawMsgs[0] as NdefMessage
  32.             // record 0 contains the MIME type, record 1 is the AAR, if present
  33.             Toast.makeText(this, msg.records[0].payload.toString(), Toast.LENGTH_SHORT).show()
  34.         }
  35.     }
  36.  
  37.     private object NfcHelper : NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback
  38.     {
  39.         override fun onNdefPushComplete(event: NfcEvent?)
  40.         {
  41.             Log.d("nfcdev", "onNdefPushComplete")
  42.             //Toast.makeText(this, "onNdefPushComplete", Toast.LENGTH_SHORT).show()
  43.         }
  44.  
  45.         override fun createNdefMessage(event: NfcEvent?) = NdefMessage(arrayOf(
  46.                 NdefRecord.createExternal("pl.bonusplay.nfcdev", "nfcdev", "hello".toByteArray()),
  47.                 NdefRecord.createApplicationRecord("pl.bonusplay.nfcdev")))
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top