iOS bluetooth low energy not detecting peripherals
Posted
by
user3712524
on Stack Overflow
See other posts from Stack Overflow
or by user3712524
Published on 2014-06-09T21:22:08Z
Indexed on
2014/06/09
21:24 UTC
Read the original article
Hit count: 278
My app won't detect peripherals. Im using light blue to simulate a bluetooth low energy peripheral and my app just won't sense it. I even installed light blue on two devices to make sure it was generating a peripheral signal properly and it is. Any suggestions? My labels are updating and the NSLog is showing that the scanning is starting.
Thanks in advance.
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *navDestination;
@end
#import "ViewController.h"
@implementation ViewController
- (IBAction)connect:(id)sender {
}
- (IBAction)navDestination:(id)sender {
NSString *destinationText = self.navDestination.text;
}
- (void)viewDidLoad {
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface BlueToothViewController : UIViewController
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *discoveredPerepheral;
@property (strong, nonatomic) NSMutableData *data;
@property (strong, nonatomic) IBOutlet UITextView *textview;
@property (weak, nonatomic) IBOutlet UILabel *charLabel;
@property (weak, nonatomic) IBOutlet UILabel *isConnected;
@property (weak, nonatomic) IBOutlet UILabel *myPeripherals;
@property (weak, nonatomic) IBOutlet UILabel *aLabel;
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral: (CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
-(void)cleanup;
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
@end
@interface BlueToothViewController ()
@end
@implementation BlueToothViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];
_data = [[NSMutableData alloc]init];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_centralManager stopScan];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
//you should test all scenarios
if (central.state == CBCentralManagerStateUnknown) {
self.aLabel.text = @"I dont do anything because my state is unknown.";
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
//scan for devices
[_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"Scanning Started");
}
if (central.state == CBCentralManagerStateResetting) {
self.aLabel.text = @"I dont do anything because my state is resetting.";
return;
}
if (central.state == CBCentralManagerStateUnsupported) {
self.aLabel.text = @"I dont do anything because my state is unsupported.";
return;
}
if (central.state == CBCentralManagerStateUnauthorized) {
self.aLabel.text = @"I dont do anything because my state is unauthorized.";
return;
}
if (central.state == CBCentralManagerStatePoweredOff) {
self.aLabel.text = @"I dont do anything because my state is powered off.";
return;
}
}
- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
self.myPeripherals.text = [NSString stringWithFormat:@"%@%@",peripheral.name, RSSI];
if (_discoveredPerepheral != peripheral) {
//save a copy of the peripheral
_discoveredPerepheral = peripheral;
//and connect
NSLog(@"Connecting to peripheral %@", peripheral);
[_centralManager connectPeripheral:peripheral options:nil];
self.aLabel.text = [NSString stringWithFormat:@"%@", peripheral];
}
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
-(void)cleanup {
//see if we are subscribed to a characteristic on the peripheral
if (_discoveredPerepheral.services != nil) {
for (CBService *service in _discoveredPerepheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"508EFF8E-F541-57EF-BD82-B0B4EC504CA9"]]) {
if (characteristic.isNotifying) {
[_discoveredPerepheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[_centralManager cancelPeripheralConnection:_discoveredPerepheral];
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
self.isConnected.text = [NSString stringWithFormat:@"Connected"];
[_data setLength:0];
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) { [self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
//discover other characteristics
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) { [self cleanup];
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) { NSLog(@"Error");
return;
}
NSString *stringFromData = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
self.charLabel.text = [NSString stringWithFormat:@"%@", stringFromData];
//Have we got everything we need?
if ([stringFromData isEqualToString:@"EOM"]) {
[_textview setText:[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]];
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if ([characteristic.UUID isEqual:nil]) {
return;
}
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
else {
[_centralManager cancelPeripheralConnection:peripheral];
}
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
_discoveredPerepheral = nil;
self.isConnected.text = [NSString stringWithFormat:@"Connecting..."];
[_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
}
@end
© Stack Overflow or respective owner