Difference between revisions of "Android Thing: Android Hardware Programming"

From FriendlyELEC WiKi
Jump to: navigation, search
(Compile & Deploy Andorid Code(SerialPortDemo))
(updated by API)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Android Hardware Access/zh|查看中文]]
 
[[Android Hardware Access/zh|查看中文]]
  
 
+
The Document has moved to: [[FriendlyThings]]
==Introduction==
+
FriendlyElec developed a library called “libfriendlyarm-hardware.so”, for android developer to access the hardware resources on the development board in their android apps, the library is based on Android NDK.<br />
+
 
+
==Accessible Interfaces & Ports==
+
* Serial Port
+
* PWM
+
* EEPROM
+
* ADC
+
* LED
+
* LCD 1602 (I2C)
+
* OLED (SPI)
+
I2C, SPI, GPIO are accessible.<br />
+
<br />
+
<div><ul>
+
<li style="display: inline-block;"> [[File:Smart4418SDK+OLED.png|frameless|350px|Smart4418SDK+OLED]]</li>
+
<li style="display: inline-block;"> [[File:NanoPCT3-I2C-LCD1602.jpg|frameless|350px|NanoPCT3-I2C-LCD1602]] </li>
+
</ul></div>
+
 
+
==Available OS & Boards==
+
* Android 2.3: Mini6410, Tiny6410
+
* Android 4.2: Mini210S, Smart210 with SDK, Tiny210 with SDK, Tiny4412 with SDK
+
* Android 4.4: NanoPi M2, NanoPi S2, NanoPC-T2, Smart4418 with SDK
+
* Android 5: NanoPi M3, NanoPi-T3, Tiny4412 with SDK
+
<br />
+
 
+
==Code Samples==
+
* Here is a github address for code samples: https://github.com/friendlyarm/AndroidHardwareAccess
+
 
+
==Compile & Deploy Andorid Code (SerialPortDemo)==
+
===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<br />
+
We name this project as "SerialPortDemo"<br />
+
 
+
<div><ul>
+
<li style="display: inline-block;">[[File:AndroidHW01.png|400px]]</li>
+
<li style="display: inline-block;">[[File:AndroidHW02.png|400px]] </li>
+
</ul></div>
+
 
+
===Step 2. Edit source code===
+
The names of serial devices this DEMO accesses are listed in this file: src/com/friendlyarm/SerialPortDemo/SerialPortDataProcessView.java<br />
+
Make changes according to your devices' names:<br />
+
[[File:AndroidHW03.png|500px]]<br />
+
 
+
====各个平台开发板的串口设备名称 ====
+
* Allwinner H3/H5平台开发板 (NanoPi M1/NanoPi M1 Plus等)
+
UART1-> /dev/ttyS1<br />
+
UART2 -> /dev/ttyS2<br />
+
UART3 -> /dev/ttyS3 (仅NanoPi M1, NanoPi K1, NanoPi K1 Plus可用)<br />
+
 
+
* S5P4418平台开发板 (NanoPi Fire2A/NanoPi M2A/NanoPi S2/NanoPC-T2等)
+
UART1 -> /dev/ttyAMA1  [注1]<br />
+
UART2 -> /dev/ttyAMA2  [注1]<br />
+
UART3 -> /dev/ttyAMA3<br />
+
UART4 -> /dev/ttyAMA4<br />
+
 
+
* S5P6818平台开发板 (NanoPi M3/NanoPC-T3等)
+
UART1 -> /dev/ttySAC1 [注1]<br />
+
UART2 -> /dev/ttySAC2  [注1]<br />
+
UART3 -> /dev/ttySAC3<br />
+
UART4 -> /dev/ttySAC4<br />
+
<br />
+
注1: 仅部分板子可用,具体请查看原理图.
+
 
+
===3) 编译DEMO源代码,并导出apk文件===
+
在Package Explorer上点击项目名称,保证其处于选中状态:<br />
+
[[File:AndroidHW04.png|500px]]<br />
+
在eclipse菜单File上选择Export...:<br />
+
[[File:AndroidHW05.png|500px]]<br />
+
选择Export Android Application:<br />
+
[[File:AndroidHW06.png|500px]]<br />
+
选择项目SerialPortDemo:<br />
+
[[File:AndroidHW07.png|500px]]<br />
+
这一步,Eclipse要求我们创建或选择自已的签名,这里我们选择Create new keystore,  填写key生成后保存的路径和密码,然后点next:<br />
+
[[File:AndroidHW08.png|500px]]<br />
+
继续参考下图完善签名信息,然后点next:<br />
+
[[File:AndroidHW09.png|500px]]<br />
+
接着选择APK文件的保存位置,这里我们选择保存到 signapk-files子目录下,以方便我们稍后要进行的签名操作:<br />
+
[[File:AndroidHW10.png|500px]]<br />
+
点Finish完成APK的导出:<br />
+
[[File:AndroidHW11.png|500px]]
+
===4) 使用系统证书对APK进行重新签名===
+
因为访问硬件需要APK有system权限,所以需要使用系统证对APK进行重新签名,<br />
+
在命令行下进入signapk-files子目录,然后执行以下命令签名即可:
+
<syntaxhighlight lang="bash">
+
cd signapk-files/
+
java -jar ./signapk.jar platform.x509.pem platform.pk8 ./SerialPortDemo.apk SerialPortDemo-Signed.apk
+
</syntaxhighlight>
+
其中,signapk.jar以及签名文件platform.x509.pem和platform.pk8可在开发板资源下载目录的android-platform-key-files目录下找到。
+
 
+
===5) 运行与调试===
+
5.1) 安装APK,用microUSB线将电脑与开发板连接,用adb命令安装SerialPortDemo-Signed.apk:
+
<syntaxhighlight lang="bash">
+
adb install SerialPortDemo-Signed.apk
+
</syntaxhighlight>
+
 
+
5.2) 调试:
+
运行 SerialPortDemo后,在adb shell命令行模式下执行:
+
<syntaxhighlight lang="bash">
+
ps -Z
+
</syntaxhighlight>
+
然后找到com.friendlyarm.SerialPortDemo这个进程,如果前面的权限是如下所示,表明程序已经获得了 system 权限:
+
<syntaxhighlight lang="bash">
+
u:r:system_app:s0              system    1610  112  com.friendlyarm.SerialPortDemo
+
</syntaxhighlight>
+
以上是Android4的,如果是Android5,则略有不同,不过都表示已经是 system 权限:
+
<syntaxhighlight lang="bash">
+
u:r:platform_app:s0            u0_a60    1905  138  com.friendlyarm.SerialPortDemo
+
</syntaxhighlight>
+
如果程序没有获得 system 权限,则打开串口设备会失败。
+
 
+
==Resources==
+
* Code Samples: https://github.com/friendlyarm/AndroidHardwareAccess
+
* Guide to API in Chinese: https://github.com/friendlyarm/AndroidHardwareAccess/blob/master/友善电子Android硬件开发指南.pdf
+
 
+
==Update Log==
+
===March-2-2017===
+
* Released English version
+

Latest revision as of 08:22, 30 October 2018

查看中文

The Document has moved to: FriendlyThings