1. The background

Most of the Android applications I’ve developed require interaction with a remote server to get or upload data from the server. That way our app (Android application, short for below) accesses the server directly on the Internet. Of course, when there is wifi, we will intervene wifi routing, and then access the server. The illustration is as follows:

Scene 1:

Android devices <– > Servers located on the Internet

Scene 2:

Android devices <– >WIFI routing (probably home routing, etc.) <– > Servers located on the Internet

The above scenario is basically what we need. Then, we have to consider some questions:

  1. The PC is still our main entry point to the Internet.
  2. Android devices are not as easy to use as PCS, after all

So we consider another scenario

Scenario 3:

Android device <– >PC <– > Server located on the Internet

In this scenario, our Android app first interacts with the desktop App on the PC, and the desktop app interacts with the Internet app (Web App).

For example, if we want to download “an application to install on the mobile phone”, we can operate on the application on the computer, search for the application we want, click Download (from the remote download to the local PC), and then install (from the PC to install the application to android phone). We commonly see “pea pod”, “Tencent mobile phone assistant” and so on are used in this way.

2. Implement

If scenario 3 is to be implemented, we need to develop:

1. A desktop app.

Use: To extract data from the Internet and transfer the data to an Android phone.

2. An Android app running on an Android device

Used for: Receiving data from the Desktop App. Receive instructions from the Desktop APP, perform relevant operations, and return messages to the PC.

2.1 Implementation of desktop applications

To make a desktop application, of course, still use WinForm (.NET direction) to achieve. To establish synchronization between PC and mobile phone, we need to rely on ADB to achieve. Let’s see what ADB is.

The full name of ADB is Android Debug Bridge, which functions as a Debug Bridge. Adb makes it easy to debug Android applications using DDMS in Eclipse, which is basically the debug tool. Adb works in a special way, by listening on ports such as Socket TCP 5554 for the IDE to communicate with Qemu. By default, ADB daemons the associated network ports, so when we run Eclipse the ADB process will run automatically. Adb is a tool in the Android SDK that allows you to directly manage the Android emulator or a real Android device (such as the G1 mobile phone). Its main functions are: * Running the device's shell(command line) * managing emulator or device port mapping * uploading/downloading files between computer and device * installing native APK software to emulator or Android deviceCopy the code

To use ADB, we can find it in our SDK directory.

Several files located under Android-sdk \platform-tools

adb.exe

AdbWinApi.dll

AdbWinUsbApi.dll

We need these three documents. Copy to our WinROM project.

In.NET Winfrom, we need to call adb.exe to complete the sequence of operations. We typically use the Process class to start a new process. As follows:

1. List all android devices currently connected to the PC. (To get a device ID)

2. Install an APK (Android application package) on your phone.

3. Send a broadcast to start a service on the phone.

4. Map the port on the PC. This port connects an Android port to a PC port, and then we can use this port to communicate with the device.

I’ve wrapped a class for process to execute adb.exe, see my blog:

www.cnblogs.com/vir56k/arch…

 

2.1 Implementation of Android applications

We need to open a daemon to listen for instructions from the PC to perform the corresponding operation. We need:

1. Write a broadcast receiver to start the listening service when a broadcast message is received.

Write a listening service. To start the listening service in the service, we need to listen for messages coming from a port, which is the same route between the phone and the PC. Due to the nature of Android devices, we need Java NIO for listening to improve performance.

3. Monitor the message response in the service and complete the message response with the PC according to specific service requirements.

 

—–

Source code is not available for download.

 

Reference: blog.csdn.net/wufenglong/…

Wenku.baidu.com/view/89bee4…