Perform an action when a bluetooth device connects or disconnects on CoreBluetooth - swift

I am in process of making an app where, where when the first time app is launched the user enter the device they want to connect. Once connected, the user leaves/closes the app.
Now every time that same bluetooth device is connected or disconnected, I want a function to be called in the app. How can I achieve this?
I am using Corebluetooth currently to get names of devices etc. but cannot find a way to achieve this. Any suggestions? TIA!

Related

cordova-plugin-ble-central autoconnect function always prompting user to pair the device

I'm currently working on an ionic capacitor app that needs to connect automatically with a ble device.
I'm using the ble ionic native plugin:
https://ionicframework.com/docs/native/ble
https://github.com/don/cordova-plugin-ble-central
The goal is sending and receiving bluetooth info and commands to the ble device, which is a car alarm central that controls a few actions like lock and unlock the doors accordingly to the RSSI values on from the device.
So, I'm using the autoconnect function to auto-connect with the device as soon the device is close to range and all works perfectly in Android and iOS. The only issue is that everytime the device reachs the ble range and the ble plugin try connecting, the system prompts the user to accept pairing with the device again.
I need it to work without that pairing prompting everytime. I tried to pairing the device previous to initialize the app but it didn't worked as well. So is it a way to make this autoconnection work without prompting the pairing acceptance everytime it is disconnected?
The issue occurs on Android versions 9 and 10 and iOS.
I've changed my ble hardware interface to ask for a PIN code. I guessed the connection could be considered secure by the OS and prevent prompting the user to pair again. Didn't work as well.
Every connection started by the plugin seems like bounding and connecting as if it was the first time connecting to the device. Even connecting to a known device is prompt the user to input the PIN, which if not provided the OS forgets the device.
Any toughts? Anyone? Thank you!

HttpUrlConnection on Android Wear with connected phone

I am trying to access the network with my Android Wear 2.0 App.
The Problem:
If the smartwatch is connected with the phone then I get an connection timeout. But if the smartwatch isnĀ“t connected with the phone then the connection works fine.
I am using HttpUrlConnection.
Thanks and sorry for my bad english!
This has been answered in a different post:
Android Wear 100% packet loss
The basic idea is that you should not count on having Wi-Fi on the watch. The user may disable Wi-Fi on the watch and then your application will not work on the watch. Instead you should use your companion phone app to connect to the internet. If the phone isn't connected to your watch then use Wi-Fi.
Even if you want your app to run on an LET enabled watch the user may have turned this off or may not have a SIM card installed. At this point the user will most likely still expect your app to work.
So depending on what it is you're working on, if you want your app to work even while connected to the phone, then make a companion app for the phone. If you want your watch app to be a stand-alone app but you still want to be able to access the internet even when the watch is connected to the phone then you're basically saying that you want to drain the user's battery by using Wi-Fi/LTE instead of going via Bluetooth through the phone to access the web.

iOS 7 Bluetooth - app that handles events in background even after phone restart

I want to write a tracking application, which reacts/record when users get in a car and phone automatically pair with Bluetooth hands free. I need also Id of the hands free device.
On Android it is easy, using Broadcast Receiver your app listen to broadcasts that device had been paired with phone. It is working even after phone had been restarted and App did not run after restart.
Is it possible on iOS(7+) ?
You can do it by reading the "Performing Long-Term Actions in the Background" part of this docs
Essentially, using state restoration, the system takes note of what your app was doing when it was killed.
If the app is searching for a bluetooth device and is killed by the system, the system will take care of continuing this research and wake up the app again when the bt device has ben found.
Note that this will work only if the app is killed by the system, not by the user (using the multitask bar). This behavior has changed since iOS 7.
Alternatively, If your BT hardware can be programmed to send advertisements using the ibeacon protocol, your app can be "awakened" by the corelocation/ibeacon api. After that prompt then maybe the corebluetooth pairing code could be initiated.

Sending a contact from an iOS app via bluetooth

I am trying to allow a user, who is running my app, to send a contacts vcard (vcf) via bluetooth to another iOS device.
The problem is, that the receiving device should not be required to run the same app.
Is there a possibiliy to accomplish this?
If there is not - is it at least possible to have Gamekit to establish a session, when the receiving device is running the same app, but without requiring the (receiving) user to start the peerPicker-dialog?
Any help and hints are appreciated!
First option: seems to be impossible.
Second option: Works. I implemented a GKSessionDelegate for receiving incoming connections. This session is startet together with the app.
For establishing the connection with a receiver, I used the well known GKPeerPickerControllerDelegate.
One remainig problem is, that the sender sees itself in the list of available peers (because it has also a receiver running in the background). My solution for this is to set its GKSession.available=NO, as soon as the sender starts its GKPeerPickerController.

Can an iPhone app send a command (data) through Wi-Fi (or maybe Bluetooth) to a device at a specific time when it is not running through?

Situation: My iPhone communicates to an external device through Wi-Fi. The app has also the ability to set up a schedule. This means that on a specific time the device should turn on and do some stuff. Now for this to happen there should be a timer on the external device or the iPhone should send a command to the device on a specific time.
Question: Is it possible to send a command to the external device while the app is in the background? Or does the app have to be in the foreground? Is it possible when the external device is an accesory? Cause I have read that apps are allowed to run in the background when they need to communicate to accessories??
Thanks in advance!
No you can't do this, you are only able to run an app in the background if its stream audio, a VOIP client or track user location. If you are in the accessories program (which means you can make hardware that works with the dock connector, then your app can also run in the background. but only if the accessories that is respons to is connected.
There is no way the schedule any operation to be triggered at a set time.
You can schedule a local notification, but the user wil have to view (open you app by click in the notification) before you app get informed about it.
Do you actually have the devices communicating? If not, you should take a look at Apple's MFI (Made for iPhone) program. Sending data or communicating from your iPhone to an external accessory through bluetooth falls under it. They provide you with certain protocol specifications that allow you to communicate with external accessories. It's difficult to do something like this if you are an individual developer though. Here's the link: https://developer.apple.com/programs/mfi/.