Estimote ビーコンを見つけてユーザーに通知をプッシュするサービスを Android で作成しようとしています。サンプル コードをダウンロードし、そのコードを使用して新しい myService アクティビティを作成しました。コードが実行されており、Android がビーコンをスキャンしていることを LogCat で確認できます。残念ながら、ビーコンは見つかりませんでした。コードの何が問題になっていますか?
それとも、私はこれを間違った方法でやっていますか?
package com.osos.service;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.BeaconManager.MonitoringListener;
import com.estimote.sdk.BeaconManager.RangingListener;
import com.estimote.sdk.Region;
import com.estimote.sdk.utils.L;
import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MyService extends Service{
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
Beacon beacon;
private NotificationManager myNotificationManager;
private NotificationManager notificationManager;
private static final String TAG = "MyService";
private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
// beaconManager.setRangingListener(new RangingListener() {
//
// @Override
// public void onBeaconsDiscovered(Region arg0, List<Beacon> beacons_list) {
// // TODO Auto-generated method stub
//
// }
// });
Toast.makeText(this, "Jus paleidote OSOS serviza", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
L.enableDebugLogging(true);
Log.d(TAG, "onStartCOmmand");
try{
connectToService();
}catch(Exception e){
System.out.println("### problem ####");
System.out.println(e);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCOmmand");
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
switch(beacon.getMinor()){
case 11111: displayNotificationOne(1,"This is OSOS");
break;
default: displayNotificationOne(1,"This is Sparta");
}
}
@Override
public void onExitedRegion(Region region) {
displayNotificationOne(2,"Exited OSOS");
}
});
return startId;
}
protected void displayNotificationOne(int i, String msg) {
Log.d(TAG, "Display notif");
// Invoking the default notification service
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle("Simplify");
mBuilder.setContentText("Jus turite nauju uzduociu");
mBuilder.setTicker("Simplify: Jus turite nauju uzduociu");
mBuilder.setSmallIcon(R.drawable.arrow_up_float).setAutoCancel(true).build();
long[] vibrate = {0,200,500 };
mBuilder.setVibrate(vibrate);
//mBuilder.setSound(Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.groan));
// Increase notification number every time a new notification arrives
mBuilder.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
// Intent resultIntent = new Intent(this, Welcome.class);
// This ensures that navigating backward from the Activity leads out of
// the app to Home page
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent
// stackBuilder.addParentStack(StartedReviewsActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
// stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_ONE_SHOT // can only be used once
);
// start the activity when the user clicks the notification text
mBuilder.setContentIntent(resultPendingIntent);
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// pass the Notification object to the system
myNotificationManager.notify(1, mBuilder.build());
}
private void connectToService() {
Log.e(TAG, "Connect to service");
// getActionBar().setSubtitle("Scanning...");
// adapter.replaceWith(Collections.<Beacon>emptyList());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Log.e(TAG, "Cannot start ranging", e);
}
}
});
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
beaconManager.disconnect();
}
}