2

OK, I'm using Core Bluetooth to connect my i-Pad to another BLE sensor. I scan and detect it easily but when I want to establish a connection, the "did connect peripheral" method is only called during 30 sec as if I've called a disconnection... And this result stay the same even I select or deselect "App Communicates using CoreBluetooth" in Required background mode.

- (id) init
{
    self = [super init];
    if (self) 
    {
         pendingInit = YES;
         centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

         foundPeripherals = [[NSMutableArray alloc] init];
         connectedServices = [[NSMutableArray alloc] init];
    }
    return self;
}



- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
     static CBCentralManagerState previousState = -1;
     switch ([centralManager state]) 
     {
         case CBCentralManagerStateUnsupported:
         {
             NSLog(@"NSCentralManager State Unsupported...");
             break;
         }
         case CBCentralManagerStatePoweredOff:
         {
             NSLog(@"NSCentralManager State Powered OFF");
             [self clearDevices];
             [discoverPeripheralDelegate discoveryDidRefresh];

             if (previousState != -1) 
             {
                 [discoverPeripheralDelegate discoveryStatePoweredOff];
             }
             break;
         }

         case CBCentralManagerStateUnauthorized:
         {
             NSLog(@"NSCentralManager State Unauthorized: the application is not allowed");
             break;
         }

         case CBCentralManagerStateUnknown:
         {
             NSLog(@"NSCentralManager state unknown... Bad News...");
             break;
         }

         case CBCentralManagerStatePoweredOn:
         {
             NSLog(@"NSCentralManager State Powered ON!");
             pendingInit = NO;
             [self loadSavedDevices];

             [centralManager retrieveConnectedPeripherals];
             [discoverPeripheralDelegate discoveryDidRefresh];
             break;
         }

         case CBCentralManagerStateResetting:
         {
             NSLog(@"CBCentralManager State Resetting");
             [self clearDevices];
             [discoverPeripheralDelegate discoveryDidRefresh];
             [peripheralDelegate alarmServiceDidReset];

             pendingInit = YES;
             break;
         }
     }
     previousState = [centralManager state];
}


- (void) loadSavedDevices
{
     storedDevices = [[NSArray alloc] initWithObjects:kPressureServiceUUIDString, nil];

     for (id deviceUUIDString in storedDevices) {

          if (![deviceUUIDString isKindOfClass:[NSString class]])
              continue;

          CFUUIDRef uuid = CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);
          if (!uuid)
              continue;

          storedUUIDs = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"StoredUUIDS"]];
          for (int i = 0; i < storedUUIDs.count; i++) {
              NSArray * tempStoredUUIDs = storedUUIDs[i];
              storedUUIDs[i] = [tempStoredUUIDs mutableCopy];
          }

          [centralManager scanForPeripheralsWithServices:nil options:nil];
          storedUUIDs = [[NSMutableArray alloc] init];
          [[NSUserDefaults standardUserDefaults] setObject:storedUUIDs forKey:@"StoredUUIDS"];
      }
} 




- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSString * finaluuids = [NSString stringWithFormat:@"%@",peripheral.UUID];
    if (storedUUIDs.count == 0)
    {
        storedUUIDs = [[NSMutableArray alloc] initWithObjects:finaluuids, nil];
        [centralManager stopScan];
        [centralManager connectPeripheral:peripheral options:nil];
    }
}




- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"connected");
}

Here is my console. I don't have any warning, any error or deallocating just:

2013-05-14 09:21:14.925 PRESSURE[262:907] connected
2013-05-14 09:21:14.927 PRESSURE[262:907] Service (Sensor) connected
2013-05-14 09:21:44.994 PRESSURE[262:907] Service (Sensor) disconnected

How can I avoid my peripheral disconnected immediately after connection please? It drives me crazy!

  • I have an error:Attempted connection to peripheral Sensor disconnected: The specified device has disconnected from us. – user2382658 May 15 '13 at 00:39
  • 1
    Do you use a `CBPeripheral` object that, when connected: `yourPeripheral = peripheral` in the `didConnect` delegate and set its delegate : `yourPeripheral setDelegate:self` (self or another class) ? – Larme May 15 '13 at 10:08
  • 1
    What is the actual BLE device you're connecting to? – C4 - Travis Jul 19 '13 at 18:07

1 Answers1

0

You have to do [peripheral retain] on didDiscoverPeripheral or put it into the retain property. It is released right after the connect by BLE framework.