前一段時(shí)間谚赎,做了給lightning耳機(jī)升級(jí)的軟件舔腾,現(xiàn)在抽空將其記錄下來沪摄。
因?yàn)樵撥浖婕暗組FI認(rèn)證躯嫉,所以需要External Accessory Framework框架。External Accessory Framework提供了配件連接iOS設(shè)備的通道杨拐。開發(fā)者可以通過它來開發(fā)連接配件的app和敬。
下面就開始簡(jiǎn)單了解下External Accessory Framework都有些什么吧:
它包含三個(gè)類:
EAAccessory:代表了一個(gè)單例的硬件配件對(duì)象。
EAAccessoryManager: 管理所有連接到iPhone的配件戏阅。
EASession:定義了iPhone app與外部配件的連接和通道昼弟。EAAccessory類
EAAccessory類的屬性:
1 )配件是否連接iPhone的標(biāo)志
@property(nonatomic, readonly, getter=isConnected) BOOL connected
2 )連接設(shè)備的配件唯一標(biāo)識(shí)
@property(nonatomic, readonly) NSUInteger connectionID
3 )配件的制造商
@property(nonatomic, readonly) NSString *manufacturer
4 )配件名稱
@property(nonatomic, readonly) NSString *name
5 )配件型號(hào)
@property(nonatomic, readonly) NSString *modelNumber
6 )配件序列號(hào)
@property(nonatomic, readonly) NSString *serialNumber
7 )配件的固件版本號(hào)
@property(nonatomic, readonly) NSString *firmwareRevision
8 )配件的硬件版本號(hào)
@property(nonatomic, readonly) NSString *hardwareRevisionEAAccessoryManager
//獲取配件的所有信息
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(@"-----處理接收過來的數(shù)據(jù)包------");
[self _readData];
break;
case NSStreamEventHasSpaceAvailable:
[self _writeData];
break;
case NSStreamEventErrorOccurred:
//TTSLOG(@"----error-----");
break;
case NSStreamEventEndEncountered:
//TTSLOG(@"------end---------");
break;
default:
break;
}
}
-
項(xiàng)目過程
1 )導(dǎo)入External Accessory Framework框架
2 )在info.plist里面添加協(xié)議,Supported external accessory protocols奕筐,協(xié)議名根廠家協(xié)商舱痘,例如:com.xxx.protocol
3 ) 設(shè)置連接和斷開連接通知
[[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 ) 配件連接成功后獲取配件對(duì)應(yīng)的協(xié)議,創(chuàng)建一個(gè)accessory controller對(duì)象离赫。
//注意:_selectedAccessory和PROTOCOLSTRING2協(xié)議要對(duì)應(yīng)芭逝,不然是不會(huì)成功的;渊胸。
EADSessionController *_eaSessionController = [EADSessionController sharedController];
[_eaSessionController setupControllerForAccessory:_selectedAccessory
withProtocolString:PROTOCOLSTRING2];
5 )打開session通訊
EADSessionController *sessionController = [EADSessionController sharedController];
_accessory = [sessionController accessory];
[sessionController openSession];
6 )最后就是向配件發(fā)送指令旬盯,相關(guān)指令就與硬件工程師協(xié)商。
對(duì)于External Accessory Framework框架就先了解到這里翎猛,如有不解或者錯(cuò)誤的地方胖翰,歡迎指出。