Skip to content

Instantly share code, notes, and snippets.

@andrask
Created April 5, 2013 08:13
Show Gist options
  • Save andrask/5317502 to your computer and use it in GitHub Desktop.
Save andrask/5317502 to your computer and use it in GitHub Desktop.
sdsd
//
// main.m
// CmdScanner
//
// Created by Andras Kovi on 2013.04.05..
//
#import <Foundation/Foundation.h>
#import "WirelessBandManager.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
WirelessBandManager *manager = [[WirelessBandManager alloc] init];
dispatch_main();
}
return 0;
}
//
// WirelessBandManager.m
// CmdScanner
//
// Created by Andras Kovi on 2013.04.05..
#import <Foundation/Foundation.h>
#import <IOBluetooth/IOBluetooth.h>
@interface WirelessBandManager : NSObject <CBCentralManagerDelegate>
{
CBCentralManager * mBLEManager;
CBPeripheral * mPeripheral;
dispatch_queue_t mBleDispatchQueue;
}
@end
@implementation WirelessBandManager
- (id)init
{
self = [super init];
if (self)
{
mBleDispatchQueue = dispatch_queue_create("com.xyz.ble.mgr", DISPATCH_QUEUE_SERIAL);
mBLEManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
NSLog(@"Initialized");
}
return self;
}
#pragma mark -
#pragma mark CBCentralManager Delegate Methods
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"Did update state to: %d",(int)central.state);
if (central.state == CBCentralManagerStatePoweredOn)
{
NSString * uuidStr = nil;
if (uuidStr) {
NSMutableArray * uuids = [[NSMutableArray alloc] initWithCapacity:1];
CFUUIDRef uuidRef = CFUUIDCreateFromString(NULL, (CFStringRef)uuidStr);
[uuids addObject:(__bridge id)uuidRef];
CFRelease(uuidRef);
[mBLEManager retrievePeripherals:uuids];
NSLog(@"Retrieved");
} else {
[mBLEManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"1802"]] options:nil];
NSLog(@"Scanning");
}
}
}
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Did discover peripheral: %@",mPeripheral);
NSLog(@"Did discover peripheral UUID: %@",mPeripheral.UUID);
mPeripheral = peripheral;
[mBLEManager stopScan];
[mBLEManager connectPeripheral:mPeripheral options:nil];
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
mPeripheral = peripherals[0];
NSLog(@"Did retrieve peripheral: %@",mPeripheral);
NSDictionary *connOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
[mBLEManager connectPeripheral:mPeripheral options:connOptions];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@",peripheral);
NSLog(@"Did connect peripheral UUID: %@",mPeripheral.UUID);
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Did FAIL to connect to peripheral: %@ Error:%@",peripheral,error);
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Did disconnect from peripheral: %@ Error:%@",peripheral,error);
}
@end
@angelovAlex
Copy link

Hi, Andras. Have you tried to run it on iOS? I have similar code but never get didDiscoverPeripheral called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment