Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to the External Accessory Framework

Similar presentations


Presentation on theme: "Intro to the External Accessory Framework"— Presentation transcript:

1 Intro to the External Accessory Framework
Andrew Craze @AndrewCr acraze at dxysolutions.com I’m Andy Craze. I manage the iOS Practice at DXY Solutions. Marketing, marketing, blah, blah, blah. This is my company car-in-a-box. I’ll have contact Info at the end as well. Please as questions as we go.

2 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?

3 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 Sessions will use a “protocol”, identified with a reverse-DNS name. This must be specified in your info.plist.

4 What can’t I do with EAAccessory?
Pair or make the connection with a BlueTooth device Use (any) BlueTooth profiles The user must do the pairing and connecting via the settings panel. (For non-BlueTooth, they must connect the device, of course.) iOS 5.0 allowed launching the BlueTooth settings panel, but it went away in 5.1 Your device may be able to launch (or even prompt download of) your App. See MFi for details. The BlueTooth profiles supported by iOS are handled by the OS itself (http://support.apple.com/kb/HT3647). Communication with an accessory is something different, (and not SPP!)

5 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 Red Park’s cable is a 30-pin to DB-9 serial connector, ready to roll for hobbyists. BlueGiga makes Bluetooth modules you can add to other hardware you build and talk to via a serial interface. iWrap is their built-in firmware that handles all the Apple-connection magic. We have a client using this for a iOS and Android connectible device

6 Easier Hardware (Examples, probably not the only options)
RedPack’s Serial Cable BlueGiga’s iWrap Firmware There’s surprisingly little out there for hobbyists, though.

7 Application Checklist
Link 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 Screenshots to follow

8 Link with ExternalAccessory.framework

9 Add protocol(s) to info.plist
Your attempt to open a session will fail otherwise Add protocol(s) to info.plist

10 Use EAAccessoryManager to find your device
[[NSNotificationCenter defaultCenter] addObserver:self name:EAAccessoryDidConnectNotification object:nil]; name:EAAccessoryDidDisconnectNotification [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]]; Code is taken from Apple’s EADemo Application The top section shows how to register for connect/disconnect notifications. If your App is backgrounded, these notifications will be queued. Note that if you connect and then immediately switch to your App, the notification will take a moment The bottom line shows how to get the currently connected accessories. This array will contain objects of EAAccessory type. EAAccessory objects have properties like name, manufacturer, model number, serial number, version numbers, etc. Use EAAccessoryManager to find your device

11 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] [[_session outputStream] open]; } else session failed"); Code is taken from Apple’s EADemo Application As the Accessory delegate you’ll get disconnection notifications (that’s it!) The EAAccessory object has an array property with the IDStrings of the accessory’s supported protocols As the stream delegates, you’ll get low-level read/write notifications Add delegate to your accessory’s object, connect to streams

12 Manage the I/O stream data, part 1
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { switch (eventCode) { case NSStreamEventNone: break; case NSStreamEventOpenCompleted: case NSStreamEventHasBytesAvailable: [self _readData]; case NSStreamEventHasSpaceAvailable: [self _writeData]; case NSStreamEventErrorOccurred: case NSStreamEventEndEncountered: default: } Code is taken from Apple’s EADemo Application As the stream delegates (NSStreamDelegate), you’ll get low-level read/write notifications via this one method Manage the I/O stream data, part 1

13 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]; %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; Code is taken from Apple’s EADemo Application This is Apple’s example where they notify a high-level read routine that data is available, then that routine removes the bytes. In real life, we use a different approach where we call a parsing routine that detects if a complete message is sent, then remove that. Manage the I/O stream data, part 2

14 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 Link to program entry page on reference slide Docs include Firmware software requirements Hardware Spec requirements Chip suppliers, etc. Apple takes an interest in your product, your packaging, your marketing, etc. This is a certification program to get the MFi logo(s)

15 (No endorsement expressed or implied. So there.)
Handy reference links (No endorsement expressed or implied. So there.) External Accessory Framework Reference (from Apple) ef/doc/uid/TP Apple’s EADemo sample code Apple MFi page https://developer.apple.com/programs/mfi/ Redpark Serial Cable BlueGiga’s iWrap firmware (for their BlueTooth modules)

16 Questions, Maybe Answers
Andrew acraze at dxysolutions.com


Download ppt "Intro to the External Accessory Framework"

Similar presentations


Ads by Google