External Accessory Framework snippets

On Friday, July 17, 2009 I posted on a previous iteration of my blog about the External Accessory Framework and code snippets. It has proven to be a well-commented upon post.

Just this morning I received notice in regards to a new comment, but the old blog doesn’t allow for commenting any more. So to keep the post active, I am reposting the original here for additional commenting if warranted. So here it is:

I do not have any immediate plans to play around with this and I don’t have any hardware or the protocol knowledge to do so with anything, but I was reading up on the  External Accessory Framework that comes with the OS3.0 SDK.

Some information, but I didn’t see any sample projects (for obvious reasons) or code snippets anywhere until just now. I’m not sure if this stuff is up to date or not, but here you go.

Add the ExternalAccessory.framework to your project. Make sure to add #import <ExternalAccessory/ExternalAccessory.h> to your .m file, and this is some example code:
- (EASession *)openSessionForProtocol:(NSString *)protocolString {
   NSArray *accessories = [[EAAccessoryManager sharedAccessoryManager]
      connectedAccessories];
   EAAccessory *accessory = nil;
   EASession *session = nil;

   for (EAAccessory *obj in accessories) {
       if ([[obj protocolStrings] containsObject:protocolString]){
           accessory = obj;
           break;
       }
   }

   if (accessory){
       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];
           [session autorelease];
       }
   }
   return session;
}

// Handle communications from the streams.
- (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent
{
   switch (streamEvent)
   {
       // case NSStreamHasBytesAvailable: this was incorrect in Apple documentation...
       case NSStreamEventHasBytesAvailable:
           // Process the incoming stream data.
           break;
        case NSStreamEventHasSpaceAvailable:
           // Send the next queued command.
           break;
        default:
           break;
   }
}
There you go – this is merely a kind of reference. I have pasted comments below (continue in the traditional fashion).
Hi,Thanks for a good post. I am new iphone developer. I want to connect third party device( Blood pressure monitoring Device) to iphone via bluetooth. So I need to implement External Accessory Framework. As per your post, its need protocol string But i am not to get this string and search the device. So Please give me some example to achieve this string. Thanks in advance…
The protocol string is going to have to come from the developer of the monitor
Yes thats correct. Moreover if the protocol string from your device is not one of those supported by Made for iPod / Work with iPhone accessories – this API wont let you work on this. In that case to make work, you need to sign up for MFi/WWi program which is all very complicated in terms of getting approval and be a part of the program. This is what I have found on the forums.
This is a great post. And actually this is the only piece of code about EAFramework that can be found on internet. I’m wondering if you got the EAFramework sample code from Apple, I’m doing some external accessory stuff, really having hard time….
Thanks
Yes, this code does come from Apple itself, but I am not sure about the date it was posted online… so I can’t speak to how accurate it is in conjunction with revs of the iPhone OS. Maybe I’ll have more information in the future.
Hi dolecki: It’s so glad to find your commons about External Accessory Framework. Now, we want to support a feature to detect new dock connecter device. Do you know any information about this? Thanks, Figo
Figo, Read AJ’s comment. Thanks.
Thanks dolecki. Same thanks to AJ’s comment. In fact, I’m not so clear about the dock device step.
  • Case 1: When dock connecter device is docked, our app is run. In this case, our App should be able to communicate with dock station.
  • Case 2: When dock connecter device is docked, our app is not running. In this case, is there any way to detect new dock connecter device, and automatically launch our App?
  • Case 3: When dock connecter device is docked, our app is not running. Sometimes later, we launch out App. In this case, it’s the same case as case 1 for our App?

Do you have any information? Thanks in advance.

Hello All, I’m using following piece of code to learn External accessory framework
EAAccessoryManager *accessoryMamaner = [EAAccessoryManager sharedAccessoryManager];
[accessoryMamaner registerForLocalNotifications];
availableAccessories = accessoryMamaner.connectedAccessories;
eSession = [self openSessionForProtocol:@"TCP"];
[accessoryMamaner isAccessibilityElement];

to detect the accessory, using following:

- (EASession *)openSessionForProtocol:(NSString *)protocolString {

NSArray *accessories = [[EAAccessoryManager sharedAccessoryManager]
connectedAccessories];

EAAccessory *accessory = nil;
EASession *session = nil;
NSMutableString *acc = [[NSMutableString alloc] init];

for (EAAccessory *obj in accessories) {
obj.delegate = self;
[acc appendString:[NSString stringWithFormat:@"Name: %@\nManufacturer: %@\nModel Number: %@ \n", [obj name], [obj manufacturer],
if ([[obj protocolStrings] containsObject:protocolString]){
accessory = obj;
break;
}
}

if([accessories count] > 0) {
NSLog(@"accessory is detected ");
} else {
NSLog(@"No accessory found");
}

if (accessory){

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];
[session autorelease];
}
}
return session;
}

While running above code in iPhone simulator, it displays the two connected accessories but running same code in iPod touch, no accessory is listed. BTW external accessory I’m using is iPod USB cord… Please suggest me what I should do? I am I doing any thing wrong? Thanks,Pankaj

AJ, Your best bet would be to ping the Apple iPhone Developer Forum(s). I haven’t actually communicated with hardware using this (yet). Case 1: Yes. Case 2: Yes, that’s possible to launch the app – and if it’s not installed, to go directly to the app in the app store on the device. Case 3: Yes.
Did you manage to view devices via BT when running your code on the device? I am stuck on the same exact issue. Any help will be greatly appreciated.
Nope, even via Bluetooth I’m not able to view any devices, Even Apple saying that you can communicate with external accessories though either via USB or through Bluetooth using external accessory framework. The docs for the framework suggest that devices will somehow register their protocols with the device, which is what will make them available to the accessory manager. Unfortunately, I can’t find any details on what the hardware needs to do to register itself so as which protocol needs to passed while initiating the accessory manager! Apple say “Participate in the Made for iPod licensing program http://developer.apple.com/ipod/ But not found any sample code any where…
I try the same currently. First to the 2 accessories in the simulator. These are just dummy accessories, named iObject and iSomething. Your iPhone doesn’t detect any accessories, because the USB cord isn’t that kind of accessory Apply supposes. A correct iPhone Accessory would start an authentification procedure after pluged in, telling its name, manufacturer, protocol names and proves that it’s officially allowed by Apple. For this it uses the Apple Accessory Protocol and probably an authentification chip. After that the device will be in the list of the EAAccessoryManager. I didn’t find any informations about this procedure till now. That probably because of the NDA you have to agree when you attend the “Make for iPod and iPhone Program”. But if you find anything please tell me.
Our comapany is a MFI member, We produce Bluetooth product working with iPhone OS application. If you are interesed in our product, please send Email to gtb_cr@yahoo.com.
awesome job! that’s everything i needed!
Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *


− 6 = zero

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>