Android BeaconSDK Tutorial
於 2016年8月15日 (一) 10:06 由 imported>Wikiuser 所做的修訂
- 廣告請求
- 請在您程式開始的Activity加入下列程式碼,以啟動TWMBeaconSDK,啟動成功之後SDK會在背景偵測附近是否有Beacon存在,並且依據Beacon的相關資訊進行廣告請求,您不需再做任何額外設定。
TWMBeaconSDK.init(MainActivity.this, 您的版位ID, new TWMBeaconRequest.Builder.build());
- TWMBeaconRequest
- TWMBeaconRequest為請求設定檔 ,提供的function及功能說明如下,請依您的需求進行設定。
- 設定要求使用者開啟藍芽之文字
setBluetoothDialogSettings(BluetoothDialogSettings settings)
- 您可以針對Title、Message、positive button以及negative button設定您想要顯示的文字,預設文字如下圖所示,您可以不變更此設定。
BluetoothDialogSettings settings = new BluetoothDialogSettings(); settings.setMessage("Message"); settings.setTitle("Title"); settings.setNegativeButtonText("NegativeButton"); settings.setPositiveButtonText("PositiveButton");
- 設定通知列small icon
- 強烈建議將通知icon設為您希望顯示的icon,若未設定則會以app icon作為替代。
setNotificationIcon(int icon)
- 設定掃描模式
- TWMBeaconSDK提供與原生方法相同的三種掃描模式預設為LowPower模式,若無特殊需求,建議您不需設定此選項。
SCAN_MODE_BALANCED , SCAN_MODE_LOW_LATENCY ,SCAN_MODE_LOW_POWER
setScanMode(ScanMode scanMode)
- 設定要求使用者開啟權限之文字(6.0以上需要)
- 此方法與setBluetoothDialogSettings類似,請參考上述說明。
- 設定測試模式
- 測試模式所有的請求將不會被記錄,傳入的testDevice字串請您參考Android_Banner_Ads_I_2.0
addTestDevice(String testDevice) setTestDevices(Set<String> testDevices)
- 加入TWMBeaconListener監聽廣告投放情況
TWMBeaconSDK.setAdListener(new TWMBeaconListener() { @Override public void onReceiveAd() { // 成功收到廣告時通知 } @Override public void onFailedToReceiveAd(TWMBeaconRequest.ErrorCode errorCode) { // 當請求廣告失敗時通知 } @Override public void onPresentScreen() { // 廣告呈現在頁面時通知 } @Override public void onDismissScreen() { // 關閉廣告時通知 } @Override public void onFailedStart (String errorMessage) { // 無法成功開啟服務時通知 } });
- Android 6.0 Runtime Permissions
- 如果您的應用程式target版本為Android 6.0以上,由於權限的問題,請您在啟動TWMBeaconSDK的activity override onRequestPermissionsResult function
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {; TWMBeaconSDK.onRequestPermissionsResult( requestCode, permissions, grantResults); }
- 完整範例請參考下列
- Activity
package com.taiwanmobile.beacontest; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.taiwanmobile.beaconsdk.settings.BluetoothDialogSettings; import com.taiwanmobile.beaconsdk.internal.TWMBeaconRequest; import com.taiwanmobile.beaconsdk.internal.TWMBeaconListener; import com.taiwanmobile.beaconsdk.internal.TWMBeaconSDK; import com.taiwanmobile.beaconsdk.service.TWMBeaconService; public class MainActivity extends AppCompatActivity { TWMBeaconRequest request; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //設定要求開啟藍芽提醒文字 BluetoothDialogSettings settings = new BluetoothDialogSettings(); settings.setMessage("Message"); settings.setTitle("Title"); settings.setNegativeButtonText("NegativeButton"); settings.setPositiveButtonText("PositiveButton"); //設定要求權限提醒文字 PermissionDialogSettings permissionDialogSettings = new PermissionDialogSettings(); permissionDialogSettings.setMessage("Message"); permissionDialogSettings.setTitle("Title"); permissionDialogSettings.setNegativeButtonText("NegativeButton"); permissionDialogSettings.setPositiveButtonText("PositiveButton"); //設定TWMBeaconRequest request = new TWMBeaconRequest.Builder() //設定通知列icon .setNotificationIcon(YOUR_ICON) .setBluetoothDialogSettings(settings) .setPermissionDialogSettings(permissionDialogSettings) .build(); //啟動TWMBeaconSDK TWMBeaconSDK.init(MainActivity.this, "您的版位ID", request); TWMBeaconSDK.setAdListener(new TWMBeaconListener() { @Override public void onReceiveAd() {); } @Override public void onFailedToReceiveAd(TWMBeaconRequest.ErrorCode errorCode) { } @Override public void onFailedStart(String errorMessage) { } @Override public void onPresentScreen() { } @Override public void onDismissScreen() { } }); }
//6.0動態權限必須 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { TWMBeaconSDK.onRequestPermissionsResult( requestCode, permissions, grantResults); } }
- Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.taiwanmobile.beacontest"> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.taiwanmobile.beaconsdk.internal.TWMRedirectActivity" android:launchMode="singleInstance"/> <activity android:name="com.taiwanmobile.beaconsdk.internal.TWMBeaconActivity" android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize" android:hardwareAccelerated="true" android:launchMode="singleTask" android:showOnLockScreen="true" android:taskAffinity="" android:excludeFromRecents="true" android:theme="@android:style/Theme.Translucent"/> <service android:name="com.taiwanmobile.beaconsdk.service.TWMBeaconService" /> </application> </manifest>