The procedure for developing an application on the iOS side for communication using OPH-5000 Bluetooth MFi is as follows.
(Note)
  • Does not 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.

Download OPHBluetoothService.m/h


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.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;
     EAAccessory  * accessory = [accessoryList lastObject];
     OPHBluetoothService  * service = [OPHBluetoothService sharedController];
     NSArray  * protocolStrings = [accessory protocolStrings];
     if  (protocolStrings == nil || [protocolStrings count] == ​​0) return;
     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
     //OPHBluetoothService.m 
    -( BOOL ) writeData: (NSData *) data
    {
        RELEASE_TO_NIL (_writeData)
         if  (_writeData == nil) {
            _writeData = [[NSMutableData alloc] init];
        }
        #define MAX_OPH_RECEIVE_SIZE 2036
         if  (data.length & gt; MAX_OPH_RECEIVE_SIZE) {
            NSLog (@ "over size error.");
             return  NO;
        }
        [_writeData appendData: data];
        [self _writeData];
         return  YES;
    }
    ---------
     //ViewController.m 
     NSData  * d = [string dataUsingEncoding: NSUTF8StringEncoding];
    [_service writeData: d];
    

  • 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. After receiving the outputStream of the OPHBluetoothService session, it is necessary to return an ACK to OPH-5000i.
    sample
    -( void ) receivedData: ( NSData  *) readdata
    {
        NSLog (@ "receivedData: _readData:% @", _readData);
        id & lt; OPHBluetoothServiceDelegate & gt delegate =
        (id & lt; OPHBluetoothServiceDelegate & gt) self.delegate;
         // It is necessary to return ACK to OPH-5000i after receiving outputStream 
        [self write ACK];
         if  ([delegate respondsToSelector: @selector (bluetoothService: receivedData :)]) {
            [delegate bluetoothService: self receivedData: readdata];
        }
        [self performSelector: @selector (clearReadData)];
    }
    .....................
    -( void ) bluetoothService: (OPHBluetoothService *) service receivedData: (NSData *) data
    {
         NSLog  (@ "receivedData:% @", data);
    }
    

Related matters

Last updated: 2021/11/18