I'm building a bluetooth app using CoreBluetooth on iOS. Every time an app is launch user receives an ID, which is saved in Peripheral's LocalNameKey, begin to advertise with it and start to search for other users using CentralManager. Every user is identified by his local name and that's work fine.
Using CentralManager every user can write a value to another's user Peripheral's characteristics, and notify them about the change. That is also working. Problem occurs here, after the connection is done and Peripheral executed didRecieveWriteRequests method, both CBPeripheralManager and CBCentralMange are being reseted and reinitialised. After that Peripheral's LocalNameKey is no longer my specific ID, but an iPhone's Given Name or (null). It ruins the whole idea of the app, but I'm pretty sure it can be done.
It works again, when I turn off and on bluetooth.
This how I cleanup Central after connection:
- (void)cleanup
{
// See if we are subscribed to a characteristic on the peripheral
if (self.peripheral.services != nil) {
for (CBService *service in self.peripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if (characteristic.isNotifying) {
// It is notifying, so unsubscribe
[self.peripheral setNotifyValue:NO forCharacteristic:characteristic];
// And we're done.
return;
}
}
}
}
}
// If we've got this far, we're connected, but we're not subscribed, so we just disconnect
[self.manager cancelPeripheralConnection:self.peripheral];
}
This is how I reinitialise bluetooth's central and peripheral:
self.bluetoothPeripheral = nil;
self.bluetoothCentral = nil;
self.bluetoothPeripheral = [[BluetoothPeripheral alloc] initWithID:idNumber];
[self.bluetoothPeripheral loadManager];
self.bluetoothCentral = [[BluetoothCentral alloc] init];