我向Pebble发送了大量数据,但有些数据不断下降。我意识到这部分原因是由于缓冲区大小不足以导致我发送的PebbleDictionary
,所以我将其分解为多个小的。但是,这会导致出现APP_MSG_BUSY
错误的问题。
这种情况可能正在发生,因为我没有等待来自鹅卵石的ack / nack,而只是将数据发送回来。因此我尝试添加ack / nack处理程序以及队列,但由于我的sendMessage()
函数在等待ack / nack处理程序时阻塞了主UI线程,因此无法使其工作。
因此,我的问题是处理APP_MSG_BUSY
这个特定实例的最佳方法是什么。我不希望我发送的任何数据被删除,这意味着要么在发送下一个数据之前等待确认,要么在获得nack之后重新发送。如果可能的话,我想避免线程化,但我还没有能够提出一个不涉及线程的合理解决方案。
编辑:据我所知,卵石代码中没有错误。它将使用正确的密钥请求数据,它将(自动)确认Android应用程序发送的任何消息。
如果您愿意,我已在下面发布了我的代码:
当前代码(android app的相关部分):
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);
}
}
BoolDictionary:
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)
答案 0 :(得分:2)
当Pebble收到您的新数据包时会返回错误APP_MSG_BUSY
,但蓝牙缓冲区中已经有一条消息显示您的应用尚未有时间阅读。
要理解这一点,你需要记住Pebble上的内存很紧,因此系统无法缓冲传入的消息,因为这会非常快速地占用大量内存。相反,它拒绝该消息。
避免此问题的最佳策略是在发送另一条消息之前等待Pebble的ACK / NACK。您似乎已经这样做了,但另一个提示是确保您在一条消息中组合多个密钥,而不是为每个密钥发送一条消息(这最大化了传入缓冲区的使用,但当然限制是该缓冲区的大小)。