简体   繁体   English

蓝牙Android聊天示例中的SendToTarget GetTarget

[英]SendToTarget GetTarget in BlueTooth Android Chat Sample

I am working on making the BlueTooth Chat Sample working with a remote service instead of a local service (for later use). 我正在努力使蓝牙聊天示例与远程服务而不是本地服务一起使用(以供以后使用)。

But when I try to use the Handler, which is used to display both sent and received messages, an error occurs, which I don't know how to fix : 但是,当我尝试使用用于同时显示已发送和已接收消息的处理程序时,发生了一个错误,我不知道如何解决:

mHandler.obtainMessage(RemoteServiceClient.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();

This line sends to the Handler some informations, including the message code (MESSAGE_WRITE) and the content of the buffer (buffer) which I would like to display. 此行向处理程序发送一些信息,包括消息代码(MESSAGE_WRITE)和我要显示的缓冲区(buffer)的内容。

But when I send a message in the application, it crashes and the log shows a nullPointerException, which, I guess, corresponds to the lack of getTarget . 但是,当我在应用程序中发送消息时,它崩溃并且日志显示nullPointerException,我想这与缺少getTarget相对应。

My problem is : in the original sample, there is no getTarget (as in my app), but the sendToTarget works perfectly. 我的问题是:在原始示例中,没有getTarget(如我的应用程序中一样),但是sendToTarget可以完美地工作。 How can it be explained and how can I make it work in my application? 如何解释它以及如何使其在我的应用程序中起作用?

I fixed the issue using dispatchMessage : 我使用dispatchMessage解决了这个问题:

messageToMainService.what = VALUES_FROM_THREAD;
messageData.putParcelable("pgnParcelable", pgnListParcelable);
messageData.putParcelable("currentLocation", myLocation);
messageData.putLong("timerBoucle", ellapseTime + timeToSleep);
    //messageData.putWhateverYouWant
messageToMainService.setData(messageData);
mHandler.dispatchMessage(messageToMainService);

I wonder if this use is correct. 我想知道这种用法是否正确。 Please tell me if it's not 请告诉我是否不是

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

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