delegate of iOS paired device reconnection - ios

I have an application which is using retrieveConnectedPeripheralsWithServices to get the list of paired HID devices. I can get the list and connect to the desired one successfully using connectPeripheral code. When the connection succeeds, following delegate is being called:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
and for disconnection, I get the corresponded delegate:
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
Now when the HID device gets connected again to the iOS (directly from Setting, not from my application), I want to be informed. Is there any way to find out when an HID device reconnected to the OS?

Finally, I found the answer.
Thanks to #Paulw11 for his guidance, based on Apple Documentation:
When the user leaves home, the iOS device may eventually become out of range of the lock, causing the connection to the lock to be lost. At this point, the app can simply call the connectPeripheral:options: method of the CBCentralManager class, and because connection requests do not time out, the iOS device will reconnect when the user returns home.

Related

Connect to Peripheral of Bluetooth LE device from OSX app

I'm trying to connect to Peripherals of a BlueTooth LE device with a simple ( very similar to an Hello World ) OSX App.
I'm following Apple's Guide Lines , but when i try to connect to a Peripheral my app does not work as expected.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(#"Discovered %#", peripheral.name);
if([peripheral.name isEqualToString:#"BLE-DEVICE"]){
NSLog(#"Found BLE Device!");
[_centralManager stopScan];
NSLog(#"Scanning stopped");
peripheral.delegate = self;
[_centralManager connectPeripheral:peripheral options:nil];
}
}
The problem is that connectPeripheral (last line) does not trigger the centralManager:didConnectPeripheral method of the delegate object, but if i run a step by step debug with a simple break point it does.
Should i add some other scan options? How can i check if connection is rightly performed?
Try to wait for the event that tells you scanning has indeed stopped. Then, connect to the Peripheral. Maybe the iOS BLE stack doesn't have enough time to stop the scanning and when it tries to connect it fails because it has not yet reached "idle" state.
That explains why it works when debugging step by step: after stopScan is executed and before you manually execute connectPeripheral, there's enough time for the LE Controller to process the first command.
Although in a normal stack architecture, the messages should be queued.
EDIT: Alternatively, add a short delay of a few milliseconds between the two stack calls.

Bluetooth LE notifications not received on iOS

We are working on a bluetooth project using cordova as a xplatform development tool.
We are developing for android and iOS and writing a BLE plugin for cordova ourselves.
On Android everything is running fine. Now comes the problem:
On iOS we are able to read/write and even subscribe to notifications.
The problem comes when we try to receive a updated value, then nothing happens.
In short
- connect to device
- discover services
- discover characterics
- subscribe to notification characteristic
this is working well:
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
is getting called, and characteristic.isNotifying is true
When doing a simple read operation on the characteristics, the
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
callback is being called.
Here comes the problem:
When updating the value from the peripheral, the callback simply does not get called.
We have no clue were to look since no errors occur at all, so maybe somebody knows a way to debug this issue?
Note: When connecting to the peripheral using a external tool, subscribing to the characteristic and updating it's value. The notification is received. It's iOS specific.
Ok the problem was:
We were calling [peripheral setNotificaiton:etc] on the temporary peripheral received by the callback.
When we are setting the notification on the onConnected stored peripheral object, notifications are received. (self.peripheral)
No this was not the answer, it was working because we restarted the iOS device. It looks like there is some sort of bug in this crappy apple thing.
Does anybody know how to restart bluetooth programmatically?

Out of range message and auto-reconnect - BLE Device - IOS

I am currently working on an app that gets data from a BLE device – similar to a heart monitor. The app reads the data from the device and then when it gets a specific amount of data, it creates a .csv file and uploads it into a server. Everything works fine, except when the device gets out of the range. The app just stop receiving data and doesn’t recognize that the connection is lost. I don’t get any error message. The app just stops in the middle of the “getting data” loop and keeps waiting for a data that never comes. When the device is back in the range, nothing happens.
I would like to show an alert informing that the BLE device is out of the range. When the device is in the range again, the app should reconnect to it automatically and then continue reading data from the device. How can I implement that? I tried to get the CM state – using the function below – but it didn’t work.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
printf("Status of CoreBluetooth central manager changed %d (%s)\r\n",central.state,[self centralManagerStateToString:central.state]);
}
I’ve even tried to add an if clause inside the loop to check the device state, but it didn’t work also.
I am using the Texas instruments chip CC2540.
As suggested by Paulw11 and SJoshi (Thank you guys), I had to implement the didDisconnectPeripheral method. So, here is how I did it:
.h file:
// will be invoked once disconnected
-(void)centralManager:(CBCentralManager *)central
didDisconnectPeripheral:(CBPeripheral *)peripheral
error:(NSError *)error;
.m file:
-(void) centralManager:(CBCentralManager *)central
didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
[central connectPeripheral:peripheral options:nil];
// you can add whatever you want here.
// will execute when the peripheral loose its connection.
}

Reconnect to a BLE device after pulling the battery

I have a BLE device that I am writing an app to pair with. I can discover and connect to the device with no problems. But if I am connected and pull and reinsert the battery on the BLE device I get the didDisconnectPeripheral callback but I never get another didConnectPeripheral even though I'm still scanning. I also tried calling retrieveConnectedPeripheralsWithServices and retrievePeripheralsWithIdentifiers but neither of those return anything.
How can I reliably reconnect after cycling the power on my BLE device?
As soon as the peripheral disconnects you can issue another connect - iOS will automatically reconnect to the device once it is visible again and call your didConnectPeripheral: delegate method
-(void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(#"Disconnected from peripheral");
[central connectPeripheral:peripheral options:nil];
}
There is no need to rescan/re-discover the peripheral.
You may want a more comprehensive implementation that updates UI etc.
Here is some sample code that connects to a peripheral and displays the vendor information -
https://github.com/paulw11/BTBackground

Can iOS re-connect to Bluetooth LE peripheral by specifying )CBPeripheral *)peripheral instead of doing retrievePeripherals?

When I get delegate didConnectPeripheral:(CBPeripheral *)peripheral
can I just store peripheral in an array, and then use it to re-connect later, instead of using retrievePeripherals and its subsequent didRetrievePeripherals?
Seems like it would be easier, if it's feasible and has no risk.
How much later can (CBPeripheral *)peripheral be re-used? Is it still valid after disconnection with that peripheral?
Workflow:
scanForPeripheralsWithServices() - to scan for a peripheral
didDiscoverPeripheral:(CBPeripheral *)peripheral - when it's detected
connectPeripheral:peripheral
didConnectPeripheral:(CBPeripheral *)peripheral stopScan and store the (CBPeripheral *)peripheral for later.
... read or write characteristics ...
cancelPeripheralConnection
didDisconnectPeripheral
LATER, TO RE-CONNECT...
connectPeripheral:peripheral - from array with peripheral
didConnectPeripheral:(CBPeripheral *)peripheral
...
YES, it will work (but it's terrible practice). The retrievePeripherals: method was specifically created so that you can reconnect to peripherals between subsequent launches of an application. You can use your method, but once the app is shutdown, you will never be able to connect to the peripheral again (without putting it into advertising mode and starting from scratch basically). You can store the uuid between launches, but you cannot store a CBPeripheral object. So there's the big downside right there.
So to sum up: it will work, but it doesn't really gain you anything. It is not faster than calling retrievePeripherals: and then connecting them. Your suggested method is only limiting your abilities for connections in CoreBluetooth. But an interesting question nonetheless.

Resources