Android Question OS Wear Example

seskindell

Active Member
Licensed User
Longtime User
Does anybody know of a good example for sending a string from the Mobile device to the Wear Device and from the Wear Device to the Mobile Device?

Any help would be appreciated!

- Steve
 
It is exactly the same on both sides.

First you get the id of the connected nodes. In most cases it is only the watch (or the mobile) depending on which side you ask the question.

Code: Copy
wear.GetConnectedNodes

The NodeResults event will be raised:
Code: Copy
Sub wear_NodeResults(Success As Boolean, Results As List)
   Log(Success)
   If Success Then
       For i = 0 To Results.Size - 1
           Dim m As Map = Results.Get(i)
           Log(m.Get("ID") & ": " & m.Get("DisplayName"))          
       Next
   End If
End Sub

The id will look something like this:
Code: Copy
73dda970

Now you just send messages:
Code: Copy
wear.SendMessage("/MyApp", "73dda970", "my message")

On the sending device, you receive an event if the message was send:
Code: Copy
Sub wear_MessageSent(Success As Boolean, Path As String, Node As String)
   Log(Success & " " & Path & " " & Node)
End Sub

And on the receiving device, the MessageReceived event is raised:
Code: Copy
Sub wear_MessageReceived(SourceNodeID As String, RequestID As Int, msgPath As String, Data As String)
   Log(SourceNodeID & " " & RequestID & " " & msgPath & " " & Data)
End Sub
 
Last edited:
Upvote 0
When I try to send:

WearService.wear.SendMessage("/MyApp","4e697298","Testing")

java.lang.NullPointerException: Attempt to read from field 'android.content.Context anywheresoftware.b4a.BA.context' on a null object reference
at com.ab.abwearos.ABWearOS$SendMessageThread.run(ABWearOS.java:178)
at java.lang.Thread.run(Thread.java)
** Activity (main) Pause, UserClosed = true **

These are the errors I get at startup:

Logger connected to: 4e697298
--------- beginning of system
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
PackageAdded: package:com.ab.weardemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
Testing ... sent...
java.lang.NullPointerException: Attempt to read from field 'android.content.Context anywheresoftware.b4a.BA.context' on a null object reference
at com.ab.abwearos.ABWearOS$SendMessageThread.run(ABWearOS.java:178)
at java.lang.Thread.run(Thread.java)
** Activity (main) Pause, UserClosed = true **
** Service (wearservice) Destroy **
PackageAdded: package:com.ab.weardemo
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ab.weardemo/com.ab.weardemo.main}: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
at android.support.wear.ambient.SharedLibraryVersion.verifySharedLibraryPresent(SharedLibraryVersion.java:57)
at android.support.wear.ambient.WearableControllerProvider.getWearableController(WearableControllerProvider.java:48)
at android.support.wear.ambient.AmbientDelegate.onCreate(AmbientDelegate.java:103)
at android.support.wear.ambient.AmbientModeSupport.onCreate(AmbientModeSupport.java:198)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
at com.ab.weardemo.main.onStart(main.java:175)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
at android.app.Activity.performStart(Activity.java:6258)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
... 10 more
PackageAdded: package:com.ab.weardemo
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ab.weardemo/com.ab.weardemo.main}: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
at android.support.wear.ambient.SharedLibraryVersion.verifySharedLibraryPresent(SharedLibraryVersion.java:57)
at android.support.wear.ambient.WearableControllerProvider.getWearableController(WearableControllerProvider.java:48)
at android.support.wear.ambient.AmbientDelegate.onCreate(AmbientDelegate.java:103)
at android.support.wear.ambient.AmbientModeSupport.onCreate(AmbientModeSupport.java:198)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
at com.ab.weardemo.main.onStart(main.java:175)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
at android.app.Activity.performStart(Activity.java:6258)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
... 10 more
PackageAdded: package:com.ab.weardemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
PackageAdded: package:com.ab.weardemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
Testing ... sent...
java.lang.NullPointerException: Attempt to read from field 'android.content.Context anywheresoftware.b4a.BA.context' on a null object reference
at com.ab.abwearos.ABWearOS$SendMessageThread.run(ABWearOS.java:178)
at java.lang.Thread.run(Thread.java)
** Activity (main) Pause, UserClosed = true **
** Service (wearservice) Destroy **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
PackageAdded: package:com.ab.weardemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (wearservice) Create ***
** Service (wearservice) Start **
 
Upvote 0
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ab.weardemo/com.ab.weardemo.main}: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
seems you did forget something in manifest!?
 
Upvote 0
No ... that was already in the manifest for both the wear and the mobile apps. I was using the template for the mobile and wear devices.
 
Upvote 0
Has your watch Ambient mode ON? (Some watches have the ability to turn it off and there is no API to check if it is ON or OFF.)

See here maybe: http://www.rightminder.com/support-ambient/

Maybe in the Wear app, disable everything that has to do with Ambient:

Remove:
Code: Copy
#Extends: com.ab.abwearos.ABWearOSAmbient

Private ambient As ABWearOSAmbient  

ambient.Initialize("ambient")
 
Upvote 0
Ok .. I did that .. says now that it is waiting for debugging on the watch.

This isn't giving any results back:

Sub wear_NodeResults(Success As Boolean, Results As List)
Log(Success)
If Success Then
For i = 0 To Results.Size - 1
Dim m As Map = Results.Get(i)
Log("ID: " & m.Get("ID") & ": " & m.Get("DisplayName"))
Next
End If
End Sub
 
Upvote 0
How do I retrieve the message?
I send a simple text message to the watch. On the watch in a edittext box on the screen it shows:

bffa036 ##### /watchapp Message was received

The ##### is a counter from 40000+ ... it just keeps spinning numbers until it crashes

This is under the sub routine:

Sub wear_MessageReceived(SourceNodeID As String, RequestID As Int, msgPath As String, Data As String)
Log(SourceNodeID & " " & RequestID & " " & msgPath & " " & Data)
EditText1.Text = SourceNodeID & " " & RequestID & " " & msgPath & " " & Data
wear.SendMessage("/watchapp","bffa036","Message was received")
End Sub

On the wear device.
 
Upvote 0
wear.SendMessage("/watchapp","bffa036","Message was received")
Are you sure you are not sending it to the wear app again here? (sender and receiver same id). So every time the wear gets a message, you send a message to ITSELF, resulting in a MessageReceived, which then again sends a 'message was received' to itself etc...

NOTE: please use the code tags.
 
Upvote 0
Ok ... I took out the message was received and it only shows 1x now.

It only shows on the watch:

385dcf69 16943 /watchapp

Doesn't show the message that I sent.
 
Upvote 0
I went to the permissions on the watch and turned on "Storage" to On.
It is now sending the text from the mobile to the watch now.

Thank you.

- Steve
 
Upvote 0
Is there a way to keep the watch app continue to run in the background? I noticed if the watch face comes up it loses connection - disconnects with the phone. I then have to go into settings .. force close the watch app and re-pair it again.
 
Upvote 0
I un-commented out the Ambient lines in the wear template example.. and recompiled .. it still times out and disconnects the bluetooth connection... going back to the watch face. I am running OS Wear 2.0 on a Fossil Explorist 4th gen watch.

In your template wear example program the
Code: Copy
For Each key As String In ChangedItems.Keys
        Dim m As ABWearOSDataMap = ChangedItems.get(key)
Next

shows that m is never being accessed.
 
Upvote 0
Back
Top