iOS bluetooth low energy not detecting peripherals -
my app won't detect peripherals. im using light blue simulate bluetooth low energy peripheral , app won't sense it. installed light blue on 2 devices make sure generating peripheral signal , is. suggestions? labels updating , nslog showing scanning 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 resources can 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 resources can recreated. } - (void)centralmanagerdidupdatestate:(cbcentralmanager *)central { //you should test scenarios if (central.state == cbcentralmanagerstateunknown) { self.alabel.text = @"i dont because state unknown."; return; } if (central.state == cbcentralmanagerstatepoweredon) { //scan devices [_centralmanager scanforperipheralswithservices:nil options:@{ cbcentralmanagerscanoptionallowduplicateskey : @yes }]; nslog(@"scanning started"); } if (central.state == cbcentralmanagerstateresetting) { self.alabel.text = @"i dont because state resetting."; return; } if (central.state == cbcentralmanagerstateunsupported) { self.alabel.text = @"i dont because state unsupported."; return; } if (central.state == cbcentralmanagerstateunauthorized) { self.alabel.text = @"i dont because state unauthorized."; return; } if (central.state == cbcentralmanagerstatepoweredoff) { self.alabel.text = @"i dont because state powered off."; return; } } - (void)centralmanger:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { nslog(@"discovered %@ @ %@", peripheral.name, rssi); self.myperipherals.text = [nsstring stringwithformat:@"%@%@",peripheral.name, rssi]; if (_discoveredperepheral != peripheral) { //save copy of peripheral _discoveredperepheral = peripheral; //and connect nslog(@"connecting 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 connect"); [self cleanup]; } -(void)cleanup { //see if subscribed characteristic on peripheral if (_discoveredperepheral.services != nil) { (cbservice *service in _discoveredperepheral.services) { if (service.characteristics != nil) { (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; } (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; } (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 got 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
looks aren't declaring implement cbcentralmanagerdelegate protocol. change bluetoothviewcontroller interface add cbcentralmanagerdelegate.
@interface bluetoothviewcontroller () <cbcentralmanagerdelegate>
Comments
Post a Comment