4

我正在尝试在 Android 上创建一个可以找到 Estimote 信标并向用户推送通知的服务。我已经下载了示例代码,并使用该代码创建了新的 myService Activity。代码正在运行,我可以在 LogCat 中看到 Android 正在扫描信标。不幸的是,没有找到信标。我的代码有什么问题?

或者,也许我做错了?

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();
    }
}
4

1 回答 1

0

是的,那是因为您没有更改信标的 UUID、MAJOR 和 MINOR 编号。在您的手机上下载 estimote sdk 应用程序,然后您可以将所有三个号码替换为您已有的号码。它将开始工作。

于 2016-07-19T15:55:42.270 回答