Hi I am looking to see if I can send a notification when my iPhone connects to a bluetooth device with my app running in the background. This is similar to what happens with an iBeacon in iOS 7 I am just wondering if this can be done with any bluetooth device. I am ok with using private API's if necessary. If I understand bluetooth correctly, any bluetooth device has to at least communicate its identifier and wait and receive pairing codes so I am hoping i can be able to determine that if a bluetooth device broadcasts an identifier and my phone connects to it then send a notification like an iBeacon.
I have used an Estimote and it can broadcast the proximity and send notifications accordingly, I just want to be able to send a notification when a bluetooth connection has been made from any bluetooth device.
Thank you
On Apple's documentation you can find that you can act as a Bluetooth Central in the background. You should get callbacks if you are scanning for the correct UUID.
I haven't tested this but check that you have the correct UIBackgroundMode key in the info.plist file.
Also your CBCentralManagerDelegate should call the selector centralManager:didDiscoverPeripheral:advertisementData:RSSI: while your app is in the background.
Related
I have a problem with the lifecycle of Bluetooth on iOS.
We are using React Native and the following library (https://github.com/innoveit/react-native-ble-manager) to manage our Bluetooth devices.
The devices sends data and clears its local data when it has sent all its data through bluetooth (we are only using ble messages ACK, no internal app ACK).
The problem is, that iOS seems to maintain the ble open when the app is killed, which is making the device think that it is still sending data that is processed correctly by our app, and clears it at the end (which causes data loss).
Is this a normal behaviour on iOS ? (the fact that the ble stays open for some time after the app is killed, and no errors are sent through ble ?)
Is there a way to force close the ble when the app is killed ?
Thanks
Is this a normal behaviour on iOS ? (the fact that the ble stays open for some time after the app is killed, and no errors are sent through ble ?)
Yes, this is normal. The OS holds open the connection and shares it with apps. The apps do not hold connections directly. It can take some time (generally on the order of seconds, but sometimes a bit longer) for the OS to drop the connection after the last app releases it.
Is there a way to force close the ble when the app is killed ?
No. There is no way to do anything when the app is killed (by which I assume you for force-quit, for example by swiping up on the app switcher).
All that said, I'm a bit surprised here that you're getting a write response back to the device if the app is no longer reading. When you say "ble messages ACK," specifically which "ack" do you mean? Are you getting a Write Response packet back? Or do you mean a lower-level ack?
I just want to figure out. If i have the BLE(cc2541) and android APP. BLE has characteristics for notification, read, write and other. Can i write value to notification characteristics from BLE, read it with android APP, and then write to same notification characteristics from android APP to BLE?
It all depends on the firmware in the device.
I have a cc2541 with jnhuamao firmware and get notifications and read and write to the device with the same characteristic.
The service has Uuid 0000ffe0-0000-1000-8000-00805f9b34fb called SimpleKeyService
The characteristic has Uuid 0000ffe1-0000-1000-8000-00805f9b34fb with the properties Read, WriteWithoutResponse and Notify.
When I connect to a BLE peripheral using encryption on iOS 9.1 using a iPhone 6S the BLE peripheral is immediately disconnected. I've verified that this works on iOS 8 and 9.02. I've validated that the CBCentralManager didConnect delegate method is being called and a valid CBPeripheral object is being returned.
The BLE peripheral is disconnected immediately. I'm not able to even proceed to discover any services or characteristics before it is disconnected. The CBCentralManager didDisconnect method is called, but no error is returned.
Is anyone else seeing this? This has been working correctly for over a year and like I said it's only happening when using iOS 9.1 on the iPhone 6S. Don't have any other peripherals on 9.1 to test with.
UPDATE
According to Apple the issue is happening because we're not bonding the peripheral with the iPhone. So we've updated the firmware on the peripheral and are now bonding. This is presenting different issues surrounding maintaining bonding pairs between the phone and the peripheral. Our peripheral has a limited amount of memory, so we cannot maintain an infinite amount of bond references. To handle this scenario the peripheral allows for a maximum of 8 BLE bonds to be stored. When it receives a ninth request the first bond is removed from the peripherals list of bonded devices. This presents an issue because the iPhone doesn't know this bond has been removed and when I try to connect with the first device and interact with a characteristic I'm not prompted to pair. Which from my understanding is what kicks off the encryption process.
I'm wondering if there is anyway to remove a bond / device from an iPhones list of bonded devices, other then going into the Bluetooth settings and 'Forgetting' the device.
UPDATE #2
Apple has confirmed that there is NOT a way to remove a bonded peripheral from the 'MY Devices' section within the Bluetooth settings in the application due to security issues.
Based on this the scenario has now turned into the following. The iOS device has a reference to what was once a bonded peripheral under "My Devices" however that peripheral no longer has a reference to the iOS device within it's bonded devices table.
When I attempt to connect the iOS device, which still has the bond reference in it, to the peripheral that no longer has the bond reference we can connect successfully. However, after I connect successfully I discover the characteristic, set it to notify and receive no response and no pairing message. The only solution that fixes this issue is going into 'Settings' and 'Forgetting' the device and then attempting to reconnect.
Has anyone else experience this issue? Suggestions?
Thanks!
I'm writing an app using CoreBluetooth in iOS8 that detects and connects to other phones using the app. I'm trying to figure out the best way to be able to create a connection while both apps are backgrounded, but have been having difficulty in the scenario where both phones are backgrounded + locked. I've followed the suggestions of this post:
How to detect nearby devices with Bluetooth LE in iOS 7.1 both in background and foreground?
I haven't been able to establish a connection when both phones are backgrounded + locked, but I've been able to get a connection where the peripheral and central are backgrounded + locked and I then unlock the central (central still in the background). When I unlock the peripheral though nothing happens. It also works when they are backgrounded but not locked.
So in order to allow a connection to be established when I unlock the "peripheral", I decided to create a central and peripheral on both phones to simultaneously scan and advertise. Whichever discovers the other first will then stop scanning/advertising and connect. I've run into problems with this method that are detailed in the following two questions:
Peripheral and central at the same time on iOS
iOS CoreBluetooth Simultaneous Peripheral and Central Managers
Basically, I think that when both of my devices try to connect to each other simultaneously, they get hung up on attempting to connect and didConnectPeripheral is never called on either of them (the last thing both of them displays is a message saying that they are attempting to connect). It has worked occasionally though so I'm also a little confused as to why there is a probabilistic nature to the connection. Does anyone have any suggestions on how to approach the problem? If there's a better way to establish a connection when both apps are backgrounded + locked, please let me know because that was my original problem.
Thanks!
Declare bluetooth-central and bluetooth-peripheral background modes in your Info.plist to act in both Central and Peripheral roles in background.
I have implement Apple Push Notification in my application.
It's working fine when my iPhone is on.
Now, when I switch off my iPhone and fire notification from the server, it's successfully sent.
but, When I switch on my iPhone, I am not getting any notification.
So, what is the problem ?
Apple Push Notification service includes a default Quality of Service
(QoS) component that performs a store-and-forward function.
If APNs attempts to deliver a notification but the device is offline,
the notification is stored for a limited period of time, and delivered
to the device when it becomes available.
Only one recent notification for a particular application is stored.
If multiple notifications are sent while the device is offline, each
new notification causes the prior notification to be discarded. This
behavior of keeping only the newest notification is referred to as
coalescing notifications.
If the device remains offline for a long time, any notifications that
were being stored for it are discarded.