简体   繁体   中英

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.

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 .

My problem is : in the original sample, there is no getTarget (as in my app), but the sendToTarget works perfectly. How can it be explained and how can I make it work in my application?

I fixed the issue using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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