樹莓派+Android Things

在開始以前
谷歌前不久發佈了Android Things面向物聯網的系統,用意是想讓android開發者用原來開發app的方式開發硬件相關的應用,擴展了android開發的方向和前景,而谷歌的Android Things已經支持了主流的幾個面向互聯網的幾個芯片( Intel® Edison, NXP Pico i.MX6UL, Raspberry Pi 3鑑於開發難度和集成度高低的選擇,選擇了樹莓派做( Raspberry Pi 3 )做爲實踐的。
1.材料準備
材料均可以在淘寶上輕易購買的,包括樹莓派主板,sdcard(最好8g或者以上),sdcard讀卡器,電源,HDMI轉VGA轉換器(若是顯示器支持hdmi能夠直接用HDMI線,由於樹莓派主板視頻口是HDMI),網線。

2.刷系統鏡像
(1)首先須要下載Android Things系統鏡像,目前最新的android Things是預覽版,能夠到下面的地址下載相對應芯片的系統鏡像。
(2)用sdcard讀卡器鏈接sdcard到電腦上。
(3)解壓剛纔下載的系統鏡像。
(4)須要下載寫入鏡像到sdcard的工具根據不一樣系統本身選擇,本教程首選windows系統能夠到 https://sourceforge.net/projects/win32diskimager/   這個網站下載 Win32 Disk Imager工具。運行win32 DiskImager ,最右邊選擇你sdcard的盤符(備註不要選到了其餘的硬盤或移動存儲設備,致使形成沒必要要的損失),而後選擇鏡像路徑,再而後點擊write等待寫入結果。寫入完後把sdcard插到樹莓派上,插上網線(網線另外一端鏈接路由),插上視頻輸出線鏈接到顯示器,USB數據線鏈接電腦到樹莓派,樹莓派就會開始啓動系統了,系統啓動後,屏幕會出現局域網的ip。
(5)打開命令行工具,輸入下面命令(ip -address就是顯示器上android things系統顯示的界面),不出狀況的話就提示 connected to < ip - address >: 5555  這樣就成功鏈接到樹莓派。
adb connect <ip-address>

3.鏈接WIFI
若是不想每次調試都要電腦鏈接樹莓派的話,能夠設置樹莓派鏈接wifi(必需要完成上面鏈接到樹莓派才能設置無線wifi鏈接)
(1)發送意圖到Android Things的wifi服務模塊的命令以下
$ adb shell am startservice \
   
-n com.google.wifisetup/.WifiSetupService \
   
-a WifiSetupService.Connect \
   
-e ssid <Network_SSID> \
   
-e passphrase <Network_Passcode>


(2)經過adb 的logcat肯定你的鏈接是否成功
$ adb logcat -d | grep Wifi

 輸入命令行後,不出狀況通常出現下面的輸出
...
V
WifiWatcher: Network state changed to CONNECTED
V
WifiWatcher: SSID changed: ...
I
WifiConfigurator: Successfully connected to ...

(3)測試你的樹莓派是否能訪問外網,命令
$ adb shell ping 8.8.8.8
PING
8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=6.67 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=55.5 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=57 time=23.0 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=57 time=245 ms

4.編寫Android Things程序
在開始編寫Android Things程序以前,
(1)確保你的sdK tools版本是24huo或者24以上
(2)確保你的sdk 支持API支持24或者更高版本。

添加依賴
(1)往app的模塊裏的build.gradle文件添加以下依賴
dependencies {
   
...
    provided
'com.google.android.things:androidthings:0.1-devpreview'
}

(2)往   manifest .xml文件添加
  1. <application ...>
       
    <uses-library android:name="com.google.android.things"/>
        ...
    </application>

聲明主Activity

要把應用運行在嵌入式設備(本教程中的樹莓派),必須包含一個   CATEGORY_LAUNCHER   這樣的Intent Filter,這樣,才能在部署和調試應用的時候,Android Studio才能啓動默認的Activity。
具體須要在Manifest聲明的設置以下。

<application
   
android:label="@string/app_name">
   
<uses-library android:name="com.google.android.things"/>
   
<activity android:name=".HomeActivity">
       
<!-- Launch activity as default from Android Studio -->
       
<intent-filter>
           
<action android:name="android.intent.action.MAIN"/>
           
<category android:name="android.intent.category.LAUNCHER"/>
       
</intent-filter>

       
<!-- Launch activity automatically on boot -->
       
<intent-filter>
           
<action android:name="android.intent.action.MAIN"/>
           
<category android:name="android.intent.category.IOT_LAUNCHER"/>
           
<category android:name="android.intent.category.DEFAULT"/>
       
</intent-filter>
   
</activity>
</application>









相關文章
相關標籤/搜索