handleMethodCall method
Handles method calls over the MethodChannel of this plugin. Note: Check the "federated" architecture for a new way of doing this: https://flutter.dev/go/federated-plugins
Implementation
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'getNFCAvailability':
if (hasProperty(html.window.navigator, 'usb')) {
return 'available';
} else {
return 'not_supported';
}
case 'poll':
int timeout = call.arguments["timeout"];
bool probe = call.arguments["probeWebUSBMagic"];
return await WebUSB.poll(timeout, probe);
case 'transceive':
var data = call.arguments["data"];
if (!(data is Uint8List || data is String)) {
throw PlatformException(
code: "400",
message:
"Bad argument: data should be String or Uint8List, got $data");
}
// always pass hex string to [transceive]
var encodedData = data;
if (data is Uint8List) {
encodedData = hex.encode(data);
}
var encodedResp = await WebUSB.transceive(encodedData);
dynamic resp = encodedResp;
// return type should be the same as [data]
if (data is Uint8List) {
resp = Uint8List.fromList(hex.decode(encodedResp));
}
return resp;
case 'finish':
bool closeWebUSB = call.arguments["closeWebUSB"];
return await WebUSB.finish(closeWebUSB);
default:
throw PlatformException(
code: "501",
details:
"flutter_nfc_kit for web does not support \"${call.method}\"");
}
}