@@ -108,12 +108,12 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
108108 case let . iso7816( tag) :
109109 let apdu : NFCISO7816APDU ? = NFCISO7816APDU ( data: data)
110110 if apdu == nil {
111- result ( FlutterError ( code: " 400 " , message: " Command format error" , details: nil ) )
111+ result ( FlutterError ( code: " 400 " , message: " APDU format error" , details: nil ) )
112112 return
113113 }
114114 tag. sendCommand ( apdu: apdu!) { ( response: Data , sw1: UInt8 , sw2: UInt8 , error: Error ? ) in
115115 if let error = error {
116- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
116+ result ( FlutterError ( code: " 500 " , message: " Communication error with iso7816 tag " , details: error. localizedDescription) )
117117 } else {
118118 var response = response
119119 response. append ( contentsOf: [ sw1, sw2] )
@@ -133,7 +133,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
133133 // the first byte in data is length, and iOS will add it for us, so skip it
134134 tag. sendFeliCaCommand ( commandPacket: data. advanced ( by: 1 ) ) { ( response: Data , error: Error ? ) in
135135 if let error = error {
136- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
136+ result ( FlutterError ( code: " 500 " , message: " Communication error with felica tag " , details: error. localizedDescription) )
137137 } else {
138138 if req is String {
139139 result ( response. hexEncodedString ( ) )
@@ -145,7 +145,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
145145 case let . miFare( tag) :
146146 tag. sendMiFareCommand ( commandPacket: data) { ( response: Data , error: Error ? ) in
147147 if let error = error {
148- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
148+ result ( FlutterError ( code: " 500 " , message: " Communication error with mifare tag " , details: error. localizedDescription) )
149149 } else {
150150 if req is String {
151151 result ( response. hexEncodedString ( ) )
@@ -198,7 +198,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
198198 let extendedMode = ( arguments [ " iso15693ExtendedMode " ] as? Bool ) ?? false
199199 let handler = { ( dataBlock: Data , error: Error ? ) in
200200 if let error = error {
201- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
201+ result ( FlutterError ( code: " 500 " , message: " Cannot read iso15693 tag " , details: error. localizedDescription) )
202202 } else {
203203 result ( dataBlock)
204204 }
@@ -213,10 +213,10 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
213213 }
214214 else if case let . miFare( tag) = tag {
215215 let blockNumber = arguments [ " index " ] as! UInt8
216- let commandPacket = Data ( [ 0x30 , blockNumber] ) //0x30 is the MIFARE Classic Read Command.
216+ let commandPacket = Data ( [ 0x30 , blockNumber] ) // MiFARE Classic / Ultralight READ command
217217 tag. sendMiFareCommand ( commandPacket: commandPacket) { ( data, error) in
218218 if let error = error {
219- result ( FlutterError ( code: " 405 " , message: " Something is wrong " , details: nil ) )
219+ result ( FlutterError ( code: " 500 " , message: " Cannot read mifare tag " , details: error . localizedDescription ) )
220220 } else {
221221 result ( data)
222222 }
@@ -233,7 +233,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
233233 let extendedMode = ( arguments [ " iso15693ExtendedMode " ] as? Bool ) ?? false
234234 let handler = { ( error: Error ? ) in
235235 if let error = error {
236- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
236+ result ( FlutterError ( code: " 500 " , message: " Cannot write iso15693 tag " , details: error. localizedDescription) )
237237 } else {
238238 result ( nil )
239239 }
@@ -247,19 +247,24 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
247247 }
248248 }
249249 else if case let . miFare( tag) = tag {
250- let blockNumber = arguments [ " index " ] as! UInt8
251- let writeCommand = Data ( [ 0xA2 , blockNumber] ) + data //0xA2 is the MIFARE Classic Write Command to write single block.
252- tag. sendMiFareCommand ( commandPacket: writeCommand) { ( response, error) in
253- if let error = error {
254- result ( FlutterError ( code: " 500 " , message: " Communication error " , details: error. localizedDescription) )
255- }
256- else {
257- result ( nil )
258- }
259- }
250+ let blockNumber = arguments [ " index " ] as! UInt8
251+ let command = switch tag. mifareFamily {
252+ case . ultralight:
253+ 0xA2 // MiFARE Ultralight WRITE command
254+ default :
255+ 0xA0 // MiFARE Classic WRITE command
256+ } as UInt8
257+ let writeCommand = Data ( [ command, blockNumber] ) + data
258+ tag. sendMiFareCommand ( commandPacket: writeCommand) { ( response, error) in
259+ if let error = error {
260+ result ( FlutterError ( code: " 500 " , message: " Cannot write mifare tag " , details: error. localizedDescription) )
261+ } else {
262+ result ( nil )
263+ }
264+ }
260265 }
261266 else {
262- result ( FlutterError ( code: " 405 " , message: " writeBlock not supported on this type of card " , details: nil ) )
267+ result ( FlutterError ( code: " 405 " , message: " writeBlock not supported on this type of card " , details: nil ) )
263268 }
264269 } else if call. method == " readNDEF " {
265270 if tag != nil {
0 commit comments