How to get connected/disconnected information for the apps with Xamarin.Android and WearOs? - xamarin.android

I am trying to implement communication between WearOs watch and Android phone Apps. Here is the key point is between Apps, not between Devices because for me important is that, i can get the information about if the watch app is connected with the Phone app and vice versa. But it seems that On Android and WearOs, it happens on device level.
I have implemented WearableListenerService on both side and works perfectly fine. OnCapabilityChanged is fired as well but problem with the OnCapabilityChanged method is only fired if connection is lost and connected again. It is not fired when phone and watch are already connected and when I start the app on the phone, I am not able to know if there is already connection.
OnPeerConnected seems to be a good candidate in this sense but it is deprecated and never fired.
What is the best way to get this information? I have tried also Activity level CapabilityClient.IOnCapabilityChangedListener on MainActivity but OnCapabilityChanged behavior seems to be exactly the same. It is not called when the activity is started.
I have tried to implement code below also in OnResume but it returns always null.
var capabilityInfo = capabilityClient
.GetCapability(capability_wear, CapabilityClient.FilterReachable);
capabilityInfo.AddOnSuccessListener(this);
public async void OnSuccess(Java.Lang.Object capabilityInfo)
{
}
Beside this question, related question, is it even possible to disconnect phone app to watch apps?
I have had similar experience on Tizen watches and There is a SocketService with Connect and Close endpoints implements. Simply can be connected between Apps. But here on WearOs, it seems to happen on Device level only.

What's your definition of "watch app is connected"?
The Node (device) is connected and the app is installed? Or do you mean the app is running.
If you just mean app installed and device connected, then your code should work. Are you using a custom capability that your app declares? That should prove it is your app installed and not just the device connected.
File a bug using a reproduction in the https://github.com/android/wear-os-samples/tree/main/DataLayer project. https://issuetracker.google.com/issues/new?component=1065087&template=1592236
https://developer.android.com/training/wearables/data/data-layer
The CapabilityClient provides information on which Nodes on the Wear OS network support which custom app capabilities.
If you mean connected as in both running simultaneously then you would probably use ChannelClient, MessageClient, or DataClient to send observable updates. But this should generally be careful how you do that as you shouldn't try to maintain a live connection, say using the ChannelClient, between your watch and mobile apps. It will waste battery on both sides and there are hopefully more efficient patterns to achieve whatever you are trying to do.

Related

Appropriate background mode for Swift NIO client

I've created an iOS app, that connects to an embedded device using swift-nio-ssh. This app has no UI to control the device, as that is supposed to be done with a Watch app, using WatchConnectivity.
As it stands, I have to keep the iOS app in the foreground or the SSH client will die after a few seconds. Instead I'd like to be able to keep the proxy app running in background, screen locked or whatever else the user wants to do with their iPhone.
I've done research on background modes, but none of the available APIs seem to fit my use case.
Have I overlooked something?
If not, would I be able to abuse another API instead (Audio, VoIP or NetworkExtension from what I've gathered)?
If so, would the app still be able to pass the review process somehow?
Thanks!
You haven't overlooked something and this applies to everything, not just SwiftNIO-using apps. On iOS, if an app goes into the background then it will be stopped and network connections will die at some point.
The only way around that are the mainly VoIP exceptions you mention in (2) which you have to specifically request from Apple.

Is it possible to interact with ios devices even though app killed?

Is it possible to create an app where the message is automatically sent from one device to another when both the devices are in the same geo-location in predefined range or in the wireless points like Bluetooth?
I think there must be some way to do this. Please let me know if you have any idea about the same.
In peer-peer connection, It is not possible. When the user terminates the app everything goes with it.
But if you connect it with server, you could try to implement the behavior you are looking for with a push notification with content available, which gives you some time awake to download content in the background.

CoreBluetooth: detect device out of range/ connection timeout

I am designing an iOS framework to handle multiple BLE devices (all of the same kind). Everything is working very well at the moment, except one thing:
The client wants a list with available devices. But how can I detect when a device, that has been discovered in the past, is not available anymore?
Another problem occurs, when I try to connect to a device that is not available anymore. Documentation says: Connection attempts never time out and
And yes, I never get an error via didFailToConnectPeripheral.
I did some research but couldn't figure out how handle these problems via CoreBluetooth properly. So I developed my own solutions, but I am not sure if that is the right way (or at least a good way, cause there may be several ways to do it).
1. Detecting devices that are not available anymore
I scan with
[_centralManager scanForPeripheralsWithServices:services options:#{CBCentralManagerScanOptionAllowDuplicatesKey: #(TRUE)}];
so I receive advertisments all the time as long as a device is not connected. I check with a timer that the advertisement reoccured in a given time interval (large enough corresponding to the devices ad interval). If the advertisement didn't occur in the interval, I remove the device from the list.
2. Detecting connection timeout
Well, that's a pretty easy one I think. I use my own timeout function and cancel the connection request if the timer expires.
If somebody ever came across these problem, I would be very interested in your opinion and/or your solution of course.
UPDATE 2014-12-17:
In the meantime I worked on my own solution using timers and it seems to work pretty well.
Connection timeout is straight forward. Simply set a timer to 5 seconds or whatever you think is good for you. If the timer expires and the device did not connect, simply cancel the connection and tell the user that there was a problem.
Detecting devices that go out of range was a bit trickier. For every discovered device I start a timer that fires after double the time, the device sends advertisements. If the device does send another advertisement till the timer expires, it probably went out of range or was turned off or connected to another device.
I don't want to answer my own question because I hope that maybe Apple will one day take care of those problems.
The correct way to determine whether a device is available is to store the peripheral identifier value. Before you attempt to reconnect, call retrievePeripheralsWithIdentifiers. However, this still does not guarantee that the device will be in range by the time you attempt to connect!
Connection attempts do not time out at the OS level, and this is explicitly documented.
Some apps may need to use the Core Bluetooth framework to perform
long-term actions in the background. As an example, imagine you are
developing a home security app for an iOS device that communicates
with a door lock (equipped with Bluetooth low energy technology). The
app and the lock interact to automatically lock the door when the user
leaves home and unlock the door when the user returns—all while the
app is in the background. 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.

Opening an App (iPhone) via bluetooth low energy

Hello I do hope someone can help me with this question as i thought it would be easy at first!
I'm a studying EE student who is not that good with code but trying!
Let me tell you my goal at the end of this.....
I would like to be able to open an app on the iPhone and then execute a bit of code to send a message, all via Bluetooth.
I would like to be able to pair this small Bluetooth device to the iPhone only once and be able for it to be in sleep mode (500 nano amps :D awesome) and when I turn it on (via a small switch) it will open an app and send that message. (the app is already done, you just have to press a button on the app in order to send the message, that's why I would like to use a Bluetooth device so you don't have to have the app open at all times!)
Sorry for the long paragraph but I need help! I have the Bluegiga BLE113 dev kit and im overwhelmed with all the information they give you! But I don't see anywhere where I connect to the iPhone and do what I want to do.
Has anyone done this before, can you steer me in the right direction?
As long as the person downloading the app has previously opened the app and they've agreed to backgrounding capabilities, then yes you can send the message in the background (if already connected). However, you cannot force your app into the foreground on a non-jailbroken iOS device.
Check out the BLE Transfer App sample code to get you started.
I guess what you need is:
the periperal implements the peripheral role
the app implements the central role
the app is trying to connect to the peripheral continuously (connectPeripheral:options:)
the peripheral wakes up on trigger and the phone connection request completes
the peripheral signals to the central using a characteristic change notification
the app receives the notification and executes the business functionality of your desire
the peripheral goes to sleep again
the app receives the disconnect event and starts a connectPeripheral:options: immediately
the app should utilize the new restoration feature of Core Bluetooth (watch the WWDC 2013 session 703 video)
This is just an outline and you need to know lots of small things to get it right. I suggest you try to go ahead and implement piece by piece and if you get stuck, ask a new questions. As #tdevoy suggested, the BTLE Transfer App is a good starting point. The Heart Rate Monitor is also a good source of info (OSX API is the same as the iOS).

Trying to retrieve previously paired bluetooth device in IOS app will not respond with failure if device is off

Sorry for the long title, but we are having a pretty interesting issue with using corebluetooth for ios. We are issuing a call to retrievePeripherals in CBCentralManager and are able to find the previously paired device.
This happens though regardless if the device is on or off though. I can't find anything in apple's documentation as to why it's able to find the device when it is off though and it isn't showing up in Settings -> Bluetooth -> Devices. I'm suspecting that Apple is caching this information but can't find any documentation to confirm this. Also, when the device is off and we issue the connect call, the program continues to execute as normal but the delegate for didFailToConnect never gets called. When the device is turned on, it will connect immediately.
Is there a way to pass a timeout parameter when trying to connect to a device? If not, what would the best solution be to handling reconnecting to a previously used device for an application (we're storing the last connected device within the app).
Two points you need to know about retrievePeripherals: and connectPeripheral:
1.) retrievePeripherals: attempts to retrieve the CBPeripheral object associated with the uuid you supply. Even if the ble device is off (or on the other side of the country) retrievePeripherals: will still return an instance of CBPeripheral that you can call connectPeripheral: on. This is done intentionally so that you can issue a call to a peripheral that is not even around and still automatically connect to it when it comes back into range. It basically creates a marker inside the system bluetooth so that when the device is actually seen, it will know it should connect to it.
2.)connectPeripheral: will not time out unless the communication channel is broken with the actual device. If the iOS device has not seen the device, it will not fail and should not time out (unless some error occurs inside the system bluetooth).
And as for the timeout parameter, there is no documented way inside the CoreBluetooth framework. You can create your own implementation for it, however I believe you'd be better off keeping a list of which peripheral uuid's you've actually called connectPeripheral: on and then just pop them from the list when they connect. If you no longer want to connect to a peripheral in the list call cancelPeripheral: on that UUID, call connectPeripheral: on the other, and swap entries. Good to go.

Resources