General

Connecting to a Bluno using CoreBluetooth ...connects then disconnects

userHead ipatch 2014-01-31 13:29:15 2718 Views0 Replies
Hello, I currently have the Bluno v1.6 and I am trying to connect to the device via BLE using CoreBluetooth provided by Apple. My app attemps to connect to the device for a second or two then disconnects. I am not sure why the device is doing this but the Bluno has no problem connecting to the app LightBlue, and staying connected.

My code is as follows,

ViewControllerDev2.m
Code: Select all
- (void)viewDidLoad {
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil]; // options:nil is an iOS 7 feature
}

// this method is called when creating a CBCentralManager, and is required!
// this method ensures that BLE is supported and available to use on the central device.
// the protocol allows the delegate to monitor the discovery, connectivity, and retrieval of peripheral devices.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    // You should test all scenarios
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }
    if (central.state == CBCentralManagerStatePoweredOn) {
        // Scan for devices
        [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:BLUNO_TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
        NSLog(@"Scanning started");
    }
}

// this method is called after the central manager discovers a peripheral.
// "Any peripheral that is discovered is returned as a CBPeripheral object"
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    
    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
    if (_discoveredPeripheral != peripheral) {
        // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
        _discoveredPeripheral = peripheral;
        // And connect
        NSLog(@"Connecting to peripheral %@", peripheral);
        [_centralManager connectPeripheral:peripheral options:nil];
        
        // then stop scanning for peripherals
        [_centralManager stopScan];
        NSLog(@"Scanning stopped");
    }
}

// if the connection request is successful, the central manager calls the following method of its delegate object.
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"Peripheral Connected");
    [_centralManager stopScan];
    NSLog(@"Scanning stopped");
    peripheral.delegate = self;
    [_data setLength:0];
    // discover the services offered by the peripheral
    [peripheral discoverServices:@[[CBUUID UUIDWithString:BLUNO_TRANSFER_SERVICE_UUID]]];
}

// this method is called once a service of a peripheral is discovered.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:BLUNO_TRANSFER_CHARACTERISTIC_UUID]] forService:service];
    }
    // Discover other characteristics
}