android_serialport_api代码分析

1. 导入Android studio

 android_serialport_api是一个开源的串口测试工具,代码应该是用eclipse工程(不确定,没用过eclipse,反正不是Android studio的)。把他放入Android studio中,正好熟悉一下Android studio的使用。

1. 查看源码结构

desk@desk-ubuntu:~/android-serialport-api-master$ tree
.
├── AndroidManifest.xml
├── bin
│   ├── AndroidManifest.xml
│   ├── classes
│   │   └── android_serialport_api
│   │       ├── sample
│   │       │   ├── Application.class
│   │       │   ├── BuildConfig.class
│   │       │   ├── ConsoleActivity$1.class
│   │       │   ├── ConsoleActivity$2.class
│   │       │   ├── ConsoleActivity.class
│   │       │   ├── LoopbackActivity.class
│   │       │   ├── LoopbackActivity$SendingThread$1.class
│   │       │   ├── LoopbackActivity$SendingThread.class
│   │       │   ├── MainMenu$1.class
│   │       │   ├── MainMenu$2.class
│   │       │   ├── MainMenu$3.class
│   │       │   ├── MainMenu$4.class
│   │       │   ├── MainMenu$5.class
│   │       │   ├── MainMenu$6.class
│   │       │   ├── MainMenu.class
│   │       │   ├── R$array.class
│   │       │   ├── R$attr.class
│   │       │   ├── R.class
│   │       │   ├── R$drawable.class
│   │       │   ├── R$id.class
│   │       │   ├── R$layout.class
│   │       │   ├── R$string.class
│   │       │   ├── R$xml.class
│   │       │   ├── Sending01010101Activity.class
│   │       │   ├── Sending01010101Activity$SendingThread.class
│   │       │   ├── SerialPortActivity$1.class
│   │       │   ├── SerialPortActivity.class
│   │       │   ├── SerialPortActivity$ReadThread.class
│   │       │   ├── SerialPortPreferences$1.class
│   │       │   ├── SerialPortPreferences$2.class
│   │       │   └── SerialPortPreferences.class
│   │       ├── SerialPort.class
│   │       ├── SerialPortFinder.class
│   │       └── SerialPortFinder$Driver.class
│   ├── classes.dex
│   ├── MainMenu.apk     
│   ├── res
│   │   └── crunch
│   │       └── drawable
│   │           └── icon.png
│   └── resources.ap_
├── gen
│   └── android_serialport_api
│       └── sample
│           ├── BuildConfig.java
│           └── R.java
├── jni
│   ├── Android.mk
│   ├── Application.mk
│   ├── gen_SerialPort_h.sh
│   ├── SerialPort.c
│   └── SerialPort.h
├── libs
│   ├── armeabi
│   │   └── libserial_port.so
│   ├── armeabi-v7a
│   │   └── libserial_port.so
│   └── x86
│       └── libserial_port.so
├── project.properties
├── README.md
├── res
│   ├── drawable
│   │   └── icon.png
│   ├── layout
│   │   ├── console.xml
│   │   ├── loopback.xml
│   │   ├── main.xml
│   │   └── sending01010101.xml
│   ├── values
│   │   ├── baudrates.xml
│   │   └── strings.xml
│   └── xml
│       └── serial_port_preferences.xml
├── run_emulator.sh
└── src
    └── android_serialport_api
        ├── sample
        │   ├── Application.java
        │   ├── ConsoleActivity.java
        │   ├── LoopbackActivity.java
        │   ├── MainMenu.java
        │   ├── Sending01010101Activity.java
        │   ├── SerialPortActivity.java
        │   └── SerialPortPreferences.java
        ├── SerialPortFinder.java
        └── SerialPort.java

 

2. 加入Android studio

将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

 

3. 分析代码

1. 总体框架

 

2. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android_serialport_api.sample"    //包名
    android:versionCode="3"
    android:versionName="1.2" >

    <application      //应用
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:name="Application" >

//第一个进入的activity;名字为MainMenu <activity android:label="@string/app_name" android:name="MainMenu" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="SerialPortPreferences" > </activity> <activity android:name="ConsoleActivity" > </activity> <activity android:name="LoopbackActivity" > </activity> <activity android:name="Sending01010101Activity"></activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>

 

3. MainMenu.java

public class MainMenu extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button buttonSetup = (Button)findViewById(R.id.ButtonSetup);
        buttonSetup.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, SerialPortPreferences.class));
            }
        });

        final Button buttonConsole = (Button)findViewById(R.id.ButtonConsole);
        buttonConsole.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, ConsoleActivity.class));
            }
        });

        final Button buttonLoopback = (Button)findViewById(R.id.ButtonLoopback);
        buttonLoopback.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, LoopbackActivity.class));
            }
        });

        final Button button01010101 = (Button)findViewById(R.id.Button01010101);
        button01010101.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, Sending01010101Activity.class));
            }
        });

        final Button buttonAbout = (Button)findViewById(R.id.ButtonAbout);
        buttonAbout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
                builder.setTitle("About");
                builder.setMessage(R.string.about_msg);
                builder.show();
            }
        });

        final Button buttonQuit = (Button)findViewById(R.id.ButtonQuit);
        buttonQuit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MainMenu.this.finish();
            }
        });
    }
}

 

4. SerialPortPreferences.java

 

 

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="BAUDRATE">300</string>
    <string name="DEVICE">/dev/ttyS2</string>
</map>

 

posted on 2017-11-22 10:56  maogefff  阅读(1198)  评论(0编辑  收藏  举报

导航