我正在使用POS设备刷卡。它的工作好。我使用了以下代码。
- (id) init
{
self = [super init];
if (self != nil)
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
EAAccessoryManager *accessoryMamaner = [EAAccessoryManager sharedAccessoryManager];
[accessoryMamaner registerForLocalNotifications];
[notificationCenter addObserver: self selector: @selector (accessoryDidConnect:) name: EAAccessoryDidConnectNotification object: nil];
[notificationCenter addObserver: self selector: @selector (accessoryDidDisconnect:) name: EAAccessoryDidDisconnectNotification object: nil];
NSArray *accessories = [accessoryMamaner connectedAccessories];
accessory = nil;
session = nil;
for (EAAccessory *obj in accessories)
{
if ([[obj protocolStrings] containsObject:@"com.XXXXX"] || [[obj protocolStrings] containsObject:@"com.YYYYYY"] )
{
accessory = obj;
break;
}
}
if (accessory)
{
session = [[EASession alloc] initWithAccessory:accessory forProtocol:@"com.dailysystems.DS247"];
if (!session) session = [[EASession alloc] initWithAccessory:accessory forProtocol:@"com.usaepay.ipos"];
if (session)
{
self.deviceConnected = YES;
[[session inputStream] setDelegate:self];
[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session inputStream] open];
[[session outputStream] setDelegate:self];
[[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session outputStream] open];
}
else
{
UIAlertView *accessoryInfo = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Hardware is not connected." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[accessoryInfo show]; [accessoryInfo release];
}
}
}
return self;
}
当我断开配件时,它给了我accessoryDidDisconnect,当我连接时,它给了我accessoryDidConnect, 但问题是在配件停止工作后它不响应命令。我试图再次释放alloc和alloc但没有用。 如果有人知道如何让配件再次工作,请告诉我。 提前谢谢。
答案 0 :(得分:0)
我不久前遇到了类似的问题。
首先,我希望你做的不仅仅是alloc。
其次,你是在发布会话并在accessoryDidDisconnect回调中设置为nil吗?我有一些问题,旧的会议阻碍了。
第三,在再次初始化配件之前,我稍稍有点运气。
我将设备设置放在另一个函数中,并从init:和accessoryDidConnect:
调用然后我放了一个NSTimer来延迟函数(或者甚至是sleep()调用会进行测试)以防万一 - 之后我似乎遇到的问题比之前少得多,所以也许通知会在之前发生设备真的准备好让会话重新开启吗?
您的通知回调测试会很棒!