iphone - iOS Core Bluetooth Not asking for Pair -
in recent project, need communicate hardware (bluetooth low energy).i have implement delegate methods code. able connect hardware , device, not getting pairing alert (attached screen shot). why not asking pairing? thank you.
#import "btwcentralconnectionmanager.h" @implementation btwcentralconnectionmanager @synthesize cbcmanager; @synthesize discoveredperipheral; @synthesize findmeservicecharacteristic; @synthesize findmeservice; @synthesize delegate=_delegate; static nsstring *kfindmeserviceuuid=@"1802"; static nsstring *kfindmecharacteristicuuid=@"2a06"; static btwcentralconnectionmanager* connectionmanager = nil; +(btwcentralconnectionmanager *)sharedconnectionmanager{ @synchronized(self) { if (!connectionmanager){ connectionmanager=[[self alloc] init]; } return connectionmanager; } return nil; } -(void)findme { byte code=0x02; if(self.discoveredperipheral){ [self.discoveredperipheral writevalue:[nsdata datawithbytes:&code length:1] forcharacteristic:self.findmeservicecharacteristic type:cbcharacteristicwritewithoutresponse]; }else{ uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:@"invalid charactersitcs" delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } } -(void)searchfordevices{ self.cbcmanager=[[cbcentralmanager alloc] initwithdelegate:self queue:nil]; } -(void)connect { nsdictionary* connectoptions = [nsdictionary dictionarywithobject:[nsnumber numberwithbool:yes] forkey:cbconnectperipheraloptionnotifyondisconnectionkey]; [self.cbcmanager connectperipheral:self.discoveredperipheral options:connectoptions]; } -(void)disconnect{ [self cleanup]; } - (void)centralmanagerdidupdatestate:(cbcentralmanager *)central { switch (central.state) { case cbcentralmanagerstatepoweredon:{ [self.cbcmanager scanforperipheralswithservices:@[ [cbuuid uuidwithstring:kfindmeserviceuuid] ] options:@{cbcentralmanagerscanoptionallowduplicateskey : @no }]; } break; // scans peripheral default:{ uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:@"cental manager did change state" delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } break; } } - (void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi { // stops scanning peripheral [self.cbcmanager stopscan]; if (self.discoveredperipheral != peripheral) { self.discoveredperipheral = peripheral; [self.delegate diddevicediscoverd:self.discoveredperipheral.name]; } } - (void)centralmanager:(cbcentralmanager *)central didfailtoconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error { [self.delegate diddeviceconnectionfailed:error]; [self cleanup]; } - (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral { [self.delegate diddeviceconnected]; [self.discoveredperipheral setdelegate:self]; [self.discoveredperipheral discoverservices:@[[cbuuid uuidwithstring:kfindmeserviceuuid]]]; } - (void)peripheral:(cbperipheral *)aperipheral diddiscoverservices:(nserror *)error { if (error) { nsstring *strmsg=[nsstring stringwithformat:@"diddiscoverservices: %@", error]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; [self cleanup]; return; } (cbservice *service in aperipheral.services) { if ([service.uuid isequal:[cbuuid uuidwithstring:kfindmeserviceuuid]]) { self.findmeservice=service; [self.discoveredperipheral discovercharacteristics:@[[cbuuid uuidwithstring:kfindmecharacteristicuuid]] forservice:self.findmeservice]; } } } - (void) peripheral:(cbperipheral *)peripheral diddiscovercharacteristicsforservice:(cbservice *)service error:(nserror *)error { if(error){ nsstring *strmsg=[nsstring stringwithformat:@"diddiscovercharacteristicsforservice: %@", error]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } for(cbcharacteristic *character in [service characteristics]) { if([[service uuid] isequal:[cbuuid uuidwithstring:kfindmeserviceuuid]] && [[character uuid] isequal:[cbuuid uuidwithstring:kfindmecharacteristicuuid]]) { nsstring *strmsg=[nsstring stringwithformat:@"diddiscovercharacteristicsforservice: %@", character]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; self.findmeservicecharacteristic = character; } } } - (void) peripheral:(cbperipheral *)peripheral didupdatevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error { nsstring *strmsg=[nsstring stringwithformat:@"did update value characteristic %@, new value: %@, error: %@", characteristic, [characteristic value], error]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } - (void)peripheral:(cbperipheral *)peripheral didupdatenotificationstateforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error { if (error) { nslog(@"error changing notification state: %@", error.localizeddescription); } // exits if it's not transfer characteristic if (![characteristic.uuid isequal:[cbuuid uuidwithstring:kfindmecharacteristicuuid]]) { return; } nsstring *strmsg=[nsstring stringwithformat:@"didupdatenotificationstateforcharacteristic %@, reason: %@", characteristic, error]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } - (void) peripheral:(cbperipheral *)peripheral didwritevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error { if (error) { nsstring *strmsg=[nsstring stringwithformat:@"failed write value characteristic %@, reason: %@", characteristic, error]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } else { nsstring *strmsg=[nsstring stringwithformat:@"did write value characterstic %@, new value: %@", characteristic, [characteristic value]]; uialertview *alertview=[[uialertview alloc] initwithtitle:@"test" message:strmsg delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil]; [alertview show]; alertview=nil; } } - (void)cleanup { if (!self.discoveredperipheral.isconnected) { return; } if (self.discoveredperipheral.services != nil) { (cbservice *service in self.discoveredperipheral.services) { if (service.characteristics != nil) { (cbcharacteristic *characteristic in service.characteristics) { if ([characteristic.uuid isequal:[cbuuid uuidwithstring:kfindmeserviceuuid]]) { if (characteristic.isnotifying) { [self.discoveredperipheral setnotifyvalue:no forcharacteristic:characteristic]; return; } } } } } } [self.cbcmanager cancelperipheralconnection:self.discoveredperipheral]; [self.delegate diddevicedisconnected]; } @end `
if understand right can write value characteristic don't pairing request.
the pairing triggered peripheral. meaning peripheral has refuse write or read request of central characteristic. central gets refusal "unauthorized authentication" , tries pair peripheral , showing pairing alert pop waiting for. done core bluetooth automatically. thing need change characteristics options , permissions in peripheral. apple sample code trigger pairing:
emailcharacteristic = [[cbmutablecharacteristic alloc] initwithtype:emailcharacteristicuuid properties:cbcharacteristicpropertyread | cbcharacteristicpropertynotifyencryptionrequired value:nil permissions:cbattributepermissionsreadencryptionrequired];
source: corebluetooth
_concepts ios7
preview
also check out wwdc 2012 advanced core bluetooth video @ 28 minutes explain concept of pairing.
Comments
Post a Comment