一(yī)些(xiē)Android
手機(jī)是(shì)有(yǒu)串口可(kě)以提供打開(kāi↑•≈λ),讀(dú)取一(yī)些(xiē)底♥β¥層的(de)數(shù)據。如(rú)果手機( ¥₩↓jī)本身(shēn)沒有(yǒu)串口的(de)話(h•↑uà),那(nà)麽打開(kāi)估計(jì)會♣α≥(huì)報(bào)錯(cuò)。串口讀(≠§dú)取數(shù)據肯定是(shì)使用(yòng)§←☆jni調用(yòng)c代碼來(lái)完成的(de)。有(yǒu)一(yī)個(gè)開(kāi)源的(de)串口庫androi₹₹ d-serialport-api。其主頁在這(zhè)裡(£₽≠lǐ)http://code.google.com/↔ p/android-serialport-api/ &nb≠≠•sp;,這(zhè)裡(lǐ)可(kě)以下(xià)到(dào)♣↔&APK及對(duì)源碼。谷歌(gē)的(de)代碼庫,無奈國(guó)內(nèi)無法下(x✔>€ià)載https://github.com/cepr/android-ser☆™✘ialport-api ,GITHUB的(de)地(dì)址,這(zhδ ∏è)個(gè)可(kě)以下(xià)載 但(dàn)是(shì)下(xΩ ià)載源碼之後發現(xiàn)源碼不(bù)能(néng&∑)直接使用(yòng),而且源碼結構較為(w₩• èi)複雜(zá)。
在這(zhè)裡(lǐ)我是(shì)使用(yòng)服務來(lái↔>)讀(dú)取串口數(shù)據,都(dōu)出來(lái¶≈₽)數(shù)據直接寫入到(dào)txt文(wén)件(jiàn)™←ε"裡(lǐ)面。
串口讀(dú)寫的(de)庫的(de)地(dì)址:https://code ✘.google.com/p/android-seriβ¥alport-api/
github地(dì)址:https://github.com/cepr/androΩ£id-serialport-api
本篇博客的(de)下(xià)載地(dì)址:http://dow•₽®©nload.csdn.net/detail/qq★★_16064871/9731908
1,注意事(shì)項
選擇串口時(shí)候,需要(yào)加上(shàng)&₽π'dev/。如(rú)果我選擇ttyMT2,路(lù)徑就(jiù)是©"±(shì)dev/ttyMT2。波特率就(jiù)是(∏$shì)整型38400,或者其他(tā)。

打開(kāi)串口以及讀(dú)取數(shù)據的(de)串口類♣↓"₹調用(yòng)加載so庫一(yī)定放(₽¥fàng)在這(zhè)個(gè)包下(xià)面android_seriaβ♦lport_api。不(bù)能(néng)放(fàng)在其他(tā)包的(↕←de)下(xià)面。
SerialPortDevice.class
-
- package android_serialportδ_api;
-
- import java.io.FileDescriptor;
- import java.io.FileIn✔¶πputStream; &nbs→"p;
- import java.io.FileOutputStream♦★γ;
- import java.io.InputStream$♦±;
- import java.io.OutputStream;  <∞&✔;
-
- import android.util.L×✘og;
-
- public class SerialPortDevice{&nbsσ★p;
-
- private FileDescriptor mFd;
- &nγ∞↑bsp;private InputStream mInputγ∞★πStream;
- private OutputStream mOutputStre≈πam;
-
- private String path;  &✘;
-  $ε;private int baudrate;
- &n€&bsp;int flags=0;
-
- public SerialPortDevice(String&n∏✘bsp;path, int baudrate, int flags) {  ®€≤;
- &≤π₩®nbsp; mFd&★≤ ±nbsp;= new FileDescriptor();
- &n®δbsp; this.path=path;  ✔✔λ;
- &nb♠₹ φsp; this.baudrate=baudrate;
- &₹₩nbsp; this.flags=flags;
- }&nbs"p;
-
-
- public boolean connect() {<←
- &nbs∏∞★↕p; mFd π♦♦✘= open(path, b∏©audrate, 0);
-  ≥♦≠; if (mFd == null) {
- &nbΩ €sp;  ₽₹×;return false;
- &n≥∏bsp; }
- &nbs≠☆©p; else
-  ∞↔Ω; {  •✔×;
- &n λ♦bsp;  £∑γ; return true;
- &nΩ÷<₩bsp; }
- β×¥≈}
-
-  α®;
- &nbs≈✔p;public InputStream getInputStream()↔ {
- &nbsδ™p;
- &nbsλ₩$p; return new FileInputStream(mFd);&nbs≠★>p;
- ↑}
-
- &nbs☆÷€p;