I've got a CGM peripheral which advertises every five minutes, being discoverable only for several seconds. The aim is to be able to gather glucose data every five minutes, especially in the background.
我有一个CGM外围设备,每五分钟扫描一次,只能扫描几秒钟。目标是能够每五分钟收集一次葡萄糖数据,特别是在后台。
The Flutter code works well in the foreground for both Android and iOS devices. After scanning and discovering the peripheral once, I'll attempt to reconnect to it with a timeout for next readings, and disconnect immediately after gathering data.
颤动代码在Android和iOS设备的前台都运行得很好。在扫描和发现外围设备一次后,我将尝试重新连接到它,下次读取超时,并在收集数据后立即断开连接。
The issue is connecting while app is the background. The timeout option for flutterBlueDevice.connect(timeout: Duration(seconds: 300)
doesn't seem to wait at all, reporting no device has been found. I understand that BLE connectivity is limited in iOS environment.
问题是连接,而APP是后台。FltterBlueDevice.Connect(超时:持续时间(秒:300))的超时选项似乎根本没有等待,报告没有找到任何设备。我知道在iOS环境中BLE连接是有限的。
Is there any way I could get around this in Flutter? If not, what approach should I use to integrate native corebluetooth
partially in my code for connection part? Also, what connection strategy works best for continuous readings in this case?
有没有什么办法可以让我轻而易举地绕过这个问题?如果没有,我应该使用什么方法来将本机核心蓝牙部分集成到我的代码中用于连接部分?此外,在这种情况下,什么连接策略对连续阅读最有效?
更多回答
Can the peripheral indicate/notify when it has a new value. If so, rather than polling a better approach is to simply remain connected and read the new data when it is available. You are not going to be able to initiate a connection from ios when the app is in the background because there is no trigger to make your app run every five minutes.
外围设备是否可以在其具有新值时进行指示/通知。如果是这样,而不是轮询,更好的方法是简单地保持连接并在新数据可用时读取它。当应用程序在后台时,你将无法从iOS启动连接,因为没有触发器让你的应用程序每五分钟运行一次。
Reading briefly though the spec it looks like the peripheral can also disconnect automatically after a period and then become reconnectable later. If that is what happens, then your app should simply call Connect
as soon as it gets a disconnection from the peripheral. iOS will complete that connection in the background when the peripheral becomes reconnectable.
简单地阅读一下规范,它看起来外围设备也可以在一段时间后自动断开,然后稍后变得可重新连接。如果发生这种情况,那么您的应用程序应该在与外围设备断开连接后立即调用Connect。当外围设备可重新连接时,IOS将在后台完成该连接。
State restoration is likely going to be necessary for the long-term operation of your app. As long as your app remains in memory, suspended, Bluetooth background mode will be sufficient, but eventually your app will be terminated because the system needs memory for some other app. State restoration will ensure that your app is relaunched in the background to complete the pending connect
.
状态恢复对于应用的长期运行可能是必要的。只要应用仍在内存中,挂起,蓝牙后台模式就足够了,但最终您的应用将被终止,因为系统需要内存用于其他应用。状态恢复将确保您的应用在后台重新启动以完成挂起的连接。
Assuming you are using this package it doesn't work because the package isn't written to work correctly with background mode core Bluetooth. When you call Connect
it checks to see if the peripheral is in a list of discovered peripherals and fails immediately if they aren't. This is not correct. It should use retrievePeripheralWithIdentifier
to try and obtain a CBPeripheral
instance and then call connect
in that. The connect will complete when/if the peripheral becomes available
假设你正在使用这个包,它不能工作,因为这个包没有被写成能在后台模式下正常工作。当您调用Connect时,它会检查外围设备是否在发现的外围设备列表中,如果不在,则立即失败。这是不正确的。它应该使用recesePeripheralWithIdentifier来尝试并获取CBPeriphery实例,然后在其中调用CONNECT。当外围设备变为可用时/如果外围设备可用,连接将完成
Ok. The code in that library looks better. It is always a good idea to link the packages you are using when asking questions. It looks like you need to have a method channel observer for onConnectionStateChanged
in your Dart code to handle the connected peripheral. You can't simply await
the connect.
好的。该库中的代码看起来更好。在提问时链接您正在使用的包始终是个好主意。看起来您需要在DART代码中为onConnectionStateChanged设置一个方法通道观察器来处理连接的外围设备。你不能简单地等待连接。
我是一名优秀的程序员,十分优秀!