1

您好,我目前正在使用 Radius Network Beacon SDK,但无法检测到我的信标。在 didRangeBeaconsInRegion(Collection beacons, Region region) 集合对象大小为 0

请帮我!!

测距活动代码-

import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.EditText;
import org.altbeacon.beacon.AltBeacon;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

public class RangingActivity extends Activity implements BeaconConsumer {
  protected static final String TAG = "RangingActivity";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);

    beaconManager.bind(this);
    beaconManager.debug = true;
}
@Override 
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
@Override 
protected void onPause() {
    super.onPause();
}
@Override 
protected void onResume() {
    super.onResume();
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            EditText editText = (EditText)RangingActivity.this
                    .findViewById(R.id.rangingText);
            Beacon firstBeacon = beacons.iterator().next();
            logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away.");
        }
    }



    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        //beaconManager.updateScanPeriods();
    } catch (RemoteException e) {   }
}
private void logToDisplay(final String line) {
    runOnUiThread(new Runnable() {
        public void run() {
            EditText editText = (EditText)RangingActivity.this
                    .findViewById(R.id.rangingText);
            editText.append(line+"\n");             
        }
    });
}

}

4

2 回答 2

4

您需要一个自定义解析器来正确识别该信标。

看看这个答案:

这是使用 AltBeacon 的 Android 信标库检测 iBeacons 的正确布局吗?

于 2014-08-20T07:31:25.117 回答
0

你需要onBeaconConnect()在你的onCreate函数中调用

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_ranging);

  beaconManager.bind(this);
  beaconManager.debug = true;

  onBeaconConnect()
}
于 2014-08-25T06:52:24.987 回答