[7 of iOS and EV3 hybrid robot programming series] EV3 and iosev3 are controlled through Bluetooth
1 Preface in my previous blogs in this series, I found that without iOS jailbreak, I could not use Bluetooth to control EV3 and write a Commander-like program. However, recent studies with Netizens found that the use of External Accessory to achieve Bluetooth transmission is simpler than imagined. MFI protocol is much easier than imagined. The key is that we can get EV3 MFI protocol strings. Next, let's see how it is implemented. 2. Specific Code implementation first, Apple officially has a demo about External Accessory called EAdemo. You can go down and modify the Protocol string in the plist file, as shown below:
Then run the command to directly connect to EV3.
With this foundation, let's look at the implementation principle.
Step 1: Add ExternalAccessory. Framework.
Step 2: connect to EV3
- (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]; }
Here I also directly use the code on EADemo for analysis. The EADSessionController in the official example has done a good job in data transmission, so we can use it directly. The basic process is to create an instance, create a controller, and then openSession.
Step 3: Control EV3 and directly use the previously compiled EV3DirectCommander to implement a simple example:
- (IBAction)go:(id)sender { if (isConnected) { NSData *data = [EV3DirectCommander turnMotorAtPort:EV3OutputPortB power:50]; [[EADSessionController sharedController] writeData:data]; isGo = YES; }}
3. Why does it use Bluetooth? Obviously, I had to buy additional devices with Wifi before, and it was too troublesome to connect to wifi. using Bluetooth directly saves a lot of trouble and can do a better job! Hey, you can create a Commander yourself! More powerful Commander!