Android Beacon 库检测 iBeacon 失败!不公平:(
Android Beacon Library detecting iBeacon fails! not fair :(
我尝试使用 Android 信标库来检测 iBeacon,但它不起作用。
logcat 的输出只是:
I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-I9195_4.4.2_0046
I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
E/dalvikvm﹕ >>>>> Normal User
E/dalvikvm﹕ >>>>> com.cyberland.felix.ibeaconexample [ userId:0 | appId:10176 ]
D/dalvikvm﹕ Late-enabling CheckJNI
I/PersonaManager﹕ getPersonaService() name persona_policy
D/BeaconParser﹕ Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser﹕ Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
E/MonitoringStatus﹕ Deserialization exception, message: $s
然后
D/BluetoothAdapter﹕ stopLeScan()
一次又一次...
我刚刚修改了官方页面的示例:
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MainActivity";
private BeaconManager beaconManager;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.d(TAG, "Scanned BLE device with mac: " + device.getAddress());
}
});
*/
beaconManager = BeaconManager.getInstanceForApplication(this);
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);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
希望你能帮帮我!
此致,
菲利克斯
嗯,官方示例(android-beacon-library-reference)产生相同的输出。我无法解释。
看来问题可能与未授予在您的设备上进行扫描的适当权限有关。为了验证这个理论,请将下面的代码添加到你的main activity 的onCreate
方法中,看看它是什么returns。下面显示了我在 Nexus 5X 上看到的示例输出。
try {
PackageInfo info = getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS);
Log.d(TAG, "SDK "+Build.VERSION.SDK_INT+" App Permissions:");
if (info.requestedPermissions != null) {
for (String p : info.requestedPermissions) {
int grantResult = this.checkPermission(p, android.os.Process.myPid(), android.os.Process.myUid());
if (grantResult == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, p+" PERMISSION_GRANTED");
}
else {
Log.d(TAG, p+" PERMISSION_DENIED: "+grantResult);
}
}
}
} catch (Exception e) {
Log.d(TAG, "Cannot get permissions due to error", e);
}
示例输出:
04-17 12:04:01.943 30624-30624/? D/MonitoringActivity: SDK 23 App Permissions:
04-17 12:04:01.944 30624-30624/? D/MonitoringActivity: android.permission.INTERNET PERMISSION_GRANTED
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH PERMISSION_GRANTED
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH_ADMIN PERMISSION_GRANTED
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.RECEIVE_BOOT_COMPLETED PERMISSION_GRANTED
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.ACCESS_COARSE_LOCATION PERMISSION_GRANTED
如果您发现设备输出中缺少上述任何权限,解决方案可能是手动将 ACCESS_COARSE_LOCATION
位置权限添加到您的 AndroidManifest.xml。如果您看到 PERMISSION_DENIED,则可能是另一个问题。您可以在您的设备上开始信标扫描之前跳过权限检查,这可以通过暂时将您的 Android 信标库版本降级到不执行此检查的 2.7 来实现。
我尝试使用 Android 信标库来检测 iBeacon,但它不起作用。
logcat 的输出只是:
I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-I9195_4.4.2_0046
I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
E/dalvikvm﹕ >>>>> Normal User
E/dalvikvm﹕ >>>>> com.cyberland.felix.ibeaconexample [ userId:0 | appId:10176 ]
D/dalvikvm﹕ Late-enabling CheckJNI
I/PersonaManager﹕ getPersonaService() name persona_policy
D/BeaconParser﹕ Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser﹕ Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
W/ModelSpecificDistanceCalculator﹕ Cannot find match for this device. Using default
E/MonitoringStatus﹕ Deserialization exception, message: $s
然后
D/BluetoothAdapter﹕ stopLeScan()
一次又一次...
我刚刚修改了官方页面的示例:
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MainActivity";
private BeaconManager beaconManager;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.d(TAG, "Scanned BLE device with mac: " + device.getAddress());
}
});
*/
beaconManager = BeaconManager.getInstanceForApplication(this);
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);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
希望你能帮帮我!
此致, 菲利克斯
嗯,官方示例(android-beacon-library-reference)产生相同的输出。我无法解释。
看来问题可能与未授予在您的设备上进行扫描的适当权限有关。为了验证这个理论,请将下面的代码添加到你的main activity 的onCreate
方法中,看看它是什么returns。下面显示了我在 Nexus 5X 上看到的示例输出。
try {
PackageInfo info = getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS);
Log.d(TAG, "SDK "+Build.VERSION.SDK_INT+" App Permissions:");
if (info.requestedPermissions != null) {
for (String p : info.requestedPermissions) {
int grantResult = this.checkPermission(p, android.os.Process.myPid(), android.os.Process.myUid());
if (grantResult == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, p+" PERMISSION_GRANTED");
}
else {
Log.d(TAG, p+" PERMISSION_DENIED: "+grantResult);
}
}
}
} catch (Exception e) {
Log.d(TAG, "Cannot get permissions due to error", e);
}
示例输出:
04-17 12:04:01.943 30624-30624/? D/MonitoringActivity: SDK 23 App Permissions:
04-17 12:04:01.944 30624-30624/? D/MonitoringActivity: android.permission.INTERNET PERMISSION_GRANTED
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH PERMISSION_GRANTED
04-17 12:04:01.945 30624-30624/? D/MonitoringActivity: android.permission.BLUETOOTH_ADMIN PERMISSION_GRANTED
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.RECEIVE_BOOT_COMPLETED PERMISSION_GRANTED
04-17 12:04:01.946 30624-30624/? D/MonitoringActivity: android.permission.ACCESS_COARSE_LOCATION PERMISSION_GRANTED
如果您发现设备输出中缺少上述任何权限,解决方案可能是手动将 ACCESS_COARSE_LOCATION
位置权限添加到您的 AndroidManifest.xml。如果您看到 PERMISSION_DENIED,则可能是另一个问题。您可以在您的设备上开始信标扫描之前跳过权限检查,这可以通过暂时将您的 Android 信标库版本降级到不执行此检查的 2.7 来实现。