Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

16
Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com

Transcript of Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Page 1: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Intro to the External Accessory Framework

Andrew Craze@AndrewCr

acraze at dxysolutions.com

Page 2: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Agenda•What is the EA Framework useful for?

•What hardware can I talk to?

•How do I use the EA Framework?

•Why/how do I get more info from Apple under the MFi NDA?

Page 3: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

What can I do with EAAccessory?

•Application [not accessory] -side code

•Enumerate connected accessories

•Open a serial session with an accessory

•Communicate via streams and get notifications

Page 4: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

What can’t I do with EAAccessory?

•Pair or make the connection with a BlueTooth device

•Use (any) BlueTooth profiles

Page 5: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Hardware Options•Consumer-ready (pre-built cables)

Like RedPark’s

•Build-your-own, with 3rd-party firmware

Like BlueGiga’s

•Build-your-own, with DIY firmware

Join the MFi program for Docs

Page 6: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Easier Hardware(Examples, probably not the only options)

RedPack’s Serial Cable BlueGiga’s iWrap Firmware

Page 7: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Application ChecklistLink with ExternalAccessory.framework

Add protocol(s) to info.plist

Use EAAccessoryManager to find your device

Add a delegate (EAAccessoryDelegate) to your accessory’s object

Connect with your chosen protocol

Manage the I/O stream data

Page 8: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Link with ExternalAccessory.framework

Page 9: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Add protocol(s) to info.plist

Page 10: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Use EAAccessoryManager to find your device

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil]; [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];

_accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];

Page 11: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Add delegate to your accessory’s object, connect to streams

[_accessory setDelegate:self]; _session = [[EASession alloc] initWithAccessory:_accessory forProtocol:_protocolString];

if (_session) { [[_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 { NSLog(@"creating session failed"); }

Page 12: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Manage the I/O stream data, part 1

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ switch (eventCode) { case NSStreamEventNone: break; case NSStreamEventOpenCompleted: break; case NSStreamEventHasBytesAvailable: [self _readData]; break; case NSStreamEventHasSpaceAvailable: [self _writeData]; break; case NSStreamEventErrorOccurred: break; case NSStreamEventEndEncountered: break; default: break; }}

Page 13: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Manage the I/O stream data, part 2

- (void)_readData {#define EAD_INPUT_BUFFER_SIZE 128 uint8_t buf[EAD_INPUT_BUFFER_SIZE]; while ([[_session inputStream] hasBytesAvailable]) { NSInteger bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE]; if (_readData == nil) { _readData = [[NSMutableData alloc] init]; } [_readData appendBytes:(void *)buf length:bytesRead]; //NSLog(@"read %d bytes from input stream", bytesRead); }

[[NSNotificationCenter defaultCenter] postNotificationName:EADSessionDataReceivedNotification object:self userInfo:nil];}

- (NSData *)readData:(NSUInteger)bytesToRead{ NSData *data = nil; if ([_readData length] >= bytesToRead) { NSRange range = NSMakeRange(0, bytesToRead); data = [_readData subdataWithRange:range]; [_readData replaceBytesInRange:range withBytes:NULL length:0]; } return data;}

Page 14: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

The MFi Program•Separate NDA, over-and-above the

iOS developer program

•Free, but you must be a bona-fide company (and prove it!)

•Plan on a month to get fully-approved

•Docs on everything you need to know for accessory-side development

Page 15: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Handy reference links

(No endorsement expressed or implied. So there.)

• External Accessory Framework Reference (from Apple)http://developer.apple.com/library/ios/#documentation/ExternalAccessory/Reference/ExternalAccessoryFrameworkReference/_index.html#//apple_ref/doc/uid/TP40008235

• Apple’s EADemo sample codehttp://developer.apple.com/library/ios/#samplecode/EADemo/Introduction/Intro.html

• Apple MFi pagehttps://developer.apple.com/programs/mfi/

• Redpark Serial Cablehttp://redpark.com/c2db9.html

• BlueGiga’s iWrap firmware (for their BlueTooth modules)http://www.bluegiga.com/iWRAP_module_firmware

Page 16: Intro to the External Accessory Framework Andrew Craze @AndrewCr acraze at dxysolutions.com.

Questions, Maybe Answers

Andrew Craze

@AndrewCr

http://blog.andrewcraze.com

acraze at dxysolutions.com