Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android iBeacon库-如何将IBeaconManager绑定到远程进程中的服务_Android_Ibeacon Android_Android Ibeacon - Fatal编程技术网

Android iBeacon库-如何将IBeaconManager绑定到远程进程中的服务

Android iBeacon库-如何将IBeaconManager绑定到远程进程中的服务,android,ibeacon-android,android-ibeacon,Android,Ibeacon Android,Android Ibeacon,我正在使用RadiusNetworks Android iBeacon库,我希望在后台进程中完成iBeacon测距,然后它将向我的应用程序广播信标信息。我尝试将IBMeaconManager绑定到在单独进程上运行的服务,但它似乎不起作用,因为该服务从未到达OnibeAccerviceConnect()回调 单独进程上的TestService: public class TestService extends Service implements IBeaconConsumer{ public

我正在使用RadiusNetworks Android iBeacon库,我希望在后台进程中完成iBeacon测距,然后它将向我的应用程序广播信标信息。我尝试将IBMeaconManager绑定到在单独进程上运行的服务,但它似乎不起作用,因为该服务从未到达OnibeAccerviceConnect()回调

单独进程上的TestService:

public class TestService extends Service implements IBeaconConsumer{

public static final String START_SERVICE = "com.example.intent.action.START";
public static final String SEND_PROXID = "com.example.intent.action.PROX";

private String proxID;
private static final String TAG = TestService.class.getSimpleName();
private IBeaconManager iBeaconManager;  
private Region currentRegion;
private boolean checkedRecently = false;

  private Timer timer;
  private TimerTask updateTask = new TimerTask() {
    @Override
    public void run() {
        checkedRecently = false;
         Log.i(TAG, "Resetting checked recently");
    }
  };

  @Override
  public int onStartCommand(Intent intent, int flags, int startId){
     int ret; 
     ret = super.onStartCommand(intent, flags, startId);
     Log.i(TAG, "Got to onStartCommand");
     if(intent.getAction().equals(START_SERVICE)){
         Log.i(TAG, "Service received start intent");
     }
     else if(intent.getAction().equals(SEND_PROXID)){
         Log.i(TAG, "Service received PROXID intent");
         proxID = intent.getStringExtra("prox");
         System.out.println(proxID);
         iBeaconManager = IBeaconManager.getInstanceForApplication(this);
        iBeaconManager.bind(this);
     }
     return ret;
       }

  @Override
  public IBinder onBind(Intent intent) {
      Log.i(TAG, "onBIND called");
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Service creating");

    timer = new Timer("TestTimer");
    timer.schedule(updateTask, 1000L, 20 * 1000L);

  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "Service destroying");

    timer.cancel();
    timer = null;
    iBeaconManager.unBind(this);
    iBeaconManager = null;
  }

  @Override
    public void onIBeaconServiceConnect() {
      Log.i(TAG, "iBeacon service connect");
        iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override 
        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
            if (iBeacons.size() > 0 && !checkedRecently) {
                checkedRecently = true;
                Log.i(TAG, "Service found beacon, sending data");
            Intent i = new Intent();
            i.setAction(ServiceReceiver.DID_ENTER);
            i.putExtra("maj", Integer.toString(iBeacons.iterator().next().getMajor()));
            i.putExtra("min", Integer.toString(iBeacons.iterator().next().getMinor()));
            sendBroadcast(i);
            }
        }
        });

        currentRegion = new Region("myRangingUniqueId", proxID, null, null);
        try {
            iBeaconManager.startMonitoringBeaconsInRegion(currentRegion);
        } catch (RemoteException e) {   }

    }

}
公共类TestService扩展服务实现IBeAconsumer{
公共静态最终字符串START\u SERVICE=“com.example.intent.action.START”;
公共静态最终字符串SEND_PROXID=“com.example.intent.action.PROX”;
私有字符串代理;
私有静态最终字符串标记=TestService.class.getSimpleName();
私有IBeaconManager IBeaconManager;
私人区域;
private boolean checkedRecently=false;
私人定时器;
专用TimerTask updateTask=新TimerTask(){
@凌驾
公开募捐{
checkedRecently=false;
Log.i(标签“最近检查的重置”);
}
};
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
int ret;
ret=super.onStartCommand(intent、flags、startId);
i(标记“gottoonstartcommand”);
if(intent.getAction().equals(启动服务)){
Log.i(标签“服务接收启动意图”);
}
else if(intent.getAction().equals(SEND_PROXID)){
Log.i(标记“服务接收代理意图”);
proxID=intent.getStringExtra(“prox”);
System.out.println(proxID);
iBeaconManager=iBeaconManager.getInstanceForApplication(此应用程序);
iBeaconManager.bind(这个);
}
返回ret;
}
@凌驾
公共IBinder onBind(意向){
Log.i(标记“onBIND called”);
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
Log.i(标记“服务创建”);
定时器=新定时器(“测试定时器”);
定时器时间表(更新任务,1000L,20*1000L);
}
@凌驾
公共空间{
super.ondestory();
Log.i(标签“服务销毁”);
timer.cancel();
定时器=空;
iBeaconManager.unBind(这个);
iBeaconManager=null;
}
@凌驾
public-void-onibeacerviceconnect(){
Log.i(标记“iBeacon服务连接”);
setRangeNotifier(新的RangeNotifier(){
@凌驾
public void DidRangeBeanConsignRegion(集合iBeacons,Region){
if(最近检查的iBeacons.size()>0&&{
checkedRecently=true;
Log.i(标签,“服务发现信标,发送数据”);
意图i=新意图();
i、 设置操作(ServiceReceiver.Dod_输入);
i、 putExtra(“maj”,Integer.toString(iBeacons.iterator().next().getMajor());
i、 putExtra(“min”,Integer.toString(iBeacons.iterator().next().getMinor());
发送广播(一);
}
}
});
currentRegion=新区域(“MyRangUniqueId”,proxID,null,null);
试一试{
iBeaconManager.StartMonitoringBeaconsistenregion(当前区域);
}捕获(远程异常e){}
}
}
AndroidManifest.xml

<service
        android:name=".TestService"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.example.intent.action.PROX" />
            <action android:name="com.example.intent.action.START" />
        </intent-filter>
    </service>
<receiver android:name="ServiceReceiver" >
        <intent-filter>
            <action android:name="com.example.intent.action.DIDENTER" >
            </action>
        </intent-filter>
    </receiver>


我从我的服务器获取相关的代理,并将其发送到服务,当时我认为我应该能够绑定到它,并开始搜索与prox匹配的区域。但我似乎无法让它成功绑定到服务。谢谢您的帮助,如果需要更新发布的代码,请告诉我。

我想知道您的
AndroidManifest.xml
是否获得了AndroidBeaconLibrary所需的正确条目。该库依赖一个名为manifest merging的功能将库的服务条目获取到您的清单中。这必须在Eclipse或Android Studio中的项目中启用。检查生成的清单的生成目录,并确保它有一个用于
androidbeacervice
的条目

也就是说,创建自己的后台服务可能有点过火。要知道,已经有了一个后台
androidibeaconservatice
类(如上所述),所以您实际上不需要创建自己的服务。您所需要做的就是将服务绑定到比活动生命周期更长的东西上(比如定制的Android
应用程序
类),这样当活动解除绑定时,服务不会停止

有一个软件可以为您设置所有这些,当您的应用程序在后台时减慢扫描速度以节省电池,并在后台自动启动您的应用程序以搜索iBeacons,即使用户没有手动启动它。“后台启动示例代码”部分提供了演示如何使用Pro版本执行此操作的示例代码


全面披露:我是Radius网络的总工程师,也是Android iBeacon库的主要作者。

感谢您的及时回复,大卫!我在项目属性中的manifestmerging为true,不确定生成的清单在哪里;当我绑定到前景中的活动时,测距效果非常好,但当活动停止或暂停时,测距当然会停止。我试着让我用于globals的应用程序单例扩展应用程序并绑定到该应用程序,但我总是遇到同样的错误。也许你能理解它们,我会更新帖子的。恐怕我有点糊涂了。您提到您“尝试让我用于globals的应用程序单例扩展应用程序并绑定到该应用程序,但我始终会收到相同的错误”,并显示一个似乎适用于此尝试的堆栈跟踪。但是您没有显示来自该应用程序类的代码,问题仍然显示了服务的使用。我建议您将这个问题与使用服务联系起来(如果我的回答没有解决问题,您可能不应该接受)