前一段时间,做了给lightning耳机升级的软件,现在抽空将其记录下来。
因为该软件涉及到MFI认证,所以需要External Accessory Framework框架。External Accessory Framework提供了配件连接iOS设备的通道。开发者可以通过它来开发连接配件的app。
下面就开始简单了解下External Accessory Framework都有些什么吧:
-
它包含三个类:
EAAccessory:代表了一个单例的硬件配件对象。
EAAccessoryManager: 管理所有连接到iPhone的配件。
EASession:定义了iPhone app与外部配件的连接和通道。 -
EAAccessory类
EAAccessory类的属性:
1 )配件是否连接iPhone的标志
@property(nonatomic, readonly, getter=isConnected) BOOL connected
2 )连接设备的配件唯一标识
@property(nonatomic, readonly) NSUInteger connectionID
3 )配件的制造商
@property(nonatomic, readonly) NSString *manufacturer
4 )配件名称
@property(nonatomic, readonly) NSString *name
5 )配件型号
@property(nonatomic, readonly) NSString *modelNumber
6 )配件序列号
@property(nonatomic, readonly) NSString *serialNumber
7 )配件的固件版本号
@property(nonatomic, readonly) NSString *firmwareRevision
8 )配件的硬件版本号
@property(nonatomic, readonly) NSString *hardwareRevision -
EAAccessoryManager
//获取配件的所有信息
NSMutableArray *accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
比如:获取配件名称
NSString *name = [accessoryList[0] name]; -
EASession
// open a session with the accessory and set up the input and output stream on the default run loop
- (BOOL)openSession
{
[_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"); } return (_session != nil); }
// close the session with the accessory.
- (void)closeSession
{
[[_session inputStream] close];
[[_session inputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session inputStream] setDelegate:nil];
[[_session outputStream] close];
[[_session outputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[_session outputStream] setDelegate:nil];
[_session release];
_session = nil;
[_writeData release];
_writeData = nil;
[_readData release];
_readData = nil;
}
// asynchronous NSStream handleEvent method
- (void)stream:(NSStream *)aStream handleEvent: (NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventNone:
break;
case NSStreamEventOpenCompleted:
break;
case NSStreamEventHasBytesAvailable:
//TTSLOG(@"-----处理接收过来的数据包------");
[self _readData];
break;
case NSStreamEventHasSpaceAvailable:
[self _writeData];
break;
case NSStreamEventErrorOccurred:
//TTSLOG(@"----error-----");
break;
case NSStreamEventEndEncountered:
//TTSLOG(@"------end---------");
break;
default:
break;
}
}
-
项目过程
1 )导入External Accessory Framework框架
2 )在info.plist里面添加协议,Supported external accessory protocols,协议名根厂家协商,例如:com.xxx.protocol
3 ) 设置连接和断开连接通知
[[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]; - (void)_accessoryDidConnect:(NSNotification *)notification { } - (void)_accessoryDidDisconnect:(NSNotification *)notification { }
4 ) 配件连接成功后获取配件对应的协议,创建一个accessory controller对象。
//注意:_selectedAccessory和PROTOCOLSTRING2协议要对应,不然是不会成功的;。
EADSessionController *_eaSessionController = [EADSessionController sharedController];
[_eaSessionController setupControllerForAccessory:_selectedAccessory
withProtocolString:PROTOCOLSTRING2];
5 )打开session通讯
EADSessionController *sessionController = [EADSessionController sharedController];
_accessory = [sessionController accessory];
[sessionController openSession];
6 )最后就是向配件发送指令,相关指令就与硬件工程师协商。
对于External Accessory Framework框架就先了解到这里,如有不解或者错误的地方,欢迎指出。
网友评论