FriendlyThings

From FriendlyARM WiKi
Jump to: navigation, search

查看中文

1 Introduction

FriendlyThings是友善电子开发的一套安卓硬件开发SDK(函数库),安卓软件开发者可以通过它在Android应用程序中编程访问和控制ARM主板上的各种硬件资源,比如Uart, SPI, I2C, GPIO等接口,它基于Android-NDK技术开发,开发者无需掌握底层的嵌入式知识(尤其是驱动程序),就可以构建基于安卓系统的各种IoT物联网应用。


2 Android版本

我们提供的Android BSP已经包含FriendlyThings SDK(libfriendlyarm-things.so),可支持运行于如下Android平台版本:

2.1 RK3399平台

  • Android 7.1.2-rk3399

BSP source code download link: https://gitlab.com/friendlyelec/rk3399-nougat
Latest ROM download link: http://download.friendlyarm.com/NanoPC-T4

  • Android 8.1-rk3399

BSP source code download link: https://gitlab.com/friendlyelec/rk3399-android-8.1
Latest ROM download link: http://download.friendlyarm.com/NanoPC-T4

2.2 S5P6818平台

  • Android 5-s5p6818

BSP source code download link: TODO
Latest ROM download link: TODO

2.3 S5P4418平台

  • Android 4.4-s5p4418

BSP source code download link: TODO
Latest ROM download link: TODO

  • Android 5-s5p4418

BSP source code download link: TODO
Latest ROM download link: TODO

2.4 Tiny4412平台

  • Android 4.2-exynos4412

BSP source code download link: TODO
Latest ROM download link: TODO

  • Android 5-tiny4412

BSP source code download link: TODO
Latest ROM download link: TODO

2.5 Tiny210/Smart210/Mini210平台

  • Android 4.2-S5pv210

BSP source code download link: TODO
Latest ROM download link: TODO

2.6 Tiny6410/Mini6410平台

  • Android 2.3-s3c6410

BSP source code download link: TODO
Latest ROM download link: TODO

3 开发板型号列表

FriendlyThings SDK(libfriendlyarm-things.so)适用于友善电子出品的如下系列主板:

3.1 RK3399

  • NanoPC-T4
  • NanoPi M4 (需外接eMMC模块)
  • NanoPi NEO4 (需外接eMMC模块)

3.2 S5P6818

  • NanoPC T3
  • NanoPi M3
  • NanoPi Fire3
  • Smart6818

3.3 S5P4418

  • Smart4418 SDK
  • NanoPC T2
  • NanoPi M2A
  • NanoPi Fire2A
  • NanoPi S2

3.4 Exynos4412

  • Tiny4412

3.5 S5PV210

  • Tiny210
  • Smart210
  • Mini210

3.6 S3C6410

  • Tiny6410
  • Mini6410

4 快速使用入门

4.1 (第1步) 集成libfriendlyarm-things.so到你的app

克隆以下仓库到本地:

git clone https://github.com/friendlyarm/friendlythings-sdk

接着复制 libs 目录下的所有内容到你的工程目录下,然后在你的Android项目的src目录下创建com/friendlyarm目录,将java/FriendlyThings目录拷贝进去即可,最后的目录的结构看上去是这样的 (注:AndroidStudio的项目可能会稍有不同,但大致如此):

YourProject/
├── AndroidManifest.xml
├── libs
│   ├── arm64-v8a
│   │   └── libfriendlyarm-things.so
│   └── armeabi
│       └── libfriendlyarm-things.so
├── src
│   └── com
│       └── friendlyarm
│           ├── FriendlyThings
│           │   ├── BoardType.java
│           │   ├── FileCtlEnum.java
│           │   ├── GPIOEnum.java
│           │   ├── HardwareControler.java
│           │   ├── SPIEnum.java
│           │   ├── SPI.java
│           │   └── WatchDogEnum.java

使用以下方法导入它们,主要的接口都集中在 HardwareControler.java文件中:

import com.friendlyarm.FriendlyThings.HardwareControler;
import com.friendlyarm.FriendlyThings.SPIEnum;
import com.friendlyarm.FriendlyThings.GPIOEnum;
import com.friendlyarm.FriendlyThings.FileCtlEnum;
import com.friendlyarm.FriendlyThings.BoardType;

4.2 (第2步) 让你的app拥有system权限

你的app需要拥有system权限,才能访问硬件资源;
请参考下面的方法修改你 app 的 AndroidManifest.xml 和 Android.mk这两个文件;
并且最好将你的app放到Android源码中去编译,这一步不是必需的,但是建议这么做,如果你的app在外部编译,你需要对apk进行签名才能让你的app拥有system权限(新手不太建议,过程比较繁琐)。

4.2.1 修改AndroidManifest.xml

在应用程序的AndroidManifest.xml中的manifest节点中加入以下属性:

android:sharedUserId="android.uid.system"

4.2.2 修改Android.mk

编写一个Android.mk文件(最简单的方法就是拷贝示例中的Android.mk文件),修改Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
 
LOCAL_SRC_FILES := $(call all-subdir-java-files)
 
LOCAL_PACKAGE_NAME := 你的项目名
 
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -lfriendlyarm-hardware
 
include $(BUILD_PACKAGE)

4.3 (最后1步) 在 Android源代码中编译你的app

先在 Android源代码根目录调用 setenv.sh 导出环境变量,然后进入你的 app 目录,使用mm命令编译;
例子:编译 GPIO_LED_Demo,以RK3399平台为例:

cd rk3399-android-8.1
. setenv.sh
cd vendor/friendlyelec/apps/GPIO_LED_Demo
mm

5 函数库(libfriendlyarm-things.so)接口说明

请参考这份WiKi文档:FriendlyThings APIs

6 Code Samples

7 Compile & Deploy Andorid Code (SerialPortDemo)

7.1 Step 1. Import source code in eclipse (SerialPortDemo)

Start eclipse, click on "Import" on its main window's File menu and then select "Existing Android Code Into Workspace" to import your source code
We name this project as "SerialPortDemo"

  • AndroidHW01.png
  • AndroidHW02.png

7.2 Step 2. Edit source code

The names of serial devices this DEMO accesses are listed in this file: src/com/friendlyarm/SerialPortDemo/SerialPortDataProcessView.java
Make changes according to your devices' names:
AndroidHW03.png

7.2.1 Serial Device Names under Multiple Platforms

  • Allwinner H3/H5 Based Boards (NanoPi M1/NanoPi M1 Plus etc)

UART1-> /dev/ttyS1
UART2 -> /dev/ttyS2
UART3 -> /dev/ttyS3 (Only applies to NanoPi M1, NanoPi K1 and NanoPi K1 Plus)

  • S5P4418 Based Boards (NanoPi Fire2A/NanoPi M2A/NanoPi S2/NanoPC-T2 etc)

UART1 -> /dev/ttyAMA1 [Note 1]
UART2 -> /dev/ttyAMA2 [Note 1]
UART3 -> /dev/ttyAMA3
UART4 -> /dev/ttyAMA4

  • S5P6818 Based Boards (NanoPi M3/NanoPC-T3 etc)

UART1 -> /dev/ttySAC1 [Note 1]
UART2 -> /dev/ttySAC2 [Note 1]
UART3 -> /dev/ttySAC3
UART4 -> /dev/ttySAC4

Note 1: only applies to specific boards, you need to check if a board has that serial device populated.

7.3 Step 3. Compile DEMO's source code and export apk file

On "Package Explorer" check your project's name:
AndroidHW04.png
On eclipse's main menu select "File" and then "Export...":
AndroidHW05.png
Select "Export Android Application":
AndroidHW06.png
Select "SerialPortDemo":
AndroidHW07.png
Eclipse requires you to create a new keystore or select an existing keysotre in this step. In this case we selected "Create new keystore", set a path to store the key and created its password and then clicked on "next":
AndroidHW08.png
Refer to the following screenshot to fill more information and click on "next":
AndroidHW09.png
Select a path to store your APK file. In this case we set it to a "signapk-files" directory:
AndroidHW10.png
Click on "Finish" to export an APK file.
AndroidHW11.png

7.4 Step 4. Give APK New Signature

For a program to access a hardware device that program needs to have "system" right therefore you need to give your APK a new signature.
Enter the signapk-files directory and give your APK a new signature:

cd signapk-files/
java -jar ./signapk.jar platform.x509.pem platform.pk8 ./SerialPortDemo.apk SerialPortDemo-Signed.apk

The "signapk.jar", "platform.x509.pem" and "platform.pk8" files are available under the "android-platform-key-files" directory.

7.5 Step 5 Run & Debug

5.1) Install APK. Connect your board to a host PC with a microUSB cable and run adb commands to install your "SerialPortDemo-Signed.apk":

adb install SerialPortDemo-Signed.apk

5.2) Debug: After SerialPortDemo is started run the following command in "adb shell":

ps -Z

Check this com.friendlyarm.SerialPortDemo" process. If its right is as follows that means this application already has "system" right:

u:r:system_app:s0              system    1610  112   com.friendlyarm.SerialPortDemo

This case is for Android 4. If you do it under Android 5 it will be as follows. Although some details are slightly different it means the application has "system" right as well:

u:r:platform_app:s0            u0_a60    1905  138   com.friendlyarm.SerialPortDemo

If your application doesn't have "system" right it will fail to open a device.

8 Resources

9 Update Log

9.1 March-2-2017

  • Released English version

9.2 Dec-19-2017

  • Added section 5