gpt4 book ai didi

android - 在后台估计信标检测器服务

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:12 24 4
gpt4 key购买 nike

我正在尝试在 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();
}
}

最佳答案

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

关于android - 在后台估计信标检测器服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com