如何在iOS注销后搜索BLE设备?

时间:2015-07-08 07:13:24

标签: ios iphone

在我的情况下,搜索视图是一个控制器,注销是在另一个视图控制器中,我如何断开蓝牙和注销。一旦我注销设备不会忘记它并保持连接到我的设备。如何搜索蓝色设备我的app afterlogout?请帮帮我。谢谢你提前

    - (void)centralManagerDidUpdateState:(CBCentralManager *)central
{

    NSLog(@"%s \n",__PRETTY_FUNCTION__);
    // Determine the state of the peripheral
    if ([central state] == CBCentralManagerStatePoweredOff) {
        NSLog(@"CoreBluetooth BLE hardware is powered off");


    }
    else if ([central state] == CBCentralManagerStatePoweredOn) {
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
        [mgr scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey: @YES }];

        return;
    }
    else if ([central state] == CBCentralManagerStateUnauthorized) {
        NSLog(@"CoreBluetooth BLE state is unauthorized");

    }
    else if ([central state] == CBCentralManagerStateUnknown) {
        NSLog(@"CoreBluetooth BLE state is unknown");


    }
    else if ([central state] == CBCentralManagerStateUnsupported) {
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");

    }

}

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    {

        NSLog(@"I see an advertisement with identifer: %@, state: %ld name: %@, services: %@, description: %@",[peripheral identifier],(long)[peripheral state],[peripheral name],[peripheral services],[advertisementData description]);
        // Name of the device
        NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];

        NSLog(@"The name get from direct periperal --> %@",localName);

        if (![localName length] > 0) {
            NSLog(@"localName is null ");

        }
        else
        {
        if (peripheral != (id)[NSNull null] && peripheral.name != (id)[NSNull null])
        {
            NSLog(@"-----> Clear Check Point 1 <------");
            if (localName==nil )
            {
                NSLog(@"-----> Clear Check Point 2 <------");
                localName=peripheral.name;
                NSLog(@"The name get if we get nil from periperal name --> %@",localName);
            }


            if (peripheral == nil || peripheral.name == (id)[NSNull null])
            {
                NSLog(@"-----> Clear Check Point 3 <------");
            }


                [peripheralsarray addObject:peripheral];
                [peripheralnamesarray addObject:localName];


                NSOrderedSet *mySet = [[NSOrderedSet alloc] initWithArray:peripheralnamesarray];
                peripheralnamesarray = [[NSMutableArray alloc] initWithArray:[mySet array]];

                NSOrderedSet *mySet2 = [[NSOrderedSet alloc] initWithArray:peripheralsarray];
                peripheralsarray = [[NSMutableArray alloc] initWithArray:[mySet2 array]];
                [mgr stopScan];


            }
        }
    }
}



- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { 
    NSLog(@"%s \n",PRETTY_FUNCTION); 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
    appDelegate.isBLE_Connected = FALSE; 
    [mgr cancelPeripheralConnection:activeperipheral]; 
    UIAlertView *disconnALert = [[UIAlertView alloc] initWithTitle:@"" message:@"No device is paired" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; 
    [disconnALert show];
 }

0 个答案:

没有答案