Download FirstApp
- Download the FirstApp zip file from the following link.
- Unzip the zip file and you will get the following files:
FirstApp
FirstApp.xcodeproj
OPHBluetoothService.framework - Open FirstApp.xcodeproj in Xcode.
- Connect your iOS device to your Mac and build FirstApp in Xcode.
FirstApp operation procedure
- Launch Settings on your iOS device and turn on Bluetooth.
- Read the iOS 12 digit Bluetooth address barcode with the OPH-5000i sample application.
- When the connection is established, start this sample (FirstApp).
- Receive data
Read the barcode with the OPH-5000i sample application.
The data sent from OPH-5000i is displayed in the text view of this sample (First App). - Send the data.
Click the "Sent Test String" button to send the "Test String" string to OPH-5000i.
OPHBluetoothService class is a core class that performs Bluetooth communication with OPH-5000i and is in charge of session management, accessory holding, I / O processing, etc.
OPHBluetoothService is implemented with GoF's Singleton pattern. There is only one instance of the OPHBluetoothService class running.
In the OPHBluetoothService class, ExternalAccessory.framework is used for communication with external accessories.
Sample: ViewController.m
// // ViewController.m // FirstApp // // Copyright (c) 2022 OPTOELECTRONICS Co.,LTD. All rights reserved. // #import "ViewController.h" @interface ViewController () - (IBAction)sendStringButtonClick:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *connectStatus; end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _text = [[NSMutableString alloc] init]; textView.delegate = self; _connectStatus.text = @"not Connected"; [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; textView.layer.cornerRadius = 1.0; textView.layer.borderColor = UIColor.blackColor.CGColor; textView.layer.borderWidth = 1; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil]; _service = [OPHBluetoothService sharedController]; [_service setDelegate:self]; [_service openSession]; if(_service.accessory.isConnected){ _connectStatus.text = [NSString stringWithFormat:@"%@[%@] Connected",_service.accessory.modelNumber,_service.accessory.serialNumber]; }else{ _connectStatus.text = @"not Connected"; } textView.text = @""; } - (BOOL)openAccessory { //Get Accessory during connection NSArray *accessoryList = [[NSMutableArray alloc] initWithArray: [[EAAccessoryManager sharedAccessoryManager] connectedAccessories]]; if(accessoryList == nil || accessoryList.count == 0) return NO; EAAccessory *accessory = [accessoryList lastObject]; OPHBluetoothService *s = [OPHBluetoothService sharedController]; NSArray *protocolStrings = [accessory protocolStrings]; if(protocolStrings == nil || [protocolStrings count]==0) return NO; BOOL result = NO; NSString *protocolString = [protocolStrings objectAtIndex:0]; for (int i = 0; i<3; i++) { [s setupControllerForAccessory:accessory withProtocolString:protocolString]; if((result =[s openSession])){ break; } sleep(1); } //openSession failed if(result == NO){ return NO; } s.delegate = self; if(_service.accessory.isConnected){ _connectStatus.text = [NSString stringWithFormat:@"%@[%@] Connected",_service.accessory.modelNumber,_service.accessory.serialNumber]; }else{ _connectStatus.text = @"not Connected"; return NO; } return YES; } - (void)_accessoryDidConnect:(NSNotification *)notification { OPHBluetoothService *sessionController = [OPHBluetoothService sharedController]; static NSString * const OPH5000iAccessoryModelNumber = @"OPH-5000i"; if ((sessionController.accessory.isConnected) && ([sessionController.accessory.modelNumber isEqualToString:OPH5000iAccessoryModelNumber])){ _connectStatus.text = [NSString stringWithFormat:@"%@[%@]Connected",sessionController.accessory.modelNumber,sessionController.accessory.serialNumber]; } else{ [self openAccessory]; } } - (void)_accessoryDidDisconnect:(NSNotification *)notification { if(!(_service.accessory.isConnected )){ _connectStatus.text = @"not Connected"; } } //[Send TestString] Button - (IBAction)sendStringButtonClick:(id)sender { NSString *writeData = @"TestString"; [self sendDataToOph5000i:writeData]; } #pragma mark - OPHBluetoothService writeData //Send data to OPH-5000i -(void)sendDataToOph5000i:(NSString *)string { if(_service.accessory.isConnected){ NSData *d = [string dataUsingEncoding:NSUTF8StringEncoding]; [_service writeData: d]; } else { return; } } #pragma mark - OPHBluetoothService Delegate receivedData //Received from OPH-5000i - (void)bluetoothService:(OPHBluetoothService *)service receivedData:(NSData *)data { NSString *str = [[NSString alloc] initWithData:data encoding:NSShiftJISStringEncoding]; [_text appendFormat:@"%@\n",str]; textView.text = _text; NSRange range = NSMakeRange(textView.text.length - 1, 1); [textView scrollRangeToVisible:range]; str = nil; } //Touch something other than textView to hide the keyboard -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if(textView.isFirstResponder){ [textView resignFirstResponder]; } } end
Related matters
Last updated: 2022/05/09