iOS开发之ExternalAccessory框架的应用
ExternalAccessory框架用来对外设进行管理,iOS外设通常是通过MFI认证的外部设备,可以通过蓝牙进行连接,也可以使用lighting端口进行连接。
EAAccessoryManager类用来对外设进行管理,其中属性方法如下:
1 2 3 4 5 6 7 8 9 10 11 12
| @interface EAAccessoryManager : NSObject
+ (EAAccessoryManager *)sharedAccessoryManager;
- (void)showBluetoothAccessoryPickerWithNameFilter:(nullable NSPredicate *)predicate completion:(nullable EABluetoothAccessoryPickerCompletion)completion;
- (void)registerForLocalNotifications;
- (void)unregisterForLocalNotifications;
@property (nonatomic, readonly) NSArray<EAAccessory *> *connectedAccessories; @end
|
如果注册了本地通知,则可以监听下面两个通知:
1 2
| EAAccessoryDidConnectNotification EAAccessoryDidDisconnectNotification
|
EAAccessory是外设对象,其中定义了外设的相关信息,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @interface EAAccessory : NSObject
@property(nonatomic, readonly, getter=isConnected) BOOL connected;
@property(nonatomic, readonly) NSUInteger connectionID;
@property(nonatomic, readonly) NSString *manufacturer;
@property(nonatomic, readonly) NSString *name;
@property(nonatomic, readonly) NSString *modelNumber;
@property(nonatomic, readonly) NSString *serialNumber;
@property(nonatomic, readonly) NSString *firmwareRevision;
@property(nonatomic, readonly) NSString *hardwareRevision;
@property(nonatomic, readonly) NSString *dockType;
@property(nonatomic, readonly) NSArray<NSString *> *protocolStrings;
@property(nonatomic, assign, nullable) id<EAAccessoryDelegate> delegate; @end
@protocol EAAccessoryDelegate <NSObject> @optional
- (void)accessoryDidDisconnect:(EAAccessory *)accessory; @end
|
需要注意,与外设进行通讯需要指定对应的协议,首先,需要在iOS应用的info.plist文件中添加如下键来指定此应用要交互的外设协议:
具体的外设协议需要查看外设的说明文档。
EASession类用来进行外设交互,解析如下:
1 2 3 4 5 6 7 8 9 10 11 12
| @interface EASession : NSObject
- (nullable instancetype)initWithAccessory:(EAAccessory *)accessory forProtocol:(NSString *)protocolString;
@property (nonatomic, readonly, nullable) EAAccessory *accessory;
@property (nonatomic, readonly, nullable) NSString *protocolString;
@property (nonatomic, readonly, nullable) NSInputStream *inputStream;
@property (nonatomic, readonly, nullable) NSOutputStream *outputStream; @end
|
EAWiFiUnconfiguredAccessoryBrowser类用来浏览未配置的WIFI外设:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| @interface EAWiFiUnconfiguredAccessoryBrowser : NSObject
- (instancetype)initWithDelegate:(nullable id<EAWiFiUnconfiguredAccessoryBrowserDelegate>)delegate queue:(nullable dispatch_queue_t)queue;
@property (weak, nonatomic, nullable) id<EAWiFiUnconfiguredAccessoryBrowserDelegate> delegate;
@property (readonly, copy, atomic) NSSet<EAWiFiUnconfiguredAccessory *> *unconfiguredAccessories;
- (void)startSearchingForUnconfiguredAccessoriesMatchingPredicate:(nullable NSPredicate *)predicate;
- (void)stopSearchingForUnconfiguredAccessories;
- (void)configureAccessory:(EAWiFiUnconfiguredAccessory *)accessory withConfigurationUIOnViewController:(UIViewController *)viewController; @end
@protocol EAWiFiUnconfiguredAccessoryBrowserDelegate <NSObject>
- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didUpdateState:(EAWiFiUnconfiguredAccessoryBrowserState)state;
- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didFindUnconfiguredAccessories:(NSSet<EAWiFiUnconfiguredAccessory *> *)accessories;
- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didRemoveUnconfiguredAccessories:(NSSet<EAWiFiUnconfiguredAccessory *> *)accessories;
- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didFinishConfiguringAccessory:(EAWiFiUnconfiguredAccessory *)accessory withStatus:(EAWiFiUnconfiguredAccessoryConfigurationStatus)status;
@end
|
EAWiFiUnconfiguredAccessory对象描述WIFI外设,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @interface EAWiFiUnconfiguredAccessory : NSObject
@property(copy, nonatomic, readonly) NSString *name;
@property(copy, nonatomic, readonly) NSString *manufacturer;
@property(copy, nonatomic, readonly) NSString *model;
@property(copy, nonatomic, readonly) NSString *ssid;
@property(copy, nonatomic, readonly) NSString *macAddress;
@property(readonly, nonatomic, readonly) EAWiFiUnconfiguredAccessoryProperties properties; @end
|