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

From FriendlyELEC WiKi
Jump to: navigation, search
(Compile & Deploy Andorid Code (SerialPortDemo))
(updated by API)
 
(7 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 />
+
 
+
====Serial Device Names under Multiple Platforms====
+
* Allwinner H3/H5 Based Boards (NanoPi M1/NanoPi M1 Plus etc)
+
UART1-> /dev/ttyS1<br />
+
UART2 -> /dev/ttyS2<br />
+
UART3 -> /dev/ttyS3 (Only applies to NanoPi M1, NanoPi K1 and NanoPi K1 Plus)<br />
+
 
+
* S5P4418 Based Boards (NanoPi Fire2A/NanoPi M2A/NanoPi S2/NanoPC-T2 etc)
+
UART1 -> /dev/ttyAMA1  [Note 1]<br />
+
UART2 -> /dev/ttyAMA2  [Note 1]<br />
+
UART3 -> /dev/ttyAMA3<br />
+
UART4 -> /dev/ttyAMA4<br />
+
 
+
* S5P6818 Based Boards (NanoPi M3/NanoPC-T3 etc)
+
UART1 -> /dev/ttySAC1 [Note 1]<br />
+
UART2 -> /dev/ttySAC2  [Note 1]<br />
+
UART3 -> /dev/ttySAC3<br />
+
UART4 -> /dev/ttySAC4<br />
+
<br />
+
Note 1: only applies to specific boards, you need to check if a board has that serial device populated.
+
 
+
===Step 3. Compile DEMO's source code and export apk file===
+
On "Package Explorer" check your project's name:<br />
+
[[File:AndroidHW04.png|500px]]<br />
+
On eclipse's main menu select "File" and then "Export...":<br />
+
[[File:AndroidHW05.png|500px]]<br />
+
Select "Export Android Application":<br />
+
[[File:AndroidHW06.png|500px]]<br />
+
Select "SerialPortDemo":<br />
+
[[File:AndroidHW07.png|500px]]<br />
+
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":<br />
+
[[File:AndroidHW08.png|500px]]<br />
+
Refer to the following screenshot to fill more information and click on "next":<br />
+
[[File:AndroidHW09.png|500px]]<br />
+
Select a path to store your APK file. In this case we set it to a "signapk-files" directory:<br />
+
[[File:AndroidHW10.png|500px]]<br />
+
Click on "Finish" to export an APK file.<br />
+
[[File:AndroidHW11.png|500px]]
+
===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.<br />
+
Enter the signapk-files directory and give your APK a new signature:
+
<syntaxhighlight lang="bash">
+
cd signapk-files/
+
java -jar ./signapk.jar platform.x509.pem platform.pk8 ./SerialPortDemo.apk SerialPortDemo-Signed.apk
+
</syntaxhighlight>
+
The "signapk.jar", "platform.x509.pem" and "platform.pk8" files are available under the "android-platform-key-files" directory.
+
 
+
===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":
+
<syntaxhighlight lang="bash">
+
adb install SerialPortDemo-Signed.apk
+
</syntaxhighlight>
+
 
+
5.2) Debug:
+
After SerialPortDemo is started run the following command in "adb shell":
+
<syntaxhighlight lang="bash">
+
ps -Z
+
</syntaxhighlight>
+
Check this com.friendlyarm.SerialPortDemo" process. If its right is as follows that means this application already has "system" right:
+
<syntaxhighlight lang="bash">
+
u:r:system_app:s0              system    1610  112  com.friendlyarm.SerialPortDemo
+
</syntaxhighlight>
+
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:
+
<syntaxhighlight lang="bash">
+
u:r:platform_app:s0            u0_a60    1905  138  com.friendlyarm.SerialPortDemo
+
</syntaxhighlight>
+
If your application doesn't have "system" right it will fail to open a device.
+
 
+
==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