基於Android的串口聊天室 (基於tiny4412) 一

1、平臺介紹

硬件平臺: tiny4412ADK + S700 4GB Flashhtml

Android版本:Android-5.0.2java

Linux版本: Linux-3.0.86linux

Bootloader:Superbootandroid

 

做者:彭東林express

郵箱:pengdonglin137@163.comapache

 

image

2、代碼目錄

下面的例子是基於Andrioid源碼中自帶的一個串口應用SerialChat,Android已經把與之相關的Freamwork、JNI代碼實現了,其中直接在JNI中調用了Linux的系統調用,沒有遵循Android的HAL架構,根本就沒有提供HAL。ubuntu

下面是相關的代碼路徑:api

APP:bash

frameworks/base/tests/SerialChat/架構

SerialManager:

frameworks/base/core/java/android/hardware/SerialManager.java

SerialService:

frameworks/base/services/core/java/com/android/server/SerialService.java

SerialPort:

frameworks/base/core/java/android/hardware/SerialPort.java

JNI:

frameworks/base/services/core/jni/com_android_server_SerialService.cpp

frameworks/base/core/jni/android_hardware_SerialPort.cpp

AIDL:

frameworks/base/core/java/android/hardware/ISerialManager.aidl

3、編譯運行

  • 編譯

root@ubuntu:~/tiny4412_android5/android-5.0.2# . setenv 
including device/lge/mako/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/friendly-arm/tiny4412/vendorsetup.sh
including device/moto/shamu/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
including device/asus/deb/vendorsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/asus/tilapia/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including sdk/bash_completion/adb.bash

root@ubuntu:~/tiny4412_android5/android-5.0.2# mmm frameworks/base/tests/SerialChat/
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=5.0.2
TARGET_PRODUCT=full_tiny4412
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a9
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.13.0-32-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=LRX22G
OUT_DIR=out
============================================
make: Entering directory `/root/tiny4412_android5/android-5.0.2'
target Java: SerialChat (out/target/common/obj/APPS/SerialChat_intermediates/classes)
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes-jarjar.jar
Copying: out/target/common/obj/APPS/SerialChat_intermediates/emma_out/lib/classes-jarjar.jar
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.jar
Proguard: out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar
ProGuard, version 4.10
Reading program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-junit_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar]
Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar]
Preparing output jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar]
  Copying resources from program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar]
target Dex: SerialChat
Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.dex
target Package: SerialChat (out/target/product/tiny4412/obj/APPS/SerialChat_intermediates/package.apk)
Install: out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk make: Leaving directory `/root/tiny4412_android5/android-5.0.2'

#### make completed successfully (13 seconds) ####
  • 安裝和修改

adb install –r out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk

點擊運行,會出錯退出。

如今做以下修改:

一、frameworks/base/tests/SerialChat/Android.mk

#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := SerialChat

LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

二、frameworks/base/tests/SerialChat/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.serialchat" android:sharedUserId="android.uid.system">

    <uses-permission android:name="android.permission.SERIAL_PORT"/>

    <application android:label="Serial Chat">
        <activity android:name="SerialChat" android:label="Serial Chat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

三、frameworks/base/core/res/res/values/config.xml

爲何要修改這個文件呢?在這個文件中配置那些設備節點做爲串口設備,在tiny4412平臺一共有四個串口,COM0和COM3在底板上用DB9引了出來,對應的設備節點是/dev/ttySAC0、/dev/ttySAC一、/dev/ttySAC二、/dev/ttySAC3,因此修改以下:

 
 <!-- List of paths to serial ports that are available to the serial manager.
         for example, /dev/ttyUSB0
  -->
<string-array translatable="false" name="config_serialPorts">
    <item>"/dev/ttySAC0"</item>
    <item>"/dev/ttySAC1"</item>
    <item>"/dev/ttySAC2"</item>
    <item>"/dev/ttySAC3"</item>
</string-array>

四、frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java

在frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java中:

    @Override
    public void onResume() {
        super.onResume();

        String[] ports = mSerialManager.getSerialPorts();
        if (ports != null && ports.length > 0) {
            try {
                mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);
                if (mSerialPort != null) {
                    new Thread(this).start();
                }
            } catch (IOException e) {
            }
        }
    }

從上面的代碼能夠發現,調用getSerialPorts返回的就是在config.xml中配置的串口設備,這裏ports[0]就是」/dev/ttySAC0」,在這裏咱們使用COM3,對應的是ports[3],也就是/dev/ttySAC3,因此這裏做以下修改:

mSerialPort = mSerialManager.openSerialPort(ports[3], 115200);

從新編譯:

mmm frameworks/base/core/res/   # 編譯出來的是framework-res.apk,替換/system/framework下的framework-res.apk
mmm frameworks/base/tests/SerialChat/ # 先卸載,在安裝 (卸載 adb uninstall com.android.serialchat)

4、測試

下面是測試結果:

  • 這個是串口調試助手,運行在PC機上。

image

  • 下面是tiny4412上運行SerialChat,輸入完畢點擊image便可發送。

image

 

下載地址: http://pan.baidu.com/s/1nWHP0

完。

相關文章
相關標籤/搜索