2016年11月18日 星期五

【API】引用注意事項

=====================================
問題環境
OS:Windows 10
開發平台:Android
React Native : 0.36.0
日期:2016/11/18
=====================================
最近在引用API時常發生錯誤,而且都是在手動設定的部分,因此我想做個範例提醒自己。

以下舉


這個控制藍芽的API為例。
教學開始(以下全為android平台):

首先,在React Native專案下開命令提示字元(CMD)

安裝package (專案名稱\專案資料夾下)
npm i -S react-native-bluetooth-serial

連結NPM 資料庫(專案名稱/專案資料夾下)
react-native link

Android平台需要設定權限(\專案資料夾下\android\app\src\main\AndroidManifest.xml)

<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


接下來就是比較難的,手動設定

設定引用(專案名稱\android\app\src\main\java\com\專案名稱\MainApplication.java)
import com.rusel.RCTBluetoothSerial.*;


接續上一步,設定getPackages()
new RCTBluetoothSerialPackage()

設定引用(專案名稱\android\settings.gradle)
include ':react-native-bluetooth-serial' project(':react-native-bluetooth-serial').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bluetooth-serial/android') 

設定dependencies block(專案名稱\android\app\build.gradle)
  compile project(':react-native-bluetooth-serial')


大功告成。





沒有留言:

張貼留言