AltBeacon didEnterRegion()/didExitRegion 无法与 PiBeacon 一起正常工作。方法被随机调用
AltBeacon didEnterRegion()/didExitRegion not working properly with PiBeacon. Methods are randomly being called
我对 Android 开发和低功耗蓝牙还很陌生。在发帖之前我已经研究了很多。我很确定,我做错了什么,我无法弄清楚。
我希望我的 android 应用程序在进入某个 Beacon(带有蓝牙加密狗的 RaspberryPi)的指定范围内时执行一些代码。
这是我在树莓派上使用的开始发射的命令:
sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39
F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00
我已经编辑了 BeaconLayout 来检测 iBeacons:
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
除自定义区域外,代码来自 AltBeacon 文档。
Region region1 = new Region("RaspberryPi", Identifier.parse("E20A39F4-73F5-4BC4-A12F-17D1AD07A961"), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw a beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see a beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+ state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region1);
} catch (RemoteException e) { }
现在这是我得到的日志的一部分。在此期间,信标没有离开该区域。除了我 运行 应用程序时随机发生的那几次调用外,这些方法再也不会被调用:
05-19 23:18:52.487 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:53.952 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:55.195 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:56.570 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:57.742 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:57.782 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.782 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:58.933 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:00.098 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:01.287 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:01.537 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.537 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:01.538 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.538 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:01.539 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.539 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:02.629 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
我假设它与 Raspberry Pi 的传输速率有关,但我无法将其更改为有效的东西。
干得好!一切似乎大部分都在工作。 (奇怪的是,每个不同的日志消息都看到三个日志行——不确定为什么会这样。)
日志显示您在信标检测中丢失,导致在 05-19 23:18:57.782 发生退出事件,然后在 05-19 23:19:01.537 发生另一个进入事件。虽然这段时间看起来很短(大约 4 秒),但 dropout 一定比这长得多。默认情况下,Android Beacon 库将不会发送区域退出事件,直到它在 10 秒内没有看到与区域定义匹配的信标。这意味着 有 14 秒的时间,您的 Android 设备没有看到来自 Raspberry Pi 的传输。
来自信标的某些传输未被 Android 设备接收是正常的。在近距离范围内,大约 80% 的数据包被正确接收,其余数据包由于无线电噪声、冲突、信道问题或其他低级蓝牙因素而丢失。如果你真的不走运,你可能会连续错过几次传输。但是连续错过超过两三个传输应该是极其罕见的。
这意味着以下两个错误之一:
你的Raspberry Pi不是很频繁地做广告(可能只有每5秒一次,如果你错过了两个数据包会导致区域退出连续)。
您的 Android 设备丢失了很多数据包。 这有时可能是由于 WiFi/Bluetooth 已知设备受到干扰造成的干扰问题,例如 Nexus 7、Nexus 4、Moto G 和 Moto X。
要弄清楚是哪一个,请尝试以下操作:
关闭您 Android 设备上的 WiFi。 这是否解决了问题?
提高Raspberry Pi BlueZ的传输速率。你可以在我的回答中看到如何做到这一点 here.
我对 Android 开发和低功耗蓝牙还很陌生。在发帖之前我已经研究了很多。我很确定,我做错了什么,我无法弄清楚。
我希望我的 android 应用程序在进入某个 Beacon(带有蓝牙加密狗的 RaspberryPi)的指定范围内时执行一些代码。
这是我在树莓派上使用的开始发射的命令:
sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39
F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00
我已经编辑了 BeaconLayout 来检测 iBeacons:
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
除自定义区域外,代码来自 AltBeacon 文档。
Region region1 = new Region("RaspberryPi", Identifier.parse("E20A39F4-73F5-4BC4-A12F-17D1AD07A961"), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw a beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see a beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+ state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region1);
} catch (RemoteException e) { }
现在这是我得到的日志的一部分。在此期间,信标没有离开该区域。除了我 运行 应用程序时随机发生的那几次调用外,这些方法再也不会被调用:
05-19 23:18:52.487 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:53.952 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:55.195 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:56.570 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:57.742 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:18:57.782 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.782 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
05-19 23:18:57.790 31204-32016/com.daniellazarov.tempapp I/MonitoringActivity: I no longer see a beacon
05-19 23:18:58.933 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:00.098 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:01.287 31204-31220/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
05-19 23:19:01.537 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.537 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:01.538 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.538 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:01.539 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
05-19 23:19:01.539 31204-32140/com.daniellazarov.tempapp I/MonitoringActivity: I just saw a beacon for the first time!
05-19 23:19:02.629 31204-31219/com.daniellazarov.tempapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
我假设它与 Raspberry Pi 的传输速率有关,但我无法将其更改为有效的东西。
干得好!一切似乎大部分都在工作。 (奇怪的是,每个不同的日志消息都看到三个日志行——不确定为什么会这样。)
日志显示您在信标检测中丢失,导致在 05-19 23:18:57.782 发生退出事件,然后在 05-19 23:19:01.537 发生另一个进入事件。虽然这段时间看起来很短(大约 4 秒),但 dropout 一定比这长得多。默认情况下,Android Beacon 库将不会发送区域退出事件,直到它在 10 秒内没有看到与区域定义匹配的信标。这意味着 有 14 秒的时间,您的 Android 设备没有看到来自 Raspberry Pi 的传输。
来自信标的某些传输未被 Android 设备接收是正常的。在近距离范围内,大约 80% 的数据包被正确接收,其余数据包由于无线电噪声、冲突、信道问题或其他低级蓝牙因素而丢失。如果你真的不走运,你可能会连续错过几次传输。但是连续错过超过两三个传输应该是极其罕见的。
这意味着以下两个错误之一:
你的Raspberry Pi不是很频繁地做广告(可能只有每5秒一次,如果你错过了两个数据包会导致区域退出连续)。
您的 Android 设备丢失了很多数据包。 这有时可能是由于 WiFi/Bluetooth 已知设备受到干扰造成的干扰问题,例如 Nexus 7、Nexus 4、Moto G 和 Moto X。
要弄清楚是哪一个,请尝试以下操作:
关闭您 Android 设备上的 WiFi。 这是否解决了问题?
提高Raspberry Pi BlueZ的传输速率。你可以在我的回答中看到如何做到这一点 here.