peripheral didUpdateValueForCharacteristic:xcode

Discussion in 'iOS Programming' started by gbenna, Mar 24, 2016.

  1. gbenna, Mar 24, 2016
    Last edited by a moderator: Mar 24, 2016

    gbenna macrumors member

    Jul 27, 2011
    I have an app that allows people to play a game between two iOS devices using bluetoothle. I have everything working. One user taps on a my turn button and the peripheral (their device) starts advertising and the other person taps a button that starts the central to start scanning on their device. When player one moves it shows up on player two's device. When player one is done they tap a your turn button and their device stops advertising and starts to scan and lets player two it is their turn. Then player two taps the my turn button and starts to advertise.

    the code I am using has this flow

    1. central scan for peripheral
    2. found peripheral connect.
    3. stop scan
    4. discover peripheral services
    5. discover characteristic
    6. get the data from the peripheral

    here is where the problem lies. in the
    peripheral didUpdateValueForCharacteristic: once the data is gotten the central disconnects from the peripheral using the code

    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {if (error) {
    NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
    return; }
    [self.textview2setText:[[NSStringalloc] initWithData:self.dataencoding:NSUTF8StringEncoding]];
    NSString *stringFromData = [[NSStringalloc] initWithData:characteristic3.valueencoding:NSUTF8StringEncoding];
    // Have we got everything we need?
    if ([stringFromData isEqualToString:textview2.text]) {
    // We have, so show the data,
    self.textview2.text = @"";
    // Cancel our subscription to the characteristic
    [peripheral setNotifyValue:NOforCharacteristic:characteristic3];
    // and disconnect from the peripehral
    which means the central needs to resubscribe to the peripheral and get the characteristic data to update it.
    this causes the connection sometimes to disconnect and then take up to 30 seconds to a minute to reconnect.

    I know the peripheral is constantly updating the data to be sent but how do i update the value for the characteristic without unsubscribing and resubscribing.
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Once the devices are connected, you should not be disconnecting until one of the gamers initiates it, or the devices go out of bluetooth reach. So why does it appear your code is causing a disconnection.

    // and disconnect from the peripehral
    Your connection should be able to handle two way communication once they are connected. I don't know of any web sources that explain this. My memory of the details is foggy since it's been a few years since I wrote something similar. :(
  3. gbenna thread starter macrumors member

    Jul 27, 2011
    You are correct. I think this came from a project where I wanted both devices to communicate over bluetooth at the same time some each would connect, get info and then disconnect. Once I removed this I no longer have a problem thanks.

Share This Page