[7 of iOS and EV3 hybrid robot programming series] EV3 and iosev3 are controlled through Bluetooth

Source: Internet
Author: User

[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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.