Linkingアプリ接続/切断検知方法
次に、Linkingアプリと周辺デバイスの接続/切断を検知する方法について説明します。
NotifyConnectクラスにobserver(NotifyConnect.ConnectInterface)を設定することで接続/切断通知を受けることが可能になります。
そして、通知を受けた時の情報は、以下の名称でローカルデータとしてSharedPreferencesに格納されます。
- 接続情報:DeviceConnectInformation
- 切断情報:DeviceDisconnectInformation
今回はLinkingServiceを実装することで、バックグラウンドでも周辺デバイスに対する接続/切断を検知しNotificationを表示できるようにします。
import com.nttdocomo.android.sdaiflib.Define;
import com.nttdocomo.android.sdaiflib.NotifyConnect;
public class LinkingService extends Service {
private NotifyConnect mNotifyConnect;
private MyConnectInterface mMyConnectInterface;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mMyConnectInterface = new MyConnectInterface();
mNotifyConnect = new NotifyConnect(this, mMyConnectInterface);
}
@Override
protected void onDestroy() {
super.onDestroy();
mNotifyConnect.release();
}
private void sendNotification(String title, String text){
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle(title)
.setContentText(text);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(0, notificationBuilder.build());
}
// 接続と切断の通知を受信する
class MyConnectInterface implements NotifyConnect.ConnectInterface {
@Override
public void onConnect() {
// 接続通知
SharedPreferences preference = getSharedPreferences(Define.ConnectInfo, Context.MODE_PRIVATE);
String deviceName = preference.getString("DEVICE_NAME", "");
sendNotification("接続通知", "デバイス[" + deviceName +"]が接続されました");
}
@Override
public void onDisconnect() {
// 切断通知
SharedPreferences preference = getSharedPreferences(Define.DisconnectInfo, Context.MODE_PRIVATE);
String deviceName = preference.getString("DEVICE_NAME", "");
sendNotification("切断通知", "デバイス[" + deviceName +"]切断されました");
}
}
}
ビーコンからデータを受信
次に、ビーコンからデータを受信する方法について説明します。
BeaconScannerクラスを利用することで、ビーコンのスキャン開始/停止依頼を行うことができます。スキャンは定期的に実行され、ビーコンが検出された場合にはビーコンの情報を取得することができます。
マニフェストファイルには、ビーコン機器通知対応の宣言を記載してください。また受信するレシーバークラスには、IntentFilterの設定が必要です。
<application>
~中略~
<meta-data
android:name="com.nttdocomo.android.smartdeviceagent.feature.beacon" android:value="true" />
~中略~
<receiver
android:name=".MainActivity$MyBeaconReceiverBase"
android:exported="true" >
<intent-filter>
<action android:name="com.nttdocomo.android.smartdeviceagent.action.BEACON_SCAN_RESULT" />
<action android:name="com.nttdocomo.android.smartdeviceagent.action.BEACON_SCAN_STATE" />
</intent-filter>
</receiver>
</application>
import com.nttdocomo.android.sdaiflib.BeaconData;
import com.nttdocomo.android.sdaiflib.BeaconReceiverBase;
import com.nttdocomo.android.sdaiflib.BeaconScanner;
public class MainActivity extends Activity {
private BeaconScanner mBeaconScanner;
private MyBeaconReceiverBase mMyBeaconReceiverBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
~中略~
mBeaconScanner = new BeaconScanner(this);
mMyBeaconReceiverBase = new MyBeaconReceiverBase();
}
public void onStartBeaconScan(View view) {
// サービスIDを指定
// すべての検出されたビーコンサービスID/データが含まれるビーコン情報を通知
mBeaconScanner.startScan(new int[] {0} );
}
public void onStopBeaconScan(View view) {
// スキャン停止依頼
mBeaconScanner.stopScan();
}
// ビーコンスキャンの結果を受信するレシーバー
public static class MyBeaconReceiverBase extends BeaconReceiverBase {
public MyBeaconReceiverBase() {
}
@Override
protected void onReceiveScanResult(BeaconData beaconData) {
Log.d(TAG, "ビーコン検出時間" + beaconData.getTimestamp());
Log.d(TAG, "距離の種別:" + beaconData.getDistance());
Log.d(TAG, "バッテリー:" + beaconData.getBatteryPower() + "%");
}
@Override
protected void onReceiveScanState(int scanState, int detail) {
if (scanState == 0 && detail == 0) {
Log.d(TAG, "スキャン実行中");
} else {
Log.d(TAG, "スキャン要求に失敗しました : " + detail);
}
}
}
}

