开发者

EAAccessory Notification problem

开发者 https://www.devze.com 2022-12-31 15:58 出处:网络
I am using a POS device for card swipe. its working good. i have used following codes. - (id) init { self = [super init];

I am using a POS device for card swipe. its working good. i have used following codes.

- (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 (accessory开发者_运维问答DidDisconnect:)   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;
}

When i disconnect the accessory it gives me accessoryDidDisconnect and when i connect it gives me accessoryDidConnect, But Problem is after that accessory stop working it does not respond to command. i tried to release the alloc and alloc again but no use. Please tell me if some one have any idea how to get the accessory work again. Thanks in advance.


I was having a similar problem to you a little while ago.

Firstly, I hope you're doing alloc/init not just alloc.

Secondly, are your releasing the session and setting to nil in the accessoryDidDisconnect callback? I had some problems where the old session was getting in the way.

Thirdly, I had some luck with a slight delay before initialising the accessory again.

I put my device setup in another function and called that from init: and accessoryDidConnect:

Then I put a NSTimer to delay the function (or even a sleep() call would do for testing) just in case - I seemed to have a lot fewer problems after that than before, so maybe the notification is coming before the device is truly ready for the session to reopen?

The test of your notification callbacks would be great!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号