exchange static method

Future<String> exchange(
  1. String hex
)

Implementation

static Future<String> exchange(String hex) async {
  if (!_isConnected()) throw PlatformException(code: "406", message: "No Device");
  try {
    var data = NfcToolkit.fromHex(hex);
    await (_activeDevice!.callMethod('controlTransferOut'.toJS, _UsbTransferParams(requestType: 'vendor', recipient: 'interface', request: 0, value: 0, index: 1), data.toJS) as JSPromise).toDart;
    while (true) {
      var res = await (_activeDevice!.callMethod('controlTransferIn'.toJS, _UsbTransferParams(requestType: 'vendor', recipient: 'interface', request: 2, value: 0, index: 1), 1.toJS) as JSPromise<JSObject>).toDart;
      var status = _extractData(res)[0];
      if (status == 0) {
        break;
      }
      if (status == 1) {
        await Future.delayed(const Duration(microseconds: 100));
      } else {
        throw PlatformException(code: "500", message: "Transfer Error $status");
      }
    }
    var finalRes = await (_activeDevice!.callMethod('controlTransferIn'.toJS, _UsbTransferParams(requestType: 'vendor', recipient: 'interface', request: 1, value: 0, index: 1), 1500.toJS) as JSPromise<JSObject>).toDart;
    return NfcToolkit.toHex(_extractData(finalRes));
  } catch (e) {
    throw PlatformException(code: "500", message: "Exchange Failed", details: e);
  }
}