Android Beacon库-正确使用BluetoothMedic吗? [英] Android Beacon Library - correct use of BluetoothMedic?

查看:102
本文介绍了Android Beacon库-正确使用BluetoothMedic吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以下观察,我对蓝牙医疗的使用存在一些疑问。

I have some questions on the use of Bluetooth Medic, based on the following observations.

由于蓝牙在我的其中一台设备上停止,我一直在查看BluetoothMedic看看是否有帮助。我查看了调试消息和源代码。根据我使用enablePeriodicTests()还是单独运行runScanTest()和runTransmitterTest(),得到的结果会有所不同。

Because Bluetooth stops on one of my devices I have been looking at the BluetoothMedic to see whether it can help. I've looked at the debug messages and the source code. I get somewhat different results depending on whether I use enablePeriodicTests() or individually run runScanTest() and runTransmitterTest().

使用enablePeriodicTests(),BluetoothTestJob.onStartJob()每15分钟运行一次扫描和发射器测试,显然可以。如果我的信标正在传输,则从扫描测试中获得扫描测试成功然后扫描测试完成,如果没有,则获得超时运行扫描测试,然后扫描测试完成。在运行发射机测试之后,在所有情况下我都获得发射机测试成功然后发射机测试完成的信息。

With enablePeriodicTests(), BluetoothTestJob.onStartJob() runs scan and transmitter tests every 15 minutes, apparently OK. If my beacon is transmitting, I get "Scan test succeeded" then "scan test complete" from the scan test, and if not I get "Timeout running scan test" then "scan test complete". After that the transmitter test runs and I get "Transmitter test succeeded" then "transmitter test complete" in all cases.

但是,当我添加执行runScanTest()和runTransmitterTest()调用的按钮时,会出现不同的行为。在这两种情况下,代码都进入while()循环以等待非空测试结果,并在5秒后超时。由于测试结果为空,因此调用将返回true(对于扫描测试)和false(对于发射器测试)。

However I get different behaviour when I add buttons that execute the runScanTest() and runTransmitterTest() calls. In both cases the code enters the while() loops waiting for a non-null test result, and times out after 5s. Because the test results are null, the calls then return true (for the scan test) and false (for the transmitter test).

在扫描测试的情况下,如果我的信标未传输,则永远不会调用onScanResult()回调,但是如果信标正在传输,则称为10-20次(我看到许多扫描测试成功消息),但仅在AFTER runScanTest()返回之后。

In the case of the scan test, the onScanResult() callback is never called if my beacon is not transmitting, but if the beacon is transmitting it is called 10-20 times (I see many "Scan test succeeded" messages) but only AFTER runScanTest() returns.

对于发送器测试,将触发onStartSuccess()回调一次,我看到传输器测试成功消息,但仅在runTransmitterTest()返回之后。

In the case of the transmitter test, the onStartSuccess() callback is fired once and I see a "Transmitter test succeeded" message but only AFTER the runTransmitterTest() returns.

两个设备(Android 7和8)的行为相同。

Behaviour is the same for two devices (Android 7 and 8).

最好有更多有关这些测试以及如何使用它们的文档。

It would be good to have further documentation on these tests and how to use them.

首先,这些测试做了什么,它们可能会发现什么错误?

Firstly, what do these tests do and what errors might they find?

其二-应该如何用过的?看起来runScanTest()和runTransmitterTest()不能简单地调用-它们需要自己的线程还是其他东西?

Secondly - how should they be used? It looks like runScanTest() and runTransmitterTest() can't be called simply - do they need their own threads or something?

最后,在

推荐答案

Android信标库的 BluetoothMedic 旨在检测Android蓝牙堆栈出现的症状进入不良状态,然后选择关闭并重新打开蓝牙电源以摆脱该状态。 Medic查找两个OS级错误代码,这些错误代码是从尝试扫描Bluetooth LE设备或传输Bluetooth LE广告返回的。已知这些错误代码与蓝牙堆栈的状态不佳相关,如果没有干预,通常不会从该状态恢复。

The Android Beacon Library's BluetoothMedic is designed to detect symptoms of the Android bluetooth stack getting into a bad state, and then optionally cycle power to bluetooth to get out of it. The Medic looks for two OS-level error codes that come back from attempting to scan for a Bluetooth LE device or transmit a Bluetooth LE advertisement. These error codes are known to be associated with a bad state of the bluetooth stack from which recovery generally does not happen without intervention.

最简单的设置方法是被动地进行设置。像这样:

The simplest way to set it up is passively like this:

BluetoothMedic medic = BluetoothMedic.getInstance();
medic.enablePowerCycleOnFailures(context);

以上两行将开始为医生检查由于常规使用而发生的任何蓝牙错误附带的应用程序对库功能(扫描和/或传输)的说明。

The above two lines will start the medic listening for any bluetooth errors that happen as a result of regular use of the library's functions (scanning and/or transmitting) by the enclosing app. If these are detected, it will cycle power in an attempt to recover.

这样做通常是安全的,但是重启电源会给其他功能(如经典蓝牙)带来问题- -如果在这种情况下手机正在使用蓝牙扬声器,则显然会断开连接。蓝牙LE功能不太可能受到电源重启的影响,因为无论如何蓝牙堆栈中的错误情况都可能阻止功能形式正常工作。

This is generally safe to do, but cycling power can cause problems to other functions like classic bluetooth -- if the phone is using a bluetooth speaker when this happens, it will obviously disconnect. Bluetooth LE functions are less likely to be adversely affected by a power cycle, as the error condition in the bluetooth stack probably prevents the functions form working, anyway.

可以看到,您还可以将BluetoothMedic设置为定期运行自己的测试。如果您的应用程序仅定期与信标一起使用,并且您希望在应用程序需要使用它们之前主动从任何错误情况中恢复,那么这可能是一个好主意。要进行设置,请致电:

As you have seen, you can also set up the BluetoothMedic to run its own test periodically. This may be a a good idea if you app only periodically works with beacons, and you want to proactively recover from any error conditions before your app needs to use them. To set this up, call:

medic.enablePeriodicTests(context, BluetoothMedic.SCAN_TEST | BluetoothMedic.TRANSMIT_TEST);

您也可以直接调用测试,但这是高级用法,不是主要设计。如果您决定这样做,则绝对应该在新线程上执行此操作,否则它们将阻塞UI线程,直到测试完成。

You can also call the tests directly, but this is an advanced usage which is not the primary design. If you decide to do so, you should definitely do this on a new thread as they will otherwise block the UI thread until the test completes. This will cause problems like you describe.

如果在扫描测试过程中附近没有BLE设备,则测试超时,结果不确定- -您不确定蓝牙堆栈是否处于良好状态。调试行表明了这一事实。

If there are no BLE devices in the vicinity during the scan test, then the test "times out" and the results are inconclusive -- you don't know for sure that the bluetooth stack is in a good state. The debug line is an indication of that fact.

这篇关于Android Beacon库-正确使用BluetoothMedic吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆