本文共 2334 字,大约阅读时间需要 7 分钟。
- (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]; }在这里我也直接使用了EADemo上的代码来分析。
- (IBAction)go:(id)sender { if (isConnected) { NSData *data = [EV3DirectCommander turnMotorAtPort:EV3OutputPortB power:50]; [[EADSessionController sharedController] writeData:data]; isGo = YES; }}