- (void)connectEV3
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionDataReceived:) name:EADSessionDataReceivedNotification object:nil];
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
self.sessionController = [EADSessionController sharedController];
accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
NSLog(@"accessory list:%@",accessoryList);
if(accessoryList != nil){
[self.sessionController setupControllerForAccessory:[accessoryList firstObject]
withProtocolString:@"COM.LEGO.MINDSTORMS.EV3"];
isConnected = [self.sessionController openSession];
}
}
- (void)accessoryDidConnect:(NSNotification *)notification {
NSLog(@"EV3 did connect!");
EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
[self.sessionController setupControllerForAccessory:connectedAccessory
withProtocolString:@"COM.LEGO.MINDSTORMS.EV3"];
isConnected = [self.sessionController openSession];
}
- (IBAction)go:(id)sender { if (isConnected) { NSData *data = [EV3DirectCommander turnMotorAtPort:EV3OutputPortB power:50]; [[EADSessionController sharedController] writeData:data]; isGo = YES; } }
原文地址:http://blog.csdn.net/songrotek/article/details/41621995