Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Friday, July 17, 2009

iPhone: External Accessory Framework snippets

Friday, July 17, 2009   

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;
}
}
Enjoy. This is here merely as a kind of reference.

Labels: , ,

 
 Return to the main page
Comments:

There are currently 17 Comments:

Blogger Gangwar said...
“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 protocolstring 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..”
 
Blogger e.dolecki said...
July 27, 2009 7:19 AM
“The protocol string is going to have to come from the developer of the monitor.”
 
Anonymous AJ said...
August 8, 2009 3:14 AM
“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.”
 
Blogger hotsjf said...
August 9, 2009 3:57 PM
“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”
 
Blogger e.dolecki said...
August 9, 2009 9:57 PM
“hotsjf,

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.”
 
Blogger hotsjf said...
August 9, 2009 10:15 PM
“Thanks :)”
 
Anonymous Figo said...
October 20, 2009 2:16 AM
“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”
 
Blogger e.dolecki said...
October 20, 2009 7:56 AM
“Figo,

Read AJ's comment. Thanks.”
 
Anonymous Anonymous said...
November 2, 2009 3:04 AM
“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.

Thanks.
Figo”
 
Anonymous Figo said...
November 2, 2009 3:04 AM
“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.

Thanks.
Figo”
 
Blogger Panks said...
November 2, 2009 6:56 AM
“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”
 
Anonymous Anonymous said...
November 2, 2009 7:46 AM
“hi”
 
Blogger e.dolecki said...
November 4, 2009 10:38 PM
“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”
 
Anonymous orr said...
November 8, 2009 11:28 AM
“Hi All and Pankaj,

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.

Thanks,”
 
Blogger Panks said...
November 9, 2009 6:02 AM
“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...:)”
 
Blogger Enrico said...
November 26, 2009 1:04 PM
“@Panks
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.”
 
Anonymous Anonymous said...
March 12, 2010 7:24 PM
“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.”
 
 Leave a comment