I have never used android USB Host programming before. When I came to the company, I had to study the task assigned by the team lead while doing a test project.
He started to talk about how to connect android phones to PCs through USB programming. In this way, an android program is installed on the mobile phone and connected to the computer by USB, you can manage mobile phone files on your computer. After studying for more than two weeks, I checked some materials and tried many methods. Later I found that I had gone into a misunderstanding and wrote my summary. I hope to give you some tips:
I. Usage of USB HOST:
USB devices are divided into HOST (master device) and SLAVE (SLAVE Device). Data transmission can be realized only when a HOST is connected to a SLAVE. Android supports USB hardware that implements the android peripheral protocol. Android peripherals and host modes are directly supported on android3.1 or a later platform. However, the support for the USB host and peripheral mode depends on the hardware of the device, not just the platform level.
I searched the internet for some early summer new mobile phones and htc WM mobile phones that support the USB HOST function. However, android mobile phones currently support very few USB hosts, samsung I9250 is found in the mobile phones that support the usb host function, but not in the HTC mobile phones. Currently, the HTC T328w does not support the usb host function. Therefore, the test conditions are inadequate.
In addition, usb host is mainly used as the HOST device for android phones. It is used to control other components, such as single-chip microcomputer, mouse, game controller, and digital camera.
The mobile phone is rarely used as a slave device to communicate with the computer through a usb host. In addition, one person said that he could not communicate with the computer via usb host:
First, let alone whether it can be implemented. In addition, the usb host communication method is originally connected by USB. If you connect to the computer by using USB, you can directly manage the files on the mobile phone, can't I use a program?
2. connect through socket
At the beginning, I saw an example saying that the PC is connected to the mobile phone through the usb host. According to the information I found and my own understanding, I thought it was like the following:
1. Connect android mobile phones and PCs to transmit data based on socket programming.
2. During connection, port forwarding must be performed through adb forward to connect.
3. broadcast is required. on the server side, use the adb shell command to enable the socket connection service of android.
4. It is mainly based on the USB Manager class and USB accessory class under the android. hardware. USB package.
5. Then there are some IO operations, IO processing and shutdown optimization.
Two key points are:
1) port forwarding:
Runtime.getruntime(cmd.exe c ("adb forward tcp: 12580 tcp: 10086 ");
Listen to port 10086 on the server.
2) use the adb command in the server-side java program to start the client-side service.
Runtime.getruntime(cmd.exe c ("adb shell am broadcast-a NotifyServiceStart ");
Later, the test showed that the usb host method was not used, but socket communication was still used, but port forwarding was performed.
In addition, for socket communication, the android program can communicate with the server on the PC on the simulator, but cannot communicate when the program is installed on the real machine, the main cause of this problem is that the wireless network on the PC and mobile phone is not in the same network segment and cannot be connected.
Iii. Serial Communication
On the Internet, I found that USB HOST development is based on serial communication between android and some single-chip microcomputer. This is an embedded development that transcodes port data, we also need to use some c ++ knowledge, and some development boards, which do not understand the underlying things. In addition, this is irrelevant to the communication between android and pc.
Therefore, after analysis, the main contradictions are summarized as follows:
1. No testing conditions (mobile phones supporting USB hosts). Most mobile phones do not implement this function.
2. The Android mobile phone communicates with the pc end using the usb host Mode. If this is necessary, you can directly plug in the USB to transfer files.
3. It is difficult to program the Android usb host, and the android mobile phone is the main device. Whether there is a need for further research. It is not associated with the test mobile phone.
I don't think it is necessary to study any more. It takes a lot of time and detours. If I study it again, I may not be able to study anything, and the program does not have a mobile phone test. I think it will take a lot of time to study it again, and I cannot guarantee that it can be done. I can't say that I have never gained anything. I should at least understand this knowledge. I am going to study the Monkey and MonkeyRunner you mentioned last time. I don't know what X means. Give some instructions ....
Author: fengbo sea