简体   繁体   English

附近的API-有效负载排队

[英]Nearby API - queuing of payloads

I have an use case where I am sending control data from AndroidThings device to an Android mobile phone - it is periodal reading of voltage 10 times a second, so every 100 millis. 我有一个用例,其中我将控制数据从AndroidThings设备发送到Android手机-这是每秒定期读取电压10次,因此每100毫秒。 But since this is Nearby API feature - concering sending payloads: 但是,由于这是“附近API”功能-允许发送有效载荷:

Senders use the sendPayload() method to send a Payload. 发件人使用sendPayload()方法发送有效载荷。 This method can be invoked multiple times, but since we guarantee in-order delivery, the second Payload onwards will be queued for sending until the first Payload is done. 可以多次调用此方法,但是由于我们保证按顺序交付,因此第二个有效载荷开始将排队等待发送,直到完成第一个有效载荷为止。

What happens in reality in my case is, based on the the fact that transmission speed varies, I am getting readings on the phone with delay that is increasing, simply queue gets bigger and bigger. 在我的情况下,实际发生的情况是,基于传输速度变化的事实,我正在通过延迟获取电话中的读数,而只是队列越来越大。

Any ideas how to overcome this ? 任何想法如何克服这个? Basically I don't need in-order delivery. 基本上我不需要按订单交货。 My first idea is to have some sort of confirmation of payload delivery and only after the confirmation of receipt the second payload should be sent to recipient. 我的第一个想法是对负载进行某种确认,只有在确认接收后,才应将第二个负载发送给接收者。

Thanks for ideas 谢谢你的想法

UPDATE: 更新:

STREAM type of payload is a perferct solution. STREAM类型的有效负载是一种完美的解决方案。 If the InputStream transfers more than one set of readings (reading inlcude voltage, maxvoltage etc. altogether 32 bytes of data) then I use the skip method to skip to the very last readings. 如果InputStream传输了一组以上的读数(读数包括电压,最大电压等共32个字节的数据),那么我将使用skip方法跳转到最后一个读数。

For your use-case, I would recommend using a STREAM Payload , and then you can keep streaming your control data over that single Payload -- this is exactly one of the use-cases for which we created STREAM. 对于您的用例,我建议您使用STREAM Payload ,然后您可以继续在单个Payload上流式传输控制数据-这正是我们创建STREAM的用例之一。 :) :)

Yup, your first idea sounds like the right thing to do. 是的,您的第一个想法听起来很正确。 There's no way to turn off in-order delivery of payloads inside Nearby Connections, so you'll have to handle dropping payloads yourself. 无法关闭“附近的连接”内部按顺序传送有效负载的方法,因此您必须自己处理丢弃有效负载的问题。

I would build a class that caches the 'most recent voltage'. 我将建立一个可缓存“最新电压”的类。 As you get new readings, this value will overwrite itself each time. 当您获得新的读数时,此值每次都会覆盖自身。

private volatile Long mostRecentVoltage;

public void updateVoltage(long voltage) {
  if (mostRecentVoltage != null) {
    Log.d(TAG, String.format("Dropping voltage %d due to poor network latency", mostRecentVoltage));
  }
  mostRecentVoltage = voltage;
}

Then add another piece of logic that'll grab the cached value every time the previous payload was successfully sent. 然后添加另一个逻辑,该逻辑将在每次成功发送之前的有效负载时获取缓存的值。

@Nullable
public Long getVoltage() {
  try {
    return mostRecentVoltage;
  } finally {
    mostRecentVoltage = null;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM