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
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.
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: