From a5ec36a69488ee5d3f75027361670268ea2adcdd Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Fri, 22 Apr 2016 10:03:00 -0700 Subject: [PATCH 1/4] Make the library compatible with swift and run BT in the background --- .../BlunoTest/Bluno/DFBlunoManager.h | 3 +- .../BlunoTest/Bluno/DFBlunoManager.m | 70 ++++++++++++++++--- IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m | 2 +- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h index 787eaf3..f474239 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,6 +74,7 @@ @interface DFBlunoManager : NSObject @property (nonatomic,weak) id delegate; +@property (nonatomic) BOOL runOnMainThread; // defaults to YES /** [""] * @brief Singleton diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m index abcaed7..15e3845 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,12 +36,21 @@ + (id)sharedInstance this.dicBleDevices = [[NSMutableDictionary alloc] init]; this.dicBlunoDevices = [[NSMutableDictionary alloc] init]; this->_bSupported = NO; + this.runOnMainThread = YES; this.centralManager = [[CBCentralManager alloc]initWithDelegate:this queue:nil]; } return this; } +-(void)setDelegate:(id)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,8 +63,14 @@ - (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 +157,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 +180,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 +203,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 +228,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 +263,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 +283,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]; } From f26ee675f99fa65aae6dbdd628f610f051c85492 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Fri, 22 Apr 2016 10:06:57 -0700 Subject: [PATCH 2/4] Spaces vs tabs --- .../BlunoTest/Bluno/DFBlunoManager.m | 118 +++++++++--------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m index 15e3845..8c99f97 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m +++ b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m @@ -13,7 +13,7 @@ @interface DFBlunoManager () { BOOL _bSupported; - dispatch_queue_t _backgroundQueue; + dispatch_queue_t _backgroundQueue; } @property (strong,nonatomic) CBCentralManager* centralManager; @@ -28,26 +28,26 @@ @implementation DFBlunoManager + (id)sharedInstance { - static DFBlunoManager* this = nil; + static DFBlunoManager* this = nil; - if (!this) + if (!this) { - this = [[DFBlunoManager alloc] init]; + this = [[DFBlunoManager alloc] init]; this.dicBleDevices = [[NSMutableDictionary alloc] init]; this.dicBlunoDevices = [[NSMutableDictionary alloc] init]; this->_bSupported = NO; - this.runOnMainThread = YES; + this.runOnMainThread = YES; this.centralManager = [[CBCentralManager alloc]initWithDelegate:this queue:nil]; } - return this; + return this; } -(void)setDelegate:(id)aDelegate { - _delegate = aDelegate; - _backgroundQueue = dispatch_queue_create("dfrobot.bluetooth", DISPATCH_QUEUE_SERIAL); - self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_backgroundQueue]; + _delegate = aDelegate; + _backgroundQueue = dispatch_queue_create("dfrobot.bluetooth", DISPATCH_QUEUE_SERIAL); + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_backgroundQueue]; } @@ -63,14 +63,14 @@ - (void)configureSensorTag:(CBPeripheral*)peripheral blunoDev->_bReadyToWrite = YES; if ([((NSObject*)_delegate) respondsToSelector:@selector(readyToCommunicate:)]) { - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate readyToCommunicate:blunoDev]; - }); - } else { - [_delegate readyToCommunicate:blunoDev]; - } - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate readyToCommunicate:blunoDev]; + }); + } else { + [_delegate readyToCommunicate:blunoDev]; + } + } } @@ -157,13 +157,13 @@ -(void)centralManagerDidUpdateState:(CBCentralManager *)central if ([((NSObject*)_delegate) respondsToSelector:@selector(bleDidUpdateState:)]) { - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate bleDidUpdateState:_bSupported]; - }); - } else { - [_delegate bleDidUpdateState:_bSupported]; - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate bleDidUpdateState:_bSupported]; + }); + } else { + [_delegate bleDidUpdateState:_bSupported]; + } } } @@ -180,13 +180,13 @@ -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPerip if ([((NSObject*)_delegate) respondsToSelector:@selector(didDiscoverDevice:)]) { DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate didDiscoverDevice:blunoDev]; - }); - } else { - [_delegate didDiscoverDevice:blunoDev]; - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDiscoverDevice:blunoDev]; + }); + } else { + [_delegate didDiscoverDevice:blunoDev]; + } } } } @@ -203,13 +203,13 @@ -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPerip if ([((NSObject*)_delegate) respondsToSelector:@selector(didDiscoverDevice:)]) { - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate didDiscoverDevice:blunoDev]; - }); - } else { - [_delegate didDiscoverDevice:blunoDev]; - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDiscoverDevice:blunoDev]; + }); + } else { + [_delegate didDiscoverDevice:blunoDev]; + } } } } @@ -228,13 +228,13 @@ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPe blunoDev->_bReadyToWrite = NO; if ([((NSObject*)_delegate) respondsToSelector:@selector(didDisconnectDevice:)]) { - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate didDisconnectDevice:blunoDev]; - }); - } else { - [_delegate didDisconnectDevice:blunoDev]; - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didDisconnectDevice:blunoDev]; + }); + } else { + [_delegate didDisconnectDevice:blunoDev]; + } } } @@ -267,13 +267,13 @@ -(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CB { NSString* key = [peripheral.identifier UUIDString]; DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate didReceiveData:characteristic.value device:blunoDev]; - }); - } else { - [_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]; + } } } @@ -283,13 +283,13 @@ -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBC { NSString* key = [peripheral.identifier UUIDString]; DFBlunoDevice* blunoDev = [self.dicBlunoDevices objectForKey:key]; - if (self.runOnMainThread) { - dispatch_async(dispatch_get_main_queue(), ^{ - [_delegate didWriteData:blunoDev]; - }); - } else { - [_delegate didWriteData:blunoDev]; - } + if (self.runOnMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_delegate didWriteData:blunoDev]; + }); + } else { + [_delegate didWriteData:blunoDev]; + } } } From eb2003ad3cbf42ef4077b345b045bc0f855481d9 Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Fri, 22 Apr 2016 10:08:19 -0700 Subject: [PATCH 3/4] Missed small detail --- IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m index 8c99f97..1172af4 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m +++ b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m @@ -28,16 +28,15 @@ @implementation DFBlunoManager + (id)sharedInstance { - static DFBlunoManager* this = nil; + static DFBlunoManager* this = nil; - if (!this) + if (!this) { - this = [[DFBlunoManager alloc] init]; + this = [[DFBlunoManager alloc] init]; this.dicBleDevices = [[NSMutableDictionary alloc] init]; this.dicBlunoDevices = [[NSMutableDictionary alloc] init]; this->_bSupported = NO; this.runOnMainThread = YES; - this.centralManager = [[CBCentralManager alloc]initWithDelegate:this queue:nil]; } return this; From 1b3215e9f5b4039503e58963608335e38f2b6d2d Mon Sep 17 00:00:00 2001 From: Pedro Enrique Date: Wed, 27 Apr 2016 10:50:14 -0700 Subject: [PATCH 4/4] made sharedInstance "instancetype" for swift compatibility --- IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h index f474239..bef14e9 100644 --- a/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h +++ b/IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h @@ -81,7 +81,7 @@ [""] * [""] * @return DFBlunoManager [""] */ -+ (id)sharedInstance; ++ (instancetype)sharedInstance; /** [""] * @brief Scan the BLUNO device