Skip to content

Commit 5dd7295

Browse files
committed
Improve error handling
1 parent 4f63b84 commit 5dd7295

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

ohos/src/main/ets/components/plugin/FlutterNfcKitPlugin.ets

+17-8
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,31 @@ export default class FlutterNfcKitPlugin implements FlutterPlugin, MethodCallHan
141141
} else if (call.method == "transceive") {
142142
let tx: string = call.argument("data");
143143
hilog.info(0x0000, "FlutterNfcKitPlugin", "transceive: tx=%{public}s", JSON.stringify(tx));
144-
this.tag!.connect();
145-
this.tag!.transmit(fromHexString(tx)).then((rx) => {
146-
hilog.info(0x0000, "FlutterNfcKitPlugin", "transceive: rx=%{public}s", JSON.stringify(rx));
147-
result.success(toHexString(rx));
148-
}).catch((err: BusinessError) => {
149-
hilog.info(0x0000, "FlutterNfcKitPlugin", "Failed to transceive: err=%{public}s", JSON.stringify(err));
150-
result.error("500", "Transceive failed", err);
151-
});
144+
if (this.tag !== null) {
145+
this.tag!.connect();
146+
this.tag!.transmit(fromHexString(tx)).then((rx) => {
147+
hilog.info(0x0000, "FlutterNfcKitPlugin", "transceive: rx=%{public}s", JSON.stringify(rx));
148+
result.success(toHexString(rx));
149+
}).catch((err: BusinessError) => {
150+
hilog.info(0x0000, "FlutterNfcKitPlugin", "Failed to transceive: err=%{public}s", JSON.stringify(err));
151+
result.error("500", "Transceive failed", err);
152+
});
153+
} else {
154+
result.error("406", "No tag polled", null);
155+
}
152156
} else if (call.method == "finish") {
153157
// turn off reader mode
154158
tag.off("readerMode", elementName, (err: BusinessError, tagInfo: tag.TagInfo) => {});
159+
this.tag = null;
155160
result.success("");
156161
} else if (call.method == "setIosAlertMessage") {
157162
// do nothing, just for compatibility
158163
result.success("");
164+
} else if (call.method == "readNDEF") {
165+
// TODO
166+
result.success("[]");
159167
} else {
168+
hilog.info(0x0000, "FlutterNfcKitPlugin", "Not implemented method: %{public}s", call.method);
160169
result.notImplemented();
161170
}
162171
} catch (e) {

0 commit comments

Comments
 (0)