Introduction to Android Wear development

Source: Internet
Author: User

Hello everyone, I am Lujiajie, I am an Android developer. I would like to make some technical exchanges with you, and I hope more and more people can become good friends with me.

As we all know, the smart watch is the next development of the tuyere, and this technology is at the forefront, so share with you the Android Wear development process.

First of all, I recommend that you use Android Studio for wear development, which is also recommended by Google, and this course will be done with Android studio as a development integration environment.

Let's create an Android Wear project below.


Please note that the Android wear project contains both mobile and wear, so when you create it, you should tick both the phone and tablet and wear, where we select wear's minimum SDK as the API 20, this is also to adapt to more system version, the phone API can be selected according to demand, here we choose API 18.

The mobile part of the project generation process is the same as the normal app development, let's take a look at the wear section:


We choose Blank Wear Activity, click Next


Click Finish


Let's look at the structure of the project, which has two parts: mobile and wear, with mobile and wear two build.gradle. At this point, our Android Wear project was created successfully.

Below, let's create an Android Wear simulator:

Click AvD Manager and select wear, you can see there are two models here: Round and square.


Here we choose round, most people currently use Moto360, so we also use round to test.

Click Next


We choose Android 5.0.1, click Next


Click Finish


At this point, our wear simulator has been created successfully, and we can see the green running button, which represents this simulator.

Let's see what files are included in the folder when you create the Wear emulator correctly:


Wear Simulator default path is C:\USERS\ASUS\.ANDROID\AVD\ANDROID_WEAR_ROUND_API_21.AVD

By the way, if the wear simulator does not run and encounters a problem such as a skin does not exists, you will find that the Config.ini in the above folder is not generated, resulting in the inability to run. This issue is a problem with the SDK used by the project, usually there is a duplicate API in the SDK Build-tools, delete duplicates, of course, if you have downloaded the correct API.

This is the author of the folder structure under API 20, including the Skins folder for reference.


Let's run the simulator.

You can see the console output wear emulator information, which indicates a successful run.


Let's take a look at the simulator:

This is the startup animation of the simulator


After the operation is complete


The reason for this is that your computer is not connected to a mobile device, or you are not launching the mobile simulator.

You can see it when your computer is connected to your mobile device


So far, our wear simulator started successfully.

See here, we can not help but think, I fell into the deep pit of Android!!! To do two models of adaptation!!!

Don't worry, Google has already handled this.

Let's take a look at the project code structure

Open the Wear Mainactivity.java, you can see the strange watchviewstub, which is based on the shape of the screen to render a special layout of the class, follow-up tutorial will be introduced for you.


Open Wear's activity_main.xml, you can see app:rectlayout= "@layout/rect_activity_main" and

app:roundlayout= "@layout/round_activity_main" two kinds of matching.


Thus we can conclude that the developer does not need to distinguish between the round model and the square model of the wear, we only need the corresponding Rect_activity_main.xml and round_activity_ Main.xml the corresponding layout can be done. (You can use the same Java logic)

Let's familiarize ourselves with the basic operation of the simulator.


As shown, the wear simulator has only one home key, and the developer can go back to the previous page using the Home key, which is usually used to return to the original page (i.e.). Developers can also use the mouse to scroll through the page to operate.

On the initial page (this must be guaranteed, otherwise cannot be clicked), click on the upper half of the color pattern, that is, enter


Use your mouse to swipe down on the display page to start ... Position


Click Start ...


Here we can see the wear emulator's running project, currently only the system default original project.

Now, let's run the program.


Click OK

You can see the success of the interface, interested friends can also try to run the square simulator, the process as above


We go back to the start of the simulator again ... Interface

Again, the method is to click the emulator's Home button, go back to the original interface, then click on the Color section


You can see that our app is stored in the Wear simulator


So far, our program is running successfully and you can show off with the Hello World running in the wear simulator! ~

Will we be able to actually develop the Android Wear app here?

The answer is.

In the above process, we just run the application directly to the simulator. However, our ultimate goal is to run to the real smart watch device. The real device does not have a USB interface, it cannot be connected directly to the computer, and it is impossible for the user to try to connect the watch to the computer in order to install an application. So how do we install the app on a real smart watch device?

First, let's take a look at the use of the user's installation of the watch application.

Users through the mobile phone, login to the major application mall, search Android Wear app, and download the app. Paired with a smartphone with a smart watch, the smart watch senses the Android Wear app on the paired phone and the app automatically installs to the smart watch.

From the above process, we will find a question, how does the phone connect with the smart watch Bluetooth interconnect, how does this work?

Google offers Android Wear app that allows users to download the Android Wear app from their mobile phone and pair it with a smart watch via Bluetooth interconnect.

For most developers, there is no smart watch device, plus the phone is not the Android native system, we have to develop wear to catch the air, we need to spend a sum of cash? Developers who are good at solving problems can always find a way.

First, we have to turn the system of the mobile phone (or the system of the mobile simulator) into a similar Android native system. Why do you do this? Because Android Wear app requires a mobile phone to run Google Play, and if the native system is not an Android native system, running Google Play will flash back, that is, the Android Wear app does not work, Causes the phone to not interconnect with the smart watch.

Many Android Wear developers will be stuck on this hurdle, and here I offer you a reliable way: Download Google installer on your phone, which will simulate your Android native system with one click and download the apps and services you need.

For developers with Real smart watch devices, it is now easy to interconnect their phones and watches. So for developers who don't have a real smart watch device, I'll provide a way for you to interconnect your phone to the wear simulator.

First, open the Android Wear app on your phone, click the bottom triangle icon in the top left corner, select the emulator, and the Android Wear app page shows that the emulator is trying to connect ... (You should successfully start the Wear emulator before, and ensure that the wear simulator is in the same wifi as the phone, because the simulator does not have Bluetooth function, to use WiFi)

Then open the cmd window and go to the directory where the SDK Platform-tools is located, as shown in


Enter ADB devices to view connected devices at this time


If this is the case, discover that only the wear simulator, and no mobile device, can use tools such as pea pods to connect the phone to the computer, or reconnect the phone (or restart the Mobile emulator).

Enter ADB devices again at this time


You can see that your mobile device is connected.

At this point enter adb-d forward tcp:5601 tcp:5601

Wait a few moments after viewing the mobile Android Wear App, you can see that this shows: emulator is connected

If you are connected for a long time: The reason is that the pea pod and other mobile helper devices are not recognized, you can turn off or restart the Pea pod, and then re-enter adb-d forward tcp:5601 tcp:5601

So far, the phone is successfully connected to the Wear simulator!

After the preparation is done, I believe the reader will be tempted to run the program directly on the phone. Whether you run the mobile Module or run the wear module,android wear application simply runs to the phone and does not automatically install to the smart watch device as you would imagine.

The right thing to do is to use Android Studio to pack the APK, and only then, to properly publish the Android Wear app.

Before we do this, we need to revise our project code.

First, add Bluetooth and network permissions to the wear Androidmanifest.xml:


Then, add Bluetooth and network permissions in the Androidmanifest.xml of mobile and:

<uses-feature android:name= "Android.hardware.type.watch"

Android:required= "false"/>


Once you're ready, you can pack the APK.

Select Build in Android Studio, click Generate signed APK


Select Mobile Module and click Next


Here we first click on the Create New ...


Fill in the content, the author except key store path, the others have filled out my name Pinyin Lujiajie


Click OK to open the local e-drive, you can see the Mykey.jks file at this time

Back to Android Studio, tick remember passwords, click Next


Enter Password:lujiajie Confirm again Password:lujiajie Click OK


Choose Build Type:release, must not choose Debug, otherwise the application will not automatically install to the smart watch


Click Finish

Wait a moment to appear after the proof pack apk succeeds


Click Show in Explorer to see Folders with mobile-release.apk and wear-release.apk


We can note that the mobile-release.apk is particularly large because of the wear-release.apk embedded in it. The final release of the APK, is also the use of mobile-release.apk.

At this point we installed the mobile-release.apk to the mobile phone, in the case of ensuring that the phone and smart watch device interconnection pairing (wear simulator same), wait 1-2 minutes to discover, the watch successfully installed the application! (Available in simulator start ... Page view)

It is important to note that the Android Wear app on the phone is the same life cycle as the app on the watch, which is installed on the phone, and the watch automatically installs the app when the phone and watch are connected. Your phone uninstalls the app, and the app on your watch disappears. (This can be achieved by reusing the emulator without having to recreate a new emulator for the application on the emulator cannot be deleted)

So far, my Android Wear development started teaching finished, I hope to help everyone! At the same time, you are welcome to leave a comment and point out my shortcomings. If you encounter problems can also leave a message, I see after the appropriate amount of reply.

Wen/Lujiajie (Jane book author)

Original link: http://www.jianshu.com/p/50c20522725b
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Introduction to Android Wear development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.