This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private var mNfcAdapter: NfcAdapter? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mNfcAdapter = NfcAdapter.getDefaultAdapter(this) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun writeMessageToTag(nfcMessage: NdefMessage, tag: Tag?): Boolean { | |
try { | |
val nDefTag = Ndef.get(tag) | |
nDefTag?.let { | |
it.connect() | |
if (it.maxSize < nfcMessage.toByteArray().size) { | |
//Message to large to write to NFC tag | |
return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun createNFCMessage(payload: String, intent: Intent?) : Boolean { | |
val pathPrefix = "peterjohnwelcome.com:nfcapp" | |
val nfcRecord = NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, pathPrefix.toByteArray(), ByteArray(0), payload.toByteArray()) | |
val nfcMessage = NdefMessage(arrayOf(nfcRecord)) | |
intent?.let { | |
val tag = it.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG) | |
return writeMessageToTag(nfcMessage, tag) | |
} | |
return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun disableNFCInForeground(nfcAdapter: NfcAdapter,activity: Activity) { | |
nfcAdapter.disableForegroundDispatch(activity) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T>enableNFCInForeground(nfcAdapter: NfcAdapter, activity: Activity, classType : Class<T>) { | |
val pendingIntent = PendingIntent.getActivity(activity, 0, | |
Intent(activity,classType).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0) | |
val nfcIntentFilter = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED) | |
val filters = arrayOf(nfcIntentFilter) | |
val TechLists = arrayOf(arrayOf(Ndef::class.java.name), arrayOf(NdefFormatable::class.java.name)) | |
nfcAdapter.enableForegroundDispatch(activity, pendingIntent, filters, TechLists) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun getNDefMessages(intent: Intent): Array<NdefMessage> { | |
val rawMessage = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) | |
rawMessage?.let { | |
return rawMessage.map { | |
it as NdefMessage | |
}.toTypedArray() | |
} | |
// Unknown tag type | |
val empty = byteArrayOf() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun retrieveNFCMessage(intent: Intent?): String { | |
intent?.let { | |
if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) { | |
val nDefMessages = getNDefMessages(intent) | |
nDefMessages[0].records?.let { | |
it.forEach { | |
it?.payload.let { | |
it?.let { | |
return String(it) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
import XCTest | |
struct Product { | |
var name: String | |
var price : Double | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Product { | |
var name: String | |
var price : Double | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ProductsRepository { | |
func fetchProducts() -> [Product] | |
} |
OlderNewer