diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h index 787eaf3..bef14e9 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h +++ b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h @@ -66,7 +66,7 @@ [""] * [""] * @return void [""] */ --(void)didReceiveData:(NSData*)data Device:(DFBlunoDevice*)dev; +-(void)didReceiveData:(NSData*)data device:(DFBlunoDevice*)dev; @end @@ -74,13 +74,14 @@ @interface DFBlunoManager : NSObject<CBCentralManagerDelegate,CBPeripheralDelegate> @property (nonatomic,weak) id<DFBlunoDelegate> delegate; +@property (nonatomic) BOOL runOnMainThread; // defaults to YES /** [""] * @brief Singleton [""] * [""] * @return DFBlunoManager [""] */ -+ (id)sharedInstance; ++ (instancetype)sharedInstance; /** [""] * @brief Scan the BLUNO device diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m index abcaed7..1172af4 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m +++ b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m @@ -13,6 +13,7 @@ @interface DFBlunoManager () { BOOL _bSupported; + dispatch_queue_t _backgroundQueue; } @property (strong,nonatomic) CBCentralManager* centralManager; @@ -35,10 +36,18 @@ + (id)sharedInstance this.dicBleDevices = [[NSMutableDictionary alloc] init]; this.dicBlunoDevices = [[NSMutableDictionary alloc] init]; this->_bSupported = NO; - this.centralManager = [[CBCentralManager alloc]initWithDelegate:this queue:nil]; + this.runOnMainThread = YES; } - return this; + return this; +} + +-(void)setDelegate:(id<DFBlunoDelegate>)aDelegate +{ + _delegate = aDelegate; + _backgroundQueue = dispatch_queue_create("dfrobot.bluetooth", DISPATCH_QUEUE_SERIAL); + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_backgroundQueue]; + } - (void)configureSensorTag:(CBPeripheral*)peripheral @@ -53,7 +62,13 @@ - (void)configureSensorTag:(CBPeripheral*)peripheral blunoDev->_bReadyToWrite = YES; if ([((NSObject*)_delegate) respondsToSelector:@selector(readyToCommunicate:)]) { - [_delegate readyToCommunicate:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate readyToCommunicate:blunoDev]; + }); + } else { + [_delegate readyToCommunicate:blunoDev]; + } } } @@ -141,7 +156,13 @@ -(void)centralManagerDidUpdateState:(CBCentralManager *)central if ([((NSObject*)_delegate) respondsToSelector:@selector(bleDidUpdateState:)]) { - [_delegate bleDidUpdateState:_bSupported]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate bleDidUpdateState:_bSupported]; + }); + } else { + [_delegate bleDidUpdateState:_bSupported]; + } } } @@ -158,7 +179,13 @@ -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPerip if ([((NSObject*)_delegate) respondsToSelector:@selector(didDiscoverDevice:)]) { DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - [_delegate didDiscoverDevice:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDiscoverDevice:blunoDev]; + }); + } else { + [_delegate didDiscoverDevice:blunoDev]; + } } } } @@ -175,7 +202,13 @@ -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPerip if ([((NSObject*)_delegate) respondsToSelector:@selector(didDiscoverDevice:)]) { - [_delegate didDiscoverDevice:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDiscoverDevice:blunoDev]; + }); + } else { + [_delegate didDiscoverDevice:blunoDev]; + } } } } @@ -194,7 +227,13 @@ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPe blunoDev->_bReadyToWrite = NO; if ([((NSObject*)_delegate) respondsToSelector:@selector(didDisconnectDevice:)]) { - [_delegate didDisconnectDevice:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDisconnectDevice:blunoDev]; + }); + } else { + [_delegate didDisconnectDevice:blunoDev]; + } } } @@ -223,11 +262,17 @@ -(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharac -(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { - if ([((NSObject*)_delegate) respondsToSelector:@selector(didReceiveData:Device:)]) + if ([((NSObject*)_delegate) respondsToSelector:@selector(didReceiveData:device:)]) { NSString* key = [peripheral.identifier UUIDString]; DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - [_delegate didReceiveData:characteristic.value Device:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didReceiveData:characteristic.value device:blunoDev]; + }); + } else { + [_delegate didReceiveData:characteristic.value device:blunoDev]; + } } } @@ -237,7 +282,13 @@ -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBC { NSString* key = [peripheral.identifier UUIDString]; DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - [_delegate didWriteData:blunoDev]; + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didWriteData:blunoDev]; + }); + } else { + [_delegate didWriteData:blunoDev]; + } } } diff --git a/IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m b/IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m index 2c56eb0..ee4f1f8 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m +++ b/IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m @@ -115,7 +115,7 @@ -(void)didWriteData:(DFBlunoDevice*)dev { } --(void)didReceiveData:(NSData*)data Device:(DFBlunoDevice*)dev +-(void)didReceiveData:(NSData*)data device:(DFBlunoDevice*)dev { self.lbReceivedMsg.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; }