The procedure for developing an application on the iOS side for communication using OPH-5000 Bluetooth MFi is as follows.
(Note)
  • Use the iOS library (OPHBluetoothService.framework) / iOS method.

  1. You need to register with Apple Developer to develop iOS applications.
    For more information, click the following link to visit the Apple Developer website.
  2. After registration, you can download Xcode.
    To download Xcode, click the following link to check the Xcode website.
  3. If you want to transfer the created app to the actual iOS device and check the operation, you need to register it in the iOS Developer Program.
    For more information, click the following link to visit the iOS Developer Program website.

The following example explains the procedure from the state where the project "FirstApp" is created on Xcode. To create an Xcode project, Please check Apple developer documentation website.

In addition, this usage procedure explains the procedure when using the development language Objective-C and the development environment Xcode 12.5.

OPHBluetoothService.framework


Procedure for using the development environment


It is necessary to support the following two points in the development environment.
  1. Add ExternalAccessory.framework and OPHBluetoothService.framework from [Frameworks, Libraries, and Embedded Content] on the [General] tab of TARGETS of the project.
    ExternalAccessory.framework is added from the Xcode library by clicking [+] on the [Frameworks, Libraries, and Embedded Content] screen.
    For more information on ExternalAccessory.framework, please see the ExternalAccessory website.

    OPHBluetoothService.framework is added from [Add Other] by clicking [+] on the [Frameworks, Libraries, and Embedded Content] screen.

  2. Add Supported external accessory protocols to Info.plist and specify jp.opto.opnprotocol .

Use of communication API


We will explain how to use the main API using sample code.
In addition, import is required to use the framework.
sample
#import <OPHBluetoothService/OPHBluetoothService.h>
  • Generate / acquire OPHBluetoothService

    All Bluetooth communication with OPH-5000i is done via OPHBluetoothService class.
    You can create and get an instance by executing the sharedController () method of OPHBluetoothService.
    Since there is only one instance of OPHBluetoothService running, it will be returned after it is created if it has not been created yet, and it will be returned if it has already been created.
    sample
     OPHBluetoothService  * sessionController = [OPHBluetoothService sharedController];
    

  • Connect with OPH-5000i

    You can connect to OPH-5000i by executing the setupControllerForAccessory () method of OPHBluetoothService.
    In this sample, the accessory and protocol string, which are the arguments required to execute the setupControllerForAccessory () method, are acquired and connected.
    sample
    NSArray *accessoryList = [[NSMutableArray alloc] initWithArray:[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
    if(accessoryList == nil || accessoryList.count == 0) return NO;       
    EAAccessory *accessory = [accessoryList lastObject];
    OPHBluetoothService *service =  [OPHBluetoothService sharedController];
    NSArray *protocolStrings = [accessory protocolStrings];
    if(protocolStrings == nil || [protocolStrings count]==0) return NO;
    NSString *protocolString = [protocolStrings objectAtIndex:0];
    [service setupControllerForAccessory:accessory withProtocolString:protocolString];
    


  • Open a session

    If you are already connected, you can open a session with OPH-5000i by executing the openSession () method of OPHBluetoothService.
    sample
     OPHBluetoothService  * sessionController = [OPHBluetoothService sharedController];
    [sessionController openSession];
    

  • Close session

    You can open a session with OPH-5000i by executing the closeSession () method of OPHBluetoothService.
    sample
     OPHBluetoothService  * sessionController = [OPHBluetoothService sharedController];
    [sessionController closeSession];
    

  • Check if it is connected to OPH-5000i

    You can check if you are connected to OPH-5000i by checking the isConnected property of EAAccessory, which is the accessory property of OPHBluetoothService. If YES, you are connected.
    sample
    isConnected = [[[ OPHBluetoothService  sharedController] accessory] isConnected];
    

  • Send to OPH-5000i

    When sending to OPH-5000i, you can send to OPH-5000i by executing the writeData () method of OPHBluetoothService.
    According to the accessory interface specifications, the maximum length of External Accessory Protocol messages that OPH-5000i (MFi accessory) can receive from iOS devices is 2036 bytes.
    sample
    -(IBAction)sendStringButtonClick:(id)sender
    {
        NSData *writeData = [@"TestSting" dataUsingEncoding:NSUTF8StringEncoding];
        if(_service.isConnected){
            //Send data to OPH-5000i
            [_service writeData: writeData];
        }  
    }
    

  • Receive from OPH-5000i

    In order to process the communication result between OPHBluetoothService and OPH-5000i, First, the class that processes the communication result must adopt the OPHBluetoothServiceDelegate protocol.
    In this sample, the ViewController class uses this protocol.
    sample
     @interface  ViewController & lt; OPHBluetoothServiceDelegate & gt {
    }
    
    Then specify that class as the delegate for your instance of OPHBluetoothService.
    sample
     OPHBluetoothService  * sessionController = [OPHBluetoothService sharedController];
    [service setDelegate: self];
    
    Finally, you can execute the following method in that class to receive from OPH-5000i.
    sample
    - (void)bluetoothService:(OPHBluetoothService *)service receivedData:(NSData *)data
    {
        NSLog(@"receivedData: %@", data);
    }
    

Related matters

Last updated: 2022/05/09