I have a method which is supposed to be called once a BLE device is connected. but for some reason this is not happening. There are multiple view controllers which are all doing there job first, but the final one which should update a label is not being called, this is called realDataCallBackWithData.
here is the code in viewController.m that is not being called:
It should be called after this method is called in VTConnectViewController.m:
for reference, here is the VTO2Communicate.h:
and the VTO2Parser.h
Everything else is being called and i have confirmed this in the logs, it is only the method in the viewController.m which is not firing, can someone tell me where i have gone wrong?
here is the code in viewController.m that is not being called:
Code Block #import "ViewController.h" #import "CRPC_300SDK.h" #import "CRBlueToothManager.h" #import "CRHeartLiveView.h" #import "DataObject.h" #import "IntervalGraph.h" #import <VTO2Lib/VTO2Lib.h> #import "VTBLEUtils.h" #import "VTConnectViewController.h" @interface ViewController ()<CRPC_300SDKDelegate,CRBlueToothManagerDelegate,VTO2CommunicateDelegate> - (void)viewDidLoad { [super viewDidLoad]; [VTO2Communicate sharedInstance].delegate = self; } // This is the method not being called: -(void)realDataCallBackWithData:(NSData *)realData { if (realData == nil){ NSLog(@"error"); return; } VTRealObject *rObj = [VTO2Parser parseO2RealObjectWithData:realData]; u_char hr = rObj.hr; _spo2Label.text = [NSString stringWithFormat:@"%d",hr]; NSLog(@"O2Ring HR = %hhu", hr); }
It should be called after this method is called in VTConnectViewController.m:
Code Block - (void)serviceDeployed:(BOOL)completed{ if (completed) { [self showAlertWithTitle:@"Good !!!" message:@"Start work" handler:^(UIAlertAction *action) { [self performSegueWithIdentifier:@"presentViewController" sender:nil]; }]; _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(readRealTimeData) userInfo:nil repeats:YES]; }else{ [self showAlertWithTitle:@"The peripheral is not supported !!!" message:@"" handler:^(UIAlertAction *action) { [self.deviceListArray removeAllObjects]; [[VTBLEUtils sharedInstance] startScan]; }]; } } //This is the method which should trigger the method in viewController.m -(void)readRealTimeData { [[VTO2Communicate sharedInstance] beginGetRealData]; NSLog(@"beginGetRealData"); }
for reference, here is the VTO2Communicate.h:
Code Block #import <Foundation/Foundation.h> #import <CoreBluetooth/CoreBluetooth.h> #import "VTO2Def.h" @class VTFileToRead; NS_ASSUME_NONNULL_BEGIN @protocol VTO2CommunicateDelegate <NSObject> @optional - (void)serviceDeployed:(BOOL)completed; /// @brief Common command send to peripheral, callback /// @param cmdType command for VTCmdTypeSyncParam/VTCmdTypeSetFactory /// @param result view the enum VTProCommonResult - (void)commonResponse:(VTCmdType)cmdType andResult:(VTCommonResult)result; /// @brief Send the current progress of reading /// @param progress progress value - (void)postCurrentReadProgress:(double)progress; /// @brief Read file complete /// @param fileData view model --- VTFileToRead - (void)readCompleteWithData:(VTFileToRead *)fileData; /// @brief get information complete . if infoData == nil , an error occurred /// @param infoData information data nullable - (void)getInfoWithResultData:(NSData * _Nullable)infoData; /// @brief use `parseO2RealObjectWithData` to parse realData. if realData == nil , an error occurred. /// @param realData real data - (void)realDataCallBackWithData:(NSData * _Nullable)realData; /// @brief use `` to parse realPPG. if realPPG == nil , an error occurred. /// @param realPPG real PPG data - (void)realPPGCallBackWithData:(NSData * _Nullable)realPPG; /// @brief read current peripheral's rssi /// @param RSSI rssi - (void)updatePeripheralRSSI:(NSNumber *)RSSI; @end @interface VTO2Communicate : NSObject /// @brief This peripheral is currently connected. Need to be set after connection @property (nonatomic, strong) CBPeripheral *peripheral; /// @brief current file been read or written @property (nonatomic, strong) VTFileToRead *curReadFile; /// @brief time out ms @property (nonatomic, assign) u_int timeout; @property (nonatomic, assign) id<VTO2CommunicateDelegate> _Nullable delegate; + (VTO2Communicate *)sharedInstance; - (void)readRSSI; /// @brief Get information of peripheral. callback `getInfoWithResultData:` - (void)beginGetInfo; /// @brief Get real-time data. callback `realDataCallBackWithData:` - (void)beginGetRealData; /// @brief Restore factory. callback `commonResponse: andResult:` - (void)beginFactory; /// @brief get PPG data. - (void)beginGetRealPPG; /// @brief set params . all type view struct `VTParamType` . callback `commonResponse: andResult:` /// @param paramType param type /// @param paramValue param content/value - (void)beginToParamType:(VTParamType)paramType content:(NSString *)paramValue; /// @brief Download file from peripheral. callback `readCompleteWithData:` & `postCurrentReadProgress:` /// @param fileName file's name - (void)beginReadFileWithFileName:(NSString *)fileName; @end /// @brief this is a class to describe the completeed current loading or writing file @interface VTFileToRead : NSObject @property (nonatomic, assign) NSString *fileName; @property (nonatomic, assign) u_int fileSize; @property (nonatomic, assign) u_int totalPkgNum; @property (nonatomic, assign) u_int curPkgNum; @property (nonatomic, assign) u_int lastPkgSize; /// @brief download completed response data . @property (nonatomic, strong) NSMutableData *fileData; /// @brief read file result @property (nonatomic, assign) VTFileLoadResult enLoadResult; @end NS_ASSUME_NONNULL_END
and the VTO2Parser.h
Code Block #import <Foundation/Foundation.h> #import "VTO2Info.h" #import "VTO2Object.h" #import "VTO2WaveObject.h" #import "VTRealObject.h" NS_ASSUME_NONNULL_BEGIN @interface VTO2Parser : NSObject /// @brief parse O2 information /// @param infoData infoData from peripheral + (VTO2Info *)parseO2InfoWithData:(NSData * _Nonnull)infoData; /// @brief parse O2 object /// @param fileData fileData from peripheral + (VTO2Object *)parseO2ObjectWithData:(NSData * _Nonnull)fileData; /// @brief parse O2 Wave array . /// @param waveData waveData from VTO2Object + (NSArray <VTO2WaveObject *>*)parseO2WaveObjectArrayWithWaveData:(NSData * _Nonnull)waveData; /// @brief parse O2 Real-time data /// @param realData realData from peripheral + (VTRealObject *)parseO2RealObjectWithData:(NSData *)realData; /// @brief parse O2 Real PPG data /// @param realPPG real PPG data from peripheral + (NSArray <VTRealPPG *>*)parseO2RealPPGWithData:(NSData *)realPPG; @end NS_ASSUME_NONNULL_END
Everything else is being called and i have confirmed this in the logs, it is only the method in the viewController.m which is not firing, can someone tell me where i have gone wrong?
I discovered that the solution to the problem was to re-declare the delegate after the peripheral was connected, i did this by adding the following line of code into the readRealTimeData method:
here is how the now working method looks:
This ensured that the delegate was reset before the callback was fired.
Code Block [VTO2Communicate sharedInstance].delegate = self;
here is how the now working method looks:
Code Block - (void)readRealtimeData{ if (self.deviceCom.sensorConnected == NO){ NSLog(@"readRealtimeData NOT activated, sensor state %d", self.deviceCom.sensorConnected); }else if (self.deviceCom.sensorConnected == YES){ NSLog (@"readRealtimeData activated"); [VTO2Communicate sharedInstance].delegate = self; [[VTO2Communicate sharedInstance] beginGetRealData]; } }
This ensured that the delegate was reset before the callback was fired.