【iOS与EV3混合机器人编程系列之7】通过蓝牙控制EV3
1 前言
2 具体代码实现
- (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; } }
3 为什么用蓝牙?
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。