Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make the library compatible with swift and run BT in the background #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@
[""] *
[""] * @return void
[""] */
-(void)didReceiveData:(NSData*)data Device:(DFBlunoDevice*)dev;
-(void)didReceiveData:(NSData*)data device:(DFBlunoDevice*)dev;


@end

@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
Expand Down
71 changes: 61 additions & 10 deletions IOS/BlunoBasicDemo/BlunoTest/Bluno/DFBlunoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@interface DFBlunoManager ()
{
BOOL _bSupported;
dispatch_queue_t _backgroundQueue;
}

@property (strong,nonatomic) CBCentralManager* centralManager;
Expand All @@ -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
Expand All @@ -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];
}
}

}
Expand Down Expand Up @@ -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];
}
}

}
Expand All @@ -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];
}
}
}
}
Expand All @@ -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];
}
}
}
}
Expand All @@ -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];
}
}
}

Expand Down Expand Up @@ -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];
}
}
}

Expand All @@ -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];
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion IOS/BlunoBasicDemo/BlunoTest/Classes/VCMain.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down