gpt4 book ai didi

java - Android BLE 接近通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:17 24 4
gpt4 key购买 nike

我一直致力于开发与 BLE 设备交互的应用程序。一切正常,我可以扫描、连接和使用服务。

我通读了所有文档,但没有看到任何可以让开发人员选择监听 BLE 设备的内容。基本上我想在设备进入 BLE 设备的范围时触发广播接收器。

我知道我可以持续扫描它,但电池使用率太高,我希望即使在我的应用程序未被使用时也能调用它。

是否不支持此功能,或者我是否遗漏了讨论此功能的文档部分?

最佳答案

我最近做了一个项目,从我看到的你的问题来看,它和我做的有一些相似之处。

I know I could continually scan for this but battery use is way too high and I would like this to be invoked even when my application is not being used.

关于电池问题,一直开着蓝牙很耗电,但同时不开着蓝牙是检测不到BLE的。

我做的是两个实验,都是有用的,但不同,我不能说哪个最好,但你需要测试它,它符合你的要求。

  1. 运行线程以编程方式打开蓝牙并监听 iBeacon 并关闭( hibernate 时间)一段时间。可以通过多种方式完成。

  2. 使用一个名为 Altbeacon 的包,它有很多有用的功能,其中一个功能是自动节电 example code :

    public class MyApplication extends Application implements BootstrapNotifier {
    private BackgroundPowerSaver backgroundPowerSaver;

    public void onCreate() {
    super.onCreate();
    // Simply constructing this class and holding a reference to it
    // in your custom Application class
    // enables auto battery saving of about 60%
    backgroundPowerSaver = new BackgroundPowerSaver(this);
    }
    }

We need a broadcast event, that wakes up our app once a BLE-device with a certain Service-UUID is in reach. Maybe now there is a better BLE API available than 2 years ago. The most energy saving and most precise method gets rewarded.

你的另一部分,它被称为在特定距离触发 Action 。我仍然使用 Altbeacon 检查信标范围和触发 Action 。 sample code有点像

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
if (beacon.getDistance() < 5.0) {
Log.d(TAG, "I see a beacon that is less than 5 meters away.");
// Perform distance-specific action here
}
}
}

所以话虽如此,您还可以获得特定 UUID 的距离我构建了一个基于 Altbeacon 的方法,看起来像这样(查看 for 循环和 if 语句内部):

private void startRangeNotifier() {
Log.i(TAG, "Starting range notifier...");
beaconManager.setRangeNotifier(new BeaconRangeListener() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

if (beacons.size() > 0) {
for (Beacon beacon : beacons) {
Log.d(TAG, "uuid's: " + beacon);
Log.d(TAG, "uuid id1: " + beacon.getId1());
if (beacon.getId1().toString()
.equals("b9407f30-f5f8-466e-aff9-25556b57fe6d")) {
Log.d(TAG, "uuid id1 distance: " + beacon.getDistance());
}
}

}
}
});

try {
beaconManager.startRangingBeaconsInRegion(
new Region(BEACON_MONITORING_ID, null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}

我的日志输出:

D/Main activity:: uuid's: id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 31807 id3: 59251
D/Main activity:: uuid id1: b9407f30-f5f8-466e-aff9-25556b57fe6d
D/Main activity:: uuid id1 distance: 0.2108658568686884

在我的回答中,我想介绍我使用的概念,Beacons 项目通常需要耐心。正如提到的另一个答案,也可以将此处的解决方案与 Geofences 和 ActivityRecognition 结合起来。

Note: Since the nature of bluetooth beacon, the distance is proximity and not absolute, and some time even the bluetooth beacon is 1 meter a way it might show 2 meter or 0.5 meter, so have that in mind

链接引用:

关于java - Android BLE 接近通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24894658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com