Skip to content

Commit

Permalink
Merge pull request #84 from tidev/iosMifare
Browse files Browse the repository at this point in the history
fix(ios): fix sendMiFareCommand parameter and return value
  • Loading branch information
hansemannn authored Apr 18, 2024
2 parents c0a8939 + 8f5d994 commit 7f72b46
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
34 changes: 27 additions & 7 deletions ios/Classes/TiNfcMiFareUltralightTagTechnology.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,49 @@ - (TiBuffer *)historicalBytes
return historicalBytes;
}

NSString *getHexString(NSData *data)
{
NSUInteger capacity = data.length * 2;
NSMutableString *sbuf = [NSMutableString stringWithCapacity:capacity];
const unsigned char *buf = data.bytes;
NSInteger i;
for (i = 0; i < data.length; ++i) {
[sbuf appendFormat:@"%02lX", (unsigned long)buf[i]];
}
return sbuf;
}

NSData *arrayToData(NSArray *array)
{
Byte bytes[[array count]];
for (int i = 0; i < [array count]; i++) {
bytes[i] = [[array objectAtIndex:i] integerValue];
}
NSData *payload = [[NSData alloc] initWithBytes:bytes length:[array count]];
return payload;
}

- (void)sendMiFareCommand:(id)args
{
NSArray *commandData = [[args firstObject] valueForKey:@"data"];
unsigned int dataValue[commandData.count];
for (int i = 0; i < commandData.count; i++) {
dataValue[i] = [commandData[i] unsignedIntValue];
}
NSData *data = [[NSData alloc] initWithBytes:dataValue length:commandData.count];
NSData *data = arrayToData(commandData);
[[self.tagProxy asNFCMiFareTag] sendMiFareCommand:data
completionHandler:^(NSData *response, NSError *error) {
if (![self _hasListeners:@"didSendMiFareCommand"]) {
return;
}

TiBuffer *responseData = [[TiBuffer alloc] _initWithPageContext:[self pageContext]];
NSMutableData *responsevalue = [NSMutableData dataWithData:response];
[responseData setData:responsevalue];

[self fireEvent:@"didSendMiFareCommand"
withObject:@{
@"errorCode" : error != nil ? NUMINTEGER([error code]) : [NSNull null],
@"errorDescription" : error != nil ? [error localizedDescription] : [NSNull null],
@"errorDomain" : error != nil ? [error domain] : [NSNull null],
@"responseDataLength" : responseData.length

@"responseDataLength" : responseData.length,
@"hex" : getHexString(response)
}];
}];
}
Expand Down
2 changes: 1 addition & 1 deletion ios/example/NDEF/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var deviceWindow = Ti.UI.createWindow({
titleAttributes: { color: 'blue' }
});

var navDeviceWindow = Ti.UI.iOS.createNavigationWindow({
var navDeviceWindow = Ti.UI.createNavigationWindow({
window: deviceWindow
});

Expand Down
4 changes: 2 additions & 2 deletions ios/example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var deviceWindow = Ti.UI.createWindow({
title: 'Mifare Sample',
titleAttributes: { color: 'blue' }
});
var navDeviceWindow = Ti.UI.iOS.createNavigationWindow({
var navDeviceWindow = Ti.UI.createNavigationWindow({
window: deviceWindow
});

Expand All @@ -81,7 +81,7 @@ nfcAdapter.addEventListener('didDetectTags', function (e) {
Ti.API.info('didDetectTags with message' + (e.errorCode ? (' error code: ' + e.errorCode + ' error domain: ' + e.errorDomain + ' error description: ' + e.errorDescription) : ': Tag Detected'));
logs.push('didDetectTags with message' + (e.errorCode ? (' error code: ' + e.errorCode + ' error domain: ' + e.errorDomain + ' error description: ' + e.errorDescription) : ': Tag Detected'));
setData(logs);

var mifare = nfcAdapter.createTagTechMifareUltralight({'tag':e.tags[0]});
mifare.addEventListener('didConnectTag', function (e) {
Ti.API.info('didConnectTag with message' + (e.errorCode ? (' error code: ' + e.errorCode + ' error domain: ' + e.errorDomain + ' error description: ' + e.errorDescription) : ': MiFare Tag Connected'));
Expand Down
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 4.1.0
version: 4.1.1
apiversion: 2
architectures: arm64 x86_64
description: ti.nfc
Expand Down

0 comments on commit 7f72b46

Please sign in to comment.