Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
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


Code:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{if (error) {

NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
return; }

[self.dataappendData:characteristic3.value];

[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

[self.centralManagercancelPeripheralConnection:peripheral];

}

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.
 
Last edited by a moderator:

xStep

macrumors 68020
Jan 28, 2003
2,031
143
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.

Code:
// and disconnect from the peripehral
[self.centralManagercancelPeripheralConnection:peripheral];

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. :(
 
  • Like
Reactions: firewood

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.