我使用的是阿尔特信标:https://altbeacon.github.io/android-beacon-library/,我绑定了我的beaconManager。然后我称之为:
public void startListeningForBeacons(RangeNotifier rangeNotifier) {
try {
Region region = new Region("all-beacons-region", null, null, null);
// Tells the BeaconService to start looking for beacons that match the passed Region object
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
// Specifies a class that should be called each time the BeaconService gets ranging data, once per second by default
beaconManager.addRangeNotifier(rangeNotifier);
}它开始在我的应用程序类中调用这个函数:
@Override
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i("BluetoothService", "BluetoothService region The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
if (didRangeBeaconsCallback != null)
didRangeBeaconsCallback.rangeCalled(beacons, region);
}else {
Log.i("BluetoothService", "BluetoothService region NO BEACONS: " + beacons.size());
if(PSBluetoothService.getInstance(PSApplicationClass.this).beaconUUID != null){
if (didRangeBeaconsCallback != null)
didRangeBeaconsCallback.rangeCalled(null, null);
}
}
}但很多时候,我会拿回0码的收藏。即使我有两个信标在射程内。我还尝试使用LG Nexus 5和LeEco LePro2,两者都返回正确的信标列表。当涉及到华为时,会有什么问题呢?
如果有帮助,这是我配置的信标布局列表:
//sets the types of beacons that the library is searching for
public void setNeededTypesOfBeacons() {
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,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,d:25-25"));
}这是一幅显示它发现的东西的图片,与其他图片相比:https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/5FNgB6ARACpywWv/upload.png
编辑:
用这个逻辑制作了一个示例应用程序,如果有帮助的话:https://www.dropbox.com/s/4ddt9mhn7zdi9gd/Beacon.zip?dl=0
即使离开它扫描很长一段时间,仍然没有结果,当它找不到它们:
03-20 12:30:24.542: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:25.644: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:26.749: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:27.855: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:28.960: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:30.071: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:31.174: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:32.277: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:33.379: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:34.486: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:35.588: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:36.696: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:37.803: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:38.906: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:40.012: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:41.210: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:42.313: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:43.418: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:44.522: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:45.628: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:46.734: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:47.839: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:48.943: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:50.061: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:50.061: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:51.165: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:52.268: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:53.372: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:54.476: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0发布于 2018-03-19 17:36:22
基于以下问题的评论,对华为的检测是断断续续的,代码只有5-10%的机会在5秒的扫描时间内检测到信标,此时代码就放弃了。
有些蓝牙芯片在开始扫描后很慢地检测到第一个信标。这可能是你必须学会接受的硬件限制。
我建议你重新修改代码,以便:
发布于 2018-03-16 15:16:31
任何带有Android 4.3+和蓝牙低能芯片组的设备都可以用这个库检测信标。据谷歌游戏商店( Google )的数据,截至2017年8月,这一比例约为安卓设备的92 %。
作为信标传输,安卓5.0+和固件支持蓝牙低能量外围模式是必要的。
要想知道你的智能手机或平板电脑是否可以使用蓝牙,请访问Google商店,在你的安卓设备上安装免费应用程序“检查器”。这个应用程序很简单-它告诉你你的设备是否支持蓝牙乐,这就是它所做的一切。
我不能在Mate10上测试您的代码,因为我现在没有代码,但是我认为您对于android 8的问题在于阅读了有关android 8上的更改的链接
http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8
使用该库的应用程序将需要升级到2.12+版本,以便在Android8设备上检测到背景信息。版本2.13+为Android8.1增加了进一步的增强,限制了屏幕关闭后的扫描。
dependencies {
compile 'org.altbeacon:android-beacon-library:2+'
}https://stackoverflow.com/questions/49324089
复制相似问题