Failed to retrieve services of a CBPeripheral after restart the peripheral - ios

I have a problem while trying to reconnect with a paired peripheral, it connects but I can not find the services.
The steps are as follows.
Turn on the peripheral device and open app.
Find the peripheral after scan. OK
Connect to the peripheral. OK
Save peripheral id for future reconnection.
Find the services / Characteristics. OK
Now I disabled the bluetooth from IOS and turned it on again to test the reconnection.
Find the peripheral after scan. OK
Connect to the peripheral with saved id. OK
Find the services / Characteristics. OK
Now I closed the app and reopen to test again.
Find the peripheral after scan. OK
Connect to the peripheral with saved id. OK
Find the services / Characteristics. OK
Now turn off the peripheral and then turn on again.
Find the peripheral after scan. OK
Connect to the peripheral with saved id. OK
Find the services / Characteristics. DOES NOT WORK NEVER MORE.
After step 4 if i repeat steps 1,2 and 3 they will not find services, the problem is when I restart the peripheral.
I'm setting the self.manager.delegate=self and implementing CBCentralManagerDelegate and CBPeripheralDelegate correctly, and I'm using self.manager.retrievePeripherals (withIdentifiers: [UUID]) to connect to the paired peripheral.
Any idea? I will be very grateful.

Related

central unsubscribes from characteristic when restart app

IOS app is a peripheral. After pairing, peripheral can successfully update characteristic value to and indicate to subscribed central via updateValue:forCharacteristic:onSubscribedCentrals:
When I call this method successfully (after pairing), logs show that characteristic had 1 subscribed central, via characteristic.subscribedCentrals.
However, If I use Xcode to stop and start the app again and try to update characteristic value, logs show that characteristic now has 0 subscribed centrals, and central device doesnt do expected behavior so I'm assuming it wasnt received.
How I make sure the central remains subscribed to my services characteristic after reboot/reconnect?
Thanks
I believe that the only way you can do this is to create a bond between the devices. Usually, once the app is killed/restarted, the value of the CCCD (Client Characteristic Configuration Descriptor) is reset and therefore the remote central app needs to re-enable it every time upon connection. However, when the two devices bond, the GATT server stores the information on which CCCDs are enabled, and then upon each subsequent connection those CCCD values are loaded. You can find more information here:-
What does CCCD mean
How to know if the notification/indication of a characteristic is enabled
GATT (Services and Characteristics) - check attribute caching
The ultimate guide to iOS's CoreBluetooth - check Pairing and Bonding

How to clone BLE device (configurations,charcterstics etc)and share it with other

I am working on an app that gets data from the BLE device, I am able to code to get the app to connect with the BLE device. But further characteristics read is an issue as I don't physically have the device
i came across an app named nRF Connect for Mobile . It has the functionality to clone BLE if paired. the video for a BLE clone is shown here but export and import is not avaible
Is there any way I can get the BLE cloned say some configuration file, which then imported in certain app create then same BLE device .i can use to read all the characteristics and other properties
Why don't you just connect to the device in for example nRF Connect, write down the services, characteristics and descriptors you see (usually isn't that many). Then just set up a local GATT server with the same content.
If I understood your question and your video correctly, you would like to copy the advertisement data of a BLE peripheral and use it to advertise on an iOS device yourself.
Maybe CBPeripheralManager can help you do that.
You can call startAdvertising([String:Any]?) and advertise a BLE peripheral.
But iOS limits on a system level which keys you can advertise.
From a short look in the documentation, it seems only the keys
CBAdvertisementDataLocalNameKey
CBAdvertisementDataServiceUUIDsKey
are supported.
But if you'd want to include more information about your peripheral, you could still create an own CBMutableService "deviceInfo" and send information within a characteristic after having established a connection.

How long central manager scan for peripherals?

I start working with CoreBluetooth for a week, but I dont know how to reconnect to the peripheral after losing connection.
For example, my CBCentralManager connect to peripheral A, the real manager on iPhone said 'Connected', then I go away for awhile, the CBCentralManager lost connection to the peripheral A, the real manager on iPhone said 'Not Connected'. Then I go back, the CBCentralManager doesn't reconnect to peripheral A, but the real manager on iPhone said 'Connected'.
How can I make my CBCentralManager reconnect automatically?
Sorry for my bad English.

iOS CoreBluetooth and preventing other devices to connect to your peripheral

I am creating a core bluetooth application and connecting to a peripheral device, is there a way for the peripheral to reject which centrals may connect to it? What if a random person scanned and found my peripheral devices broadcasted UUID and then broadcasted that UUID and tried to connect to it, how would I prevent this?
No, the iOS peripheral cannot prohibit centrals from connecting to it. However it has some tricks to disable the connection. When a dynamic characteristic is read:
return an error instead of the value of the characteristic,
don't respond to the request. This will cause the connection to stall and then break up after about 30 seconds.
Think of the advertisement as a real advertisement in the media. As many can see it as want. This is the same for iBeacons. You shouldn't rely any security on being hidden. (Wifi SSID broadcasting can be turned off but if your hotspot is not encrypted and authenticated, people with find you.)

What, exactly, is a BLE connected device on iOS?

The CBCentralManager retrieveConnectedPeripherals method says it gets "the list of the peripherals currently connected to the system." The definition of system is a bit ambiguous here.
Does this mean I get a list of peripherals connected to my app, or a list of peripherals connected to any app?
If I can get peripherals connected to another app, does this also mean multiple apps can connect to the same peripheral?
Can this only happen if the app is in the foreground, or do I need to allow for the possibility that a background app is sending commands to a peripheral I think my app owns?
BLE 4.0 enabled devices are usually connected with CBCentralManager unboundly. What is bounded and unbounded connection, you can know from this link.
One BLE 4.0 enabled device can be connected with only one master device thus enhancing secured connectivity. You should follow this link.
Whether in foreground of background, if the connection exists, the BLE slave device won't get connected with another app or device.

Resources