我正在向我的 Pebble 发送大量数据,但其中一些数据不断丢失。我意识到部分原因是缓冲区大小对于我发送的 PebbleDictionary
来说不够大,因此我将其分成多个小缓冲区。但是,这会导致出现 APP_MSG_BUSY
错误。
发生这种情况可能是因为我没有等待来自卵石的 ack/nack,而是只是连续发送数据。因此,我尝试添加 ack/nack 处理程序以及队列,但由于我的 sendMessage()
函数在等待 ack/nack 处理程序时阻塞了主 UI 线程,因此无法使其正常工作.
因此,我的问题是处理 APP_MSG_BUSY
这个特定实例的最佳方法是什么。我不希望发送的任何数据被丢弃,因此这意味着要么在发送下一条数据之前等待确认,要么在收到 nack 后重新发送。如果可能的话,我想避免线程,但我一直无法想出一个不涉及线程的合理解决方案。
编辑:据我所知,pebble 代码中没有错误。它会要求使用正确 key 的数据,并且会(自动)确认 Android 应用程序发送的任何消息。
如果您愿意,我已在下面发布了我的代码:
当前代码(Android应用程序的相关部分):
public class MainActivity extends ActionBarActivity {
private PebbleDataReceiver mReceiver;
private PebbleAckReceiver ackReceiver;
private PebbleNackReceiver nackReceiver;
ConcurrentLinkedQueue<BoolDictionary> queue = new ConcurrentLinkedQueue<BoolDictionary>();
final UUID PEBBLE_APP_UUID = UUID.fromString("2ef1h2ba-1a59-41f7-87da-797beca4d395");
final static int CONTACTS_NEEDED = 0x0;
final static int CONTACTS_SIZE = 0x1;
final static int NEW_MESSAGE = 0x2;
final static int NEW_CONVERSATION = 0x3;
final static int RECORD_NEW_MESSAGE = 0x4;
Thread sendMessages = new Thread(){
public void run(){
while (true){
PebbleKit.sendDataToPebbleWithTransactionId(getApplicationContext(), PEBBLE_APP_UUID, queue.element().getDict(), queue.element().getTransId());
try {
queue.element().wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
if (queue.element().isAkced()){
break;
}
}
queue.remove();
if (queue.size() > 0){
run();
}
else if (queue.size() == 0){
queue.element().wait();
}
}
};
public BoolDictionary createBoolDictionary(int key, int data){
PebbleDictionary dict = new PebbleDictionary();
dict.addInt32(key, data);
return new BoolDictionary(dict);
}
public BoolDictionary createBoolDictionary(int key, String data){
PebbleDictionary dict = new PebbleDictionary();
dict.addString(key, data);
return new BoolDictionary(dict);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessages.start();
ackReceiver = new PebbleAckReceiver(PEBBLE_APP_UUID) {
@Override
public void receiveAck(Context context, int transactionId) {
if (queue.element().getTransId() == transactionId){
queue.element().setAkced(true);
queue.element().notifyAll();
}
}
};
nackReceiver = new PebbleNackReceiver(PEBBLE_APP_UUID){
@Override
public void receiveNack(Context context, int transactionId) {
if (queue.element().getTransId() == transactionId){
queue.element().setAkced(false);
queue.element().notifyAll();
}
}
};
mReceiver = new PebbleDataReceiver(PEBBLE_APP_UUID) {
@Override
public void receiveData(Context context, int transactionId, PebbleDictionary data) {
PebbleKit.sendAckToPebble(context, transactionId);
if (data.contains(CONTACTS_NEEDED)){
//test data
queue.add(createBoolDictionary(0x5, "Entry 1"));
queue.add(createBoolDictionary(0x6, "Entry 2"));
queue.add(createBoolDictionary(0x7, "Entry 3"));
queue.add(createBoolDictionary(0x8, "Entry 4"));
queue.add(createBoolDictionary(0x9, "Entry 5"));
queue.element().notifyAll();
}
}
};
PebbleKit.registerReceivedDataHandler(this, mReceiver);
PebbleKit.registerReceivedAckHandler(this, ackReceiver);
PebbleKit.registerReceivedNackHandler(this, nackReceiver);
}
@Override
protected void onPause(){
super.onPause();
unregisterReceiver(mReceiver);
unregisterReceiver(ackReceiver);
unregisterReceiver(nackReceiver);
}
}
boolean 字典:
public class BoolDictionary extends PebbleDictionary{
private PebbleDictionary dict;
private boolean akced = false;
private int transId;
BoolDictionary(PebbleDictionary data){
this.setDict(data);
setTransId(new Random().nextInt(Integer.MAX_VALUE));
}
[insert getters and setters here]
}
这会产生以下错误:
07-01 10:43:06.096: E/AndroidRuntime(21941): FATAL EXCEPTION: Thread-5310
07-01 10:43:06.096: E/AndroidRuntime(21941): Process: com.example.firstapp, PID: 21941
07-01 10:43:06.096: E/AndroidRuntime(21941): java.util.NoSuchElementException
07-01 10:43:06.096: E/AndroidRuntime(21941): at java.util.AbstractQueue.element(AbstractQueue.java:107)
07-01 10:43:06.096: E/AndroidRuntime(21941): at com.example.firstapp.MainActivity$1.run(MainActivity.java:366)
最佳答案
当 Pebble 收到您的新数据包但蓝牙缓冲区中已经有一条消息您的应用尚 future 得及读取时,会返回错误 APP_MSG_BUSY
。
要理解这一点,您需要记住 Pebble 上的内存很紧张,因此系统无法缓冲传入的消息,因为这会很快占用大量内存。相反,它拒绝该消息。
避免此问题的最佳策略是在发送另一条消息之前等待来自 Pebble 的 ACK/NACK。看来您已经这样做了,但另一个技巧是确保将多个 key 分组在一条消息中,而不是为每个 key 发送一条消息(这最大化了传入缓冲区的使用,但当然限制是该缓冲区的大小)。
关于java - 如何正确处理pebble中的APP_MSG_BUSY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24513243/