Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added null check on initialization to support background thread scenarios #61

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added logging in android implementation.
  • Loading branch information
AntSwift committed Oct 24, 2019
commit a2f5941711bac4c5304c0a6dca18c665f17a22f1
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
import java.nio.charset.Charset
import android.os.Looper
import java.io.IOException
import android.util.Log

const val PERMISSION_NFC = 1007

class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, EventChannel.StreamHandler, NfcAdapter.ReaderCallback {

private val logTag = "FlutterNfcReaderPlugin"
private val activity = registrar.activity()

private var nfcAdapter: NfcAdapter? = null
Expand All @@ -48,6 +49,7 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even
companion object {
@JvmStatic
fun registerWith(registrar: Registrar): Unit {
Log.v(logTag, registerWith)
val messenger = registrar.messenger()
val channel = MethodChannel(messenger, "flutter_nfc_reader")
val eventChannel = EventChannel(messenger, "it.matteocrippa.flutternfcreader.flutter_nfc_reader")
Expand All @@ -58,18 +60,22 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even
}

init {
Log.v(logTag, "init")
if(activity != null) {
Log.v(logTag, "have activity")
nfcManager = activity.getSystemService(Context.NFC_SERVICE) as? NfcManager
nfcAdapter = nfcManager?.defaultAdapter

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.v(logTag, "requesting permissions")
activity.requestPermissions(
arrayOf(Manifest.permission.NFC),
PERMISSION_NFC
)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.v(logTag, "enabling reader mode")
nfcAdapter?.enableReaderMode(activity, this, READER_FLAGS, null)
}
}
Expand Down Expand Up @@ -183,7 +189,9 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even


private fun writeTag() {
Log.v(logTag, "writing tag")
if (writeResult != null) {
Log.v(logTag, "have writeResult")
val nfcRecord = NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, kPath.toByteArray(), ByteArray(0), kWrite.toByteArray())
val nfcMessage = NdefMessage(arrayOf(nfcRecord))
writeMessageToTag(nfcMessage, tag)
Expand All @@ -199,6 +207,7 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even

private fun readTag() {
if (readResult != null) {
Log.v(logTag, "have readResult")
// convert tag to NDEF tag
val ndef = Ndef.get(tag)
ndef?.connect()
Expand All @@ -215,6 +224,7 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even
readResult = null
}
} else {
Log.v(logTag, "readResult null")
// convert tag to NDEF tag
val ndef = Ndef.get(tag)
ndef?.connect()
Expand All @@ -234,6 +244,7 @@ class FlutterNfcReaderPlugin(val registrar: Registrar) : MethodCallHandler, Even

// handle discovered NDEF Tags
override fun onTagDiscovered(tag: Tag?) {
Log.v(logTag, "tag discovered")
this.tag = tag
writeTag()
readTag()
Expand Down
9 changes: 4 additions & 5 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/henrysachs/development/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/henrysachs/projekte/flutter/flutter-nfc-reader/example"
export "FLUTTER_TARGET=/Users/henrysachs/projekte/flutter/flutter-nfc-reader/example/lib/main.dart"
export "FLUTTER_ROOT=/Users/anthonyswift/Library/Flutter"
export "FLUTTER_APPLICATION_PATH=/Users/anthonyswift/Documents/Source/flutter-nfc-reader/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/henrysachs/development/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/anthonyswift/Library/Flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "TRACK_WIDGET_CREATION=true"