我正在向我的 Pebble 发送大量数据,但其中一些数据不断丢失。我意识到这部分是由于缓冲区大小不足以PebbleDictionary
我发送它,所以我把它分成多个小块。但是,这会带来APP_MSG_BUSY
错误的问题。
这可能会发生,因为我不是在等待来自卵石的 ack/nack,而是只是背靠背地发送数据。因此,我尝试添加 ack/nack 处理程序以及队列,但由于我的sendMessage()
函数在等待 ack/nack 处理程序时阻塞了主 UI 线程,我无法让它工作。
因此,我的问题是处理这个特定实例的最佳方法是什么APP_MSG_BUSY
。我不希望我发送的任何数据被丢弃,这意味着要么在发送下一条数据之前等待确认,要么在收到 nack 后重新发送。如果可能的话,我想避免线程,但我一直无法想出一个不涉及线程的合理解决方案。
编辑:据我所知,卵石代码中没有错误。它将使用正确的密钥请求数据,并且会(自动)确认 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);
}
}
布尔字典:
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)