使用来自https://altbeacon.github.io/android-beacon-library/samples.html的代码尝试检测 ios 8 的 IBeacon 发射器,如下所示http://blog.passkit.com/configure-iphone-ibeacon -transmitter/并且在哪一步出错时有点失落,我是否需要在 Region 中使用 BeaconIdentifier 作为 myMonitoringUniqueId?无法检测到 IBeacon .... 我是否需要额外的步骤才能从 ios 收听 IBeacon,因为使用的库是 Android Beacon 库
还添加了
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")) ;
如在这是使用 AltBeacon 的 Android 信标库检测 iBeacons 的正确布局吗?
BeaconIdentifier = @"com.example.apple-samplecode.AirLocate";
import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.TextView;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.logging.LogManager;
public class MainActivity extends Activity implements BeaconConsumer{
protected static final String TAG = "MonitoringActivity";
private BeaconManager beaconManager;
private TextView message;
private TextView num;
private int IBeaconCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView)findViewById(R.id.IBeaconMsg);
num = (TextView)findViewById(R.id.Numdetect);
beaconManager = BeaconManager.getInstanceForApplication(this);
/*
* beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
* */
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
//
beaconManager.bind(this);
//Hz to see if its receiveing from the emitter
//beaconManager.setForegroundScanPeriod(2000l);
//beaconManager.setBackgroundBetweenScanPeriod(1100l);
//
//IBeaconCount = 0;
//num.setText(IBeaconCount);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
protected void onResume() {
super.onResume();
//num.setText(IBeaconCount);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
//IBeaconCount++;
Log.i(TAG, "I just saw an beacon for the first time!");
message.setText("I just saw an beacon for the first time!");
LogManager.d(TAG, "didEnterRegion %s", region);
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
message.setText("I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
message.setText("I have just switched from seeing/not seeing beacons: "+state);
}
});
try {
//beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
beaconManager.startMonitoringBeaconsInRegion(new Region("com.example.apple-samplecode.AirLocate", null, null, null));
} catch (RemoteException e) { }
}
}
仍然没有显示 didEnterRegion 编辑文件日志的日志消息:
04-08 12:45:39.283 7223-7223/com.mobinteg.ibeacon W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
04-08 12:45:39.283 7223-7223/com.mobinteg.ibeacon W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
04-08 12:45:39.303 7223-7223/com.mobinteg.ibeacon I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.24.00.08
Build Date: 03/21/14 Fri
Local Branch: AU200+patches_03212014
Remote Branch:
Local Patches:
Reconstruct Branch:
04-08 12:45:39.343 7223-7223/com.mobinteg.ibeacon D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 12:45:39.413 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-08 12:45:39.453 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-08 12:45:39.483 7223-7262/com.mobinteg.ibeacon I/System.out﹕ Thread-1162(HTTPLog):isShipBuild true
04-08 12:45:39.483 7223-7262/com.mobinteg.ibeacon I/System.out﹕ Thread-1162(HTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
04-08 12:45:39.523 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=64:98:8D:4F:33:00 RSSI=-45
04-08 12:45:39.573 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=6A:7F:9C:42:A0:00 RSSI=-80
04-08 12:45:40.123 7223-7262/com.mobinteg.ibeacon W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
04-08 12:45:40.563 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
04-08 12:45:40.563 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-08 12:45:40.563 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-08 12:45:40.603 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=64:98:8D:4F:33:00 RSSI=-45
04-08 12:45:40.673 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=6A:7F:9C:42:A0:00 RSSI=-80
04-08 12:45:41.673 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
04-08 12:45:41.673 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-08 12:45:41.673 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-08 12:45:41.713 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=64:98:8D:4F:33:00 RSSI=-44
04-08 12:45:42.783 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
04-08 12:45:42.803 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-08 12:45:42.813 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-08 12:45:42.883 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=64:98:8D:4F:33:00 RSSI=-44
04-08 12:45:43.053 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=6A:7F:9C:42:A0:00 RSSI=-87
04-08 12:45:43.933 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
04-08 12:45:43.943 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-08 12:45:43.953 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-08 12:45:44.003 7223-7248/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=6A:7F:9C:42:A0:00 RSSI=-79
04-08 12:45:44.013 7223-7246/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=64:98:8D:4F:33:00 RSSI=-39
04-08 12:45:45.063 7223-7223/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
----------------------------------------------编辑2-- -------------------------------------------------- ------
在 onBeaconServiceConnect 中添加了 RangeNotifier,如下所示,但它从不进入 didRangeBeaconsInRegion 和 didEnterRegion、didExitRegion 和 didDetermineStateForRegion 仅在应用程序启动时触发一次 如何使其所有列表仅在应用程序关闭时停止 有人能指出如何做到这一点吗?
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.i(TAG,"beacons.size less then 0???????");
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
}
/*
for(Beacon beacon: beacons){
Log.i(TAG, "Beacon detected with id1: "+beacon.getId1()+" id2:"+beacon.getId2()+" id3: "+beacon.getId3()+" distance: "+beacon.getDistance());
}
*/
}
});
日志文件:
04-09 12:28:16.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Waiting to stop scan cycle for another 1100 milliseconds
04-09 12:28:16.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Scan started
04-09 12:28:16.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Set scan periods called with 1100, 110 Background mode must have changed.
04-09 12:28:16.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ We are not in the background. Cancelling wakeup alarm
04-09 12:28:16.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ cancel wakeup alarm: null
04-09 12:28:16.433 27850-27874/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=73:A5:A7:D6:24:06 RSSI=-67
04-09 12:28:16.433 27850-27874/com.mobinteg.ibeacon D/CycledLeScannerForJellyBeanMr2﹕ got record
04-09 12:28:16.443 27850-27872/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=D0:4F:7E:0A:88:5B RSSI=-92
04-09 12:28:16.443 27850-27872/com.mobinteg.ibeacon D/CycledLeScannerForJellyBeanMr2﹕ got record
04-09 12:28:16.443 27850-27891/com.mobinteg.ibeacon D/BeaconParser﹕ This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 02011a0bff4c0009060102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
04-09 12:28:16.443 27850-27891/com.mobinteg.ibeacon D/BeaconParser﹕ This is not a matching Beacon advertisement. (Was expecting 02 15. The bytes I see are: 02011a0bff4c0009060102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
04-09 12:28:16.443 27850-27890/com.mobinteg.ibeacon D/BeaconParser﹕ This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 02011a1aff4c00021513ba60dbdcdb42ec99a33f7eef7bbdbd00000000c50000000000000000000000000000000000000000000000000000000000000000
04-09 12:28:16.443 27850-27890/com.mobinteg.ibeacon D/BeaconParser﹕ This is a recognized beacon advertisement -- 02 15 seen
04-09 12:28:16.443 27850-27890/com.mobinteg.ibeacon D/BeaconService﹕ beacon detected : id1: 13ba60db-dcdb-42ec-99a3-3f7eef7bbdbd id2: 0 id3: 0
04-09 12:28:16.443 27850-27890/com.mobinteg.ibeacon D/Callback﹕ attempting callback via intent: ComponentInfo{com.mobinteg.ibeacon/org.altbeacon.beacon.BeaconIntentProcessor}
04-09 12:28:16.453 27850-27890/com.mobinteg.ibeacon D/BeaconService﹕ looking for ranging region matches for this beacon
04-09 12:28:16.453 27850-27892/com.mobinteg.ibeacon D/BeaconIntentProcessor﹕ got an intent to process
04-09 12:28:16.453 27850-27892/com.mobinteg.ibeacon D/BeaconIntentProcessor﹕ got monitoring data
04-09 12:28:16.453 27850-27892/com.mobinteg.ibeacon D/BeaconIntentProcessor﹕ Calling monitoring notifier: com.mobinteg.ibeacon.MainActivity$2@420d5b28
04-09 12:28:16.453 27850-27892/com.mobinteg.ibeacon I/MonitoringActivity﹕ I have just switched from seeing/not seeing beacons: 1
04-09 12:28:16.453 27850-27892/com.mobinteg.ibeacon I/MonitoringActivity﹕ I just saw an beacon for the first time!
04-09 12:28:17.383 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Waiting to stop scan cycle for another 97 milliseconds
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Done with scan cycle
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ stopping bluetooth le scan
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/BluetoothAdapter﹕ stopLeScan()
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Normalizing between scan period from 110 to -1027
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ starting a new scan cycle
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ starting a new bluetooth le scan
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/BluetoothAdapter﹕ startLeScan(): null
04-09 12:28:17.493 27850-27872/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Waiting to stop scan cycle for another 1100 milliseconds
04-09 12:28:17.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Scan started
04-09 12:28:17.503 27850-27874/com.mobinteg.ibeacon D/BluetoothAdapter﹕ onScanResult() - Device=73:A5:A7:D6:24:06 RSSI=-63
04-09 12:28:17.503 27850-27874/com.mobinteg.ibeacon D/CycledLeScannerForJellyBeanMr2﹕ got record
04-09 12:28:17.503 27850-28015/com.mobinteg.ibeacon D/BeaconParser﹕ This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 02011a1aff4c00021513ba60dbdcdb42ec99a33f7eef7bbdbd00000000c50000000000000000000000000000000000000000000000000000000000000000
04-09 12:28:17.503 27850-28015/com.mobinteg.ibeacon D/BeaconParser﹕ This is a recognized beacon advertisement -- 02 15 seen
04-09 12:28:17.503 27850-28015/com.mobinteg.ibeacon D/BeaconService﹕ beacon detected multiple times in scan cycle : id1: 13ba60db-dcdb-42ec-99a3-3f7eef7bbdbd id2: 0 id3: 0
04-09 12:28:17.503 27850-28015/com.mobinteg.ibeacon D/BeaconService﹕ beacon detected : id1: 13ba60db-dcdb-42ec-99a3-3f7eef7bbdbd id2: 0 id3: 0
04-09 12:28:17.503 27850-28015/com.mobinteg.ibeacon D/BeaconService﹕ looking for ranging region matches for this beacon
04-09 12:28:18.493 27850-27850/com.mobinteg.ibeacon D/CycledLeScanner﹕ Waiting to stop scan cycle for another 99 milliseconds
---------------------------------------编辑 3---------- ----------------------------
将 didEnterRegion 内的 Log 更改为
Log.i(TAG, "I just saw an beacon for the first time! Id1->"+region.getId1());
并开始进入 didRangeBeaconsInRegion 但 beacons.size() 显示为 0......
---------------------------------------编辑 4---------- -----------------------------------------
好的Log.i(TAG, "I just saw an beacon for the first time! Id1->"+region.getId1()+" id 2:"+region.getId2()+" id 3:"+region.getId3());
设备出现了,现在距离工作正常......