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

Viewed 162

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.

1 Answers

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