Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package org.fossasia.magic_epaper_app

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity()
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.yourapp.nfc/settings"

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "openNFCSettings") {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} else {
result.notImplemented()
}
Comment on lines +17 to +24
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider error handling for the platform intent.

In case startActivity fails (for example, if NFC settings are not available), adding try/catch around the intent launch could help avoid unexpected crashes.

Suggested change
if (call.method == "openNFCSettings") {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} else {
result.notImplemented()
}
if (call.method == "openNFCSettings") {
try {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} catch (e: Exception) {
result.error("UNAVAILABLE", "NFC Settings not available", null)
}
} else {
result.notImplemented()
}

}
}
}
14 changes: 14 additions & 0 deletions lib/util/nfc_settings_launcher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class NFCSettingsLauncher {
static const platform = MethodChannel('com.yourapp.nfc/settings');

static Future<void> openNFCSettings() async {
try {
await platform.invokeMethod('openNFCSettings');
} on PlatformException catch (e) {
debugPrint("Failed to open NFC settings: ${e.message}");
}
}
}
22 changes: 20 additions & 2 deletions lib/util/protocol.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image/image.dart' as img;
import 'package:magic_epaper_app/util/epd/edp.dart';
import 'package:app_settings/app_settings.dart';
import 'package:magic_epaper_app/util/magic_epaper_firmware.dart';
import 'package:magic_epaper_app/util/nfc_settings_launcher.dart';

class Protocol {
final fw = MagicEpaperFirmware();
Expand Down Expand Up @@ -93,10 +97,24 @@ class Protocol {

void writeImages(img.Image image) async {
var availability = await FlutterNfcKit.nfcAvailability;
if (availability != NFCAvailability.available) {
throw "NFC is not available";
switch (availability) {
case NFCAvailability.available:
break;
case NFCAvailability.disabled:
Fluttertoast.showToast(msg: "NFC is disabled. Please enable it.");
if (Platform.isAndroid) {
await NFCSettingsLauncher.openNFCSettings();
} else if (Platform.isIOS) {
await AppSettings.openAppSettings();
}
return;
case NFCAvailability.not_supported:
Fluttertoast.showToast(msg: "This device does not support NFC.");
return;
}

Fluttertoast.showToast(
msg: "Bring your phone near to the Magic Epaper Hardware");
debugPrint("Bring your phone near to the Magic Epaper Hardware");
final tag = await FlutterNfcKit.poll(timeout: timeout);
debugPrint("Got a tag!");
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dependencies:
image: ^4.3.0
image_picker: ^1.1.2
image_cropper: ^9.0.0
app_settings: ^6.1.1
fluttertoast: ^8.2.12


dev_dependencies:
Expand Down
Loading