Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Merge PR #13
Browse files Browse the repository at this point in the history
  • Loading branch information
semlette committed Oct 19, 2019
2 parents dd5a71b + 4a64c5a commit 9a9e0e6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class NfcInFlutterPlugin implements MethodCallHandler,
private static final String LOG_TAG = "NfcInFlutterPlugin";

private final Activity activity;
private IsoDep iso_dep;
private NfcAdapter adapter;
private EventChannel.EventSink events;

Expand Down Expand Up @@ -128,11 +130,87 @@ public void onMethodCall(MethodCall call, Result result) {
result.error(e.code, e.message, e.details);
}
break;
case "startISODepReading":
Log.d("isodep", "start reading");
startReadingISODep(result);
break;
case "connectISODep":
connectIsoDep(result);
break;
case "closeISODep":
closeIsoDep(result);
break;
case "setTimeOutIsoDep":
setTimeOutIsoDep(call, result);
break;
case "transceiveIsoDep":
transceiveIsoDep(call, result);
break;
default:
result.notImplemented();
}
}

private void startReadingISODep( final Result result ) {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
if (adapter == null) return;
Bundle bundle = new Bundle();
int DEFAULT_READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V;
adapter.enableReaderMode(activity, new NfcAdapter.ReaderCallback() {
@Override
public void onTagDiscovered(Tag tag) {
Log.d("tag", tag.toString() );
IsoDep new_iso_dep = IsoDep.get(tag);
if ( new_iso_dep == null ) return;
iso_dep = new_iso_dep;
eventSuccess(result, null);
Log.d("tag", "event success" );
}
}, DEFAULT_READER_FLAGS, bundle);
}

private void connectIsoDep( final Result result ) {
try {
iso_dep.connect();
eventSuccess(result,null);
} catch (IOException e) {
eventError( result, e.getMessage(), e.getLocalizedMessage(), e.getStackTrace());
}
}

private void closeIsoDep( final Result result ) {
try {
iso_dep.close();
eventSuccess(result,null);
} catch (IOException e) {
eventError( result, e.getMessage(), e.getLocalizedMessage(), e.getStackTrace());
}
}

private void setTimeOutIsoDep( final MethodCall call, final Result result ) {
if ( !call.hasArgument("timeout") ) {
eventError( result,"timeout must be provided", null, null);
return;
}
iso_dep.setTimeout( (int)call.argument("timeout") );
eventSuccess( result, null);
}

private void transceiveIsoDep(final MethodCall call, final Result result ) {
if ( !call.hasArgument("data") ) {
eventError(result,"To transceive data must be provided", null, null);
return;
}
final byte[] data = call.argument("data");
try {
final byte[] response = iso_dep.transceive(data);
eventSuccess( result, response );
} catch (IOException e) {
eventError( result, e.getMessage(), e.getLocalizedMessage(), e.getStackTrace() );
}

}

private Boolean nfcIsEnabled() {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
if (adapter == null) return false;
Expand Down Expand Up @@ -607,4 +685,22 @@ public void run() {
};
mainThread.post(runnable);
}

private void eventSuccess(final Result result, final Object parameter) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
result.success(parameter);
}
});
}

private void eventError( final Result result, final String code, final String message, final Object details) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
result.error(code, message, details);
}
});
}
}
8 changes: 8 additions & 0 deletions lib/src/api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:core';
import 'dart:typed_data';

import 'package:flutter/services.dart';

Expand Down Expand Up @@ -217,6 +218,13 @@ class NFC {
assert(supported is bool);
return supported as bool;
}

static Future<void> readNFCIsoDep() async => await _channel.invokeMethod("startISODepReading");
static Future<void> setTimeOutIsoDep( final int timeout ) async => await _channel.invokeMethod("setTimeOutIsoDep", { "timeout": timeout });
static Future<void> connectISODep() async => await _channel.invokeMethod("connectISODep");
static Future<void> closeISODep() async => await _channel.invokeMethod("closeISODep");
static Future<Uint8List> transceiveIsoDep( Uint8List data ) async => await _channel.invokeMethod("transceiveIsoDep", { "data": data } );

}

/// NFCReaderMode is an interface for different reading modes
Expand Down

0 comments on commit 9a9e0e6

Please sign in to comment.