ios - CBManager state always unknown -


i trying check if bluetooth of device on or off. code i've written far

   cbcentralmanager *cbmanager = [[cbcentralmanager alloc] initwithdelegate:self                                                                        queue:nil                                                                      options:                                    [nsdictionary dictionarywithobject:[nsnumber numberwithint:0]                                                                forkey:cbcentralmanageroptionshowpoweralertkey]];       [cbmanager scanforperipheralswithservices:nil options:                                     [nsdictionary dictionarywithobject:[nsnumber numberwithint:0]                                                                 forkey:cbcentralmanageroptionshowpoweralertkey]];      if (cbmanager.state==cbcentralmanagerstatepoweredoff)     {         //do stuff     }  - (void)centralmanagerdidupdatestate:(cbcentralmanager *)central {     nsstring *statestring = nil;     switch(bluetoothmanager.state)     {         case cbcentralmanagerstateresetting: statestring = @"the connection system service momentarily lost, update imminent."; break;         case cbcentralmanagerstateunsupported: statestring = @"the platform doesn't support bluetooth low energy."; break;         case cbcentralmanagerstateunauthorized: statestring = @"the app not authorized use bluetooth low energy."; break;         case cbcentralmanagerstatepoweredoff: statestring = @"bluetooth powered off."; break;         case cbcentralmanagerstatepoweredon: statestring = @"bluetooth powered on , available use."; break;         default: statestring = @"state unknown, update imminent."; break;     }     uialertview *alert = [[[uialertview alloc] initwithtitle:@"bluetooth state"                                                      message:statestring                                                     delegate:nil                                            cancelbuttontitle:@"okay" otherbuttontitlearray:nil] autorelease];     [alert show]; } 

the problem state of manager unknown regardless if bluetooth on device turned on or not. ideas on why happens ?

two things -

  1. your cbcentralmanager should property, otherwise released method initialise in exits

  2. you shouldn't call scanforperipheralswithservices until in powered on state.

    @property (strong,nonatomic) cbcentralmanager *cbmanager;    self.cbmanager = [[cbcentralmanager alloc] initwithdelegate:self                                                                queue:nil                                                              options:                            [nsdictionary dictionarywithobject:[nsnumber numberwithint:0]                                                                forkey:cbcentralmanageroptionshowpoweralertkey]];   - (void)centralmanagerdidupdatestate:(cbcentralmanager *)central  {     nsstring *statestring = nil;     switch(central.state)     {         case cbcentralmanagerstateresetting:             statestring = @"the connection system service momentarily lost, update imminent.";             break;         case cbcentralmanagerstateunsupported:             statestring = @"the platform doesn't support bluetooth low energy.";             break;         case cbcentralmanagerstateunauthorized:             statestring = @"the app not authorized use bluetooth low energy.";             break;         case cbcentralmanagerstatepoweredoff:             statestring = @"bluetooth powered off.";             break;         case cbcentralmanagerstatepoweredon:             statestring = @"bluetooth powered on , available use.";            [central scanforperipheralswithservices:nil options:                             [nsdictionary dictionarywithobject:[nsnumber numberwithint:0]                                                         forkey:cbcentralmanageroptionshowpoweralertkey]];             break;         default:             statestring = @"state unknown, update imminent.";             break;     }     uialertview *alert = [[[uialertview alloc] initwithtitle:@"bluetooth state"                                                      message:statestring                                                     delegate:nil                                            cancelbuttontitle:@"okay" otherbuttontitlearray:nil] autorelease];     [alert show]; } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -