Android Tutorial Creating and Packaging a Wear App

This tutorial will show you how to create an app for a Wearable device and how to package it so it auto installs.

What is Android Wear?
For those that do not know, Google has released a new branch of the Android platform designed for Wearables. The current 'Wearable' devices are smartwatches. Namely the Samsung Gear Live, LG G Watch with the Moto 360 due soon and a few others just being announced.

More can be found here

What are we achieving and why does it require a tutorial?
We are trying to achieve the actions of creating an app for Wear devices and getting it installed on the device. Android Wear devices do not have direct access to an app market. Wear apps must have a handheld counterpart. Basically, you install the handheld app from the appropriate market place. Within that handheld app you package your Wear app. This process I believe is something that has never been done with B4A.

Here goes...

Creating the app for the Wearable device.
Apps for Wearables should obviously take into account the screen size and features available on the target device. The current watches are 280x280 or 320x320 screens so we don't have as much real estate. They are also very small screens so you should make your layouts simple with large interaction view for easy tapping. There is an unofficial UI library for Wearable devices, something I hope will be made into a B4A lib soon. Until then, standard views are support out of the box or you could create your own custom views. The Wear app should mainly be used for displaying info and interacting with the wear device sensors. Intensive computing operations and network access should be done by the handheld and the results passed to the Wearable with the DataLayer. The DataLayer is a library I am currently working on. I was hoping to have it completed before this tutorial, but I feel it is simply taking too long. Hopefully it won't be long now.

OK, let's get down to the good stuff.
Create a new project in B4A and some UI and code. This can be anything you like really, Wear devices support most Android APIs and have a few sensor too (Accelerometer, Gyro, Compass). Set the package name to whatever your normal convention suits. The recommended theme for Wearables is Theme.DeviceDefault.Light. Set this by adding the following in the manifest editor.

B4X:
SetApplicationAttribute(android:theme, "@android:style/Theme.DeviceDefault.Light")

You must edit your SDK levels and add a feature to the in the manifest. The final result should be
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="20" android:targetSdkVersion="20"/>
<uses-feature android:name="android.hardware.type.watch"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

SetApplicationAttribute(android:theme, "@android:style/Theme.DeviceDefault.Light")

Once you are happy with the app, compile the project without a device connected to the pc. This will produce the .apk under project/Objects/.

Create the Handheld counterpart
Next create the handheld app, again, whatever UI / code you like. Set the package name exactly the same as you did in the Wearable app. The DataLayer will again be used eventually to send / receive data between the 2.

Add the following to the manifest editor

B4X:
AddApplicationText(
<meta-data android:name="com.google.android.wearable.beta.app"
    android:resource="@xml/wearable_app_desc"/>
)
I'm assuming the 'beta' will be dropped eventually.
Also, target SDK version 20. Min SDk version 14.

Tying the 2 together
Now go to the project folder for the handheld app. Create a folder called 'raw' with the Objects/res/ directory. Also, create an 'xml' folder if it doesn't exist already. So you have this
DirStructure.jpg


Next inside the /xml/ create a new xml file and add the following code.
B4X:
<?xml version="1.0" encoding="utf-8"?>
<wearableApp package="barxdroid.wearable_app">
    <versionCode>1</versionCode>
    <versionName>1.0</versionName>
    <rawPathResId>wearable_app</rawPathResId> '<-- replace with name of your Wearable APK file (minus the '.apk')
</wearableApp>
Save the file as 'wearable_app_desc.xml' and make the file read-only.

Copy the .apk file of your Wearable app into the /raw/ file and make that read-only also.

Finally...

The last step is to compile you handheld app. This will package the Wearable app inside the handheld .apk, because we put it in the /raw/.

Now when you install the app onto your phone/tablet that is connected to a Wear device it will automatically install the Wear app to the Wear device.

You can check and launch the app by doing the following:
On the Wear device:
* Tap the screen ('Speak Now' will show)
* Scroll down the page until you see 'Start' and tap that
* You should see your app in the list.

I Hope this is of use to some, I was going to wait until I had the DataLayer lib ready to show a more complete setup but I feel bad for keeping this to myself While the lib is taking time.

Attached are the 2 demo projects (Wearable and Handheld)

Note: will probably make this prettier when I have a little more time.
 

Attachments

  • WearTest(handheld).zip
    109.1 KB · Views: 880
  • WearTest(wearable).zip
    5.5 KB · Views: 823
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
reserved
 

appie21

Active Member
Licensed User
Longtime User
Hi!

Many thannks for this tutorial so far..

I have download your files but can you give a simple command to sent some text to each other
B4X:
Sub btnSend_Click
   
End Sub
 

barx

Well-Known Member
Licensed User
Longtime User
Hi!

Many thannks for this tutorial so far..

I have download your files but can you give a simple command to sent some text to each other
B4X:
Sub btnSend_Click
  
End Sub


As I have previously explained, this (send messages) requires a new library, one that I have been working on for a while. Unfortunately I don't have a great deal of spare time per day so it has taken longer than I would have hoped. I have the main communication channel open and some messaging services working. There have been a good few bits of the library that I have found tricky.

I hope to have an initial release of the library by the end of this weekend but don't hold me to that :confused:
 

barx

Well-Known Member
Licensed User
Longtime User
Will it help if i press your donate button ? :)
LoL, all donations are always welcomed / appreciated and although they don't put more hours into the day, they do keep the coffee flowing a little longer ;)
 

barx

Well-Known Member
Licensed User
Longtime User
Well, I missed my weekend deadline, sorry. Was simply too busy to get the coding time in, although I did make some good progress and I believe I am very close now. Going to be doing some testing during today if I get the time at work. Last main thing to code is getting the images / files back out of an Asset, read some info on it yesterday and watched a fairly informative video last night that went on for over an hour :confused:
 

barx

Well-Known Member
Licensed User
Longtime User
Initial testing of the DataMap has been successful which means items such as int, long, string, etc can be kept in sync between the 2 devices. A little more testing and I think I will release a Beta while I finish working on the Asset object. At least this way you guys can have a play and get the 2 working.
 

appie21

Active Member
Licensed User
Longtime User
Hi to your Example code i add

Dim pv As PhoneVibrate

Then i Run and copy the apk to the Objects\res\raw of the host file
But if i use Phonevibrate the app will not install to the wearable (Lg Watch)
mylibary from phone is version 2.25

The Strange is that if I run my app incuded PhoneVibrate direct to my watch it run perfect and the wearable device is vibrating
 

barx

Well-Known Member
Licensed User
Longtime User
The only thing I can think of is the fact the g watch doesn't have a vibration motor. In the Google docs for wear it mentions new methods to check if a particular sensor or interaction unit is present. Not really looked into it much more than that. Try logging both devices whilst you install the app see if you get an error.
 

appie21

Active Member
Licensed User
Longtime User
Hi
if i i run the app direct from b4a to the watch everything looks ok and work fine, tomorrow i will test again!
 

appie21

Active Member
Licensed User
Longtime User
Hi

I think i know the problem

I must also give permission for Vibrate in the main app (run on the devic and also on the wearable)

if i do that everything works perfect!
 

barx

Well-Known Member
Licensed User
Longtime User
Nice to see you got it working OK. wear is new to all of us, so sharing these tips will help many.
 

Uitenhage

Member
Licensed User
Longtime User
Wow! Totally awesome. Any news on your progress?

Thank you for the great tutorial.

Jim
 

smishra

Member
Licensed User
Longtime User
Great tutorial by Barx!

I ran into an installation problem similar to appie21, and the resolution was similar.

The permissions in Mobile App and the Wearable app must match, else the wearable app does not install.
 

green2000

New Member
Licensed User
Longtime User
Hi Barx

Nice work!

I tried to test the demos on Samsung Gear 2 with no luck. It gets installed on the Samsung phone (Note 3) via B4A bridge but nothing shows up on Gear 2 (even though it is connected to the phone).

Any suggestions that I should try?

Thanks,
 

barx

Well-Known Member
Licensed User
Longtime User
Hi Barx

Nice work!

I tried to test the demos on Samsung Gear 2 with no luck. It gets installed on the Samsung phone (Note 3) via B4A bridge but nothing shows up on Gear 2 (even though it is connected to the phone).

Any suggestions that I should try?

Thanks,
Unfortunately green2000, This tutorial is for Android Wear devices. Not all smart watches are running Android Wear. The Samsung Gear / Gear 2 / Neo do not run Android Wear.

Sorry to bring bad news....
 
Top