Sending message to disconnected mobile from wearable - android

I'm trying to send an event or better a message to the mobile while the wearable is disconnected.
Here is the code I'm using:
Wearable.MessageApi.sendMessage(
mGoogleApiClient, node, event, message).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
#Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if(!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG, "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
}
}
}
);
The node ID is cached when onPeerConnected(Node peer) is called so I don't need to query the Node API to get an empty list. However I send the data to the node which is offline. That results the StatusCode 4000 which is TARGET_NODE_NOT_CONNECTED. Of course I know that, but what is the best way to cache this event to send it as soon as possible?

I ended in the that idea which Maciej Ciemięga pointed out in the comments. I'm using the DataAPI to store and forward my events. After the event was recieved by the mobile I delete the path from the data layer, since it did its job.
You should keep in mind the deletion will invoke again the onDataChanged method. So you should check the type of the DataEvent:
event.getType() == DataEvent.TYPE_DELETED
If you don't keep that in mind you may get an infinity loop.

Related

How to get sip messages on during call on android?

I want to get and read sip info messages on during call. I sent other side to sip info message and I saw on logcat but I dont get the message on code. I use the pjsip.
How to get sip info messages on during call?
Thanks.
you will require. onCallTsxState call back function from call class to listen to Sip message shared during the call. It took quite some time for me to figure out and hopefully it could help some once else ,if its too late for you.
#Override
public void onCallTsxState(OnCallTsxStateParam prm) {
if (eventBody.getTsxState().getType() == (pjsip_event_id_e.PJSIP_EVENT_RX_MSG)) {
SipRxData rdata = eventBody.getTsxState().getSrc().getRdata();
//technically here in messageBuffer I am storing all header file info shared during the call
String messageBuffer = rdata.getWholeMsg();
Log.e("bingo", String.valueOf(messageBuffer));
// in my case in my case I am check presences of info message : Connection Established every time new header message is send
if(messageBuffer.contains("Connection established")){
Log.e("bingo", "bingo established");
try {
//perform what ever you want to do here when you receive info message
}
});

How to change database value if user's internet is switched off

For the past few days i've been trying to show the online/offline status of a user.. For this i have a register activity where they register and their info gets saved in firebase and if they exit an activity i have overriden its onstop method and made the value to set to offline... but if the user suddenly loses internet connection it still shows online.. i cant change it to offline because internet is needed to make a change in the database and the use doesn't have internet... SO how do i set the database value to offline... i googled quite some stuff about this but didnt find anything... Can anyone please help me out please
My code
#Override
protected void onStart() {
super.onStart();
fetchData();
// mDatabaseReference.child("UserData").child(UID).child("Online").setValue("True");
}
#Override
protected void onStop() {
super.onStop();
fetchData();
// mDatabaseReference.child("UserData").child(UID).child("Online").setValue(false);
}
What you're trying to do is known as a presence system. The Firebase Database has a special API to allow this: onDisconnect(). When you attach a handler to onDisconnect(), the write operation you specify will be executed on the server when that server detects that the client has disconnected.
From the documentation on managing presence:
Here is a simple example of writing data upon disconnection by using the onDisconnect primitive:
DatabaseRef presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().setValue("I disconnected!");
In your case this could be as simple as:
protected void onStart() {
super.onStart();
fetchData();
DatabaseReference onlineRef = mDatabaseReference.child("UserData").child(UID).child("Online");
onlineRef.setValue("True");
onlineRef.onDisconnect().setValue("False");
}
Note that this will work in simple cases, but will start to have problems for example when your connection toggles rapidly. In that case it may take the server longer to detect that the client disappears (since this may depends on the socket timing out) than it takes the client to reconnect, resulting in an invalid False.
To handle these situations better, check out the sample presence system in the documentation, which has more elaborate handling of edge cases.

How to detect data is added in firestore during offline android app

I'm working with firestore in android. I want to allow my user to save the data in app during the offline mode.(Data insertion during offline is also working fine) But I don't know how I can detect that data is added in offline mode, I need to get document id that is added. In the online mode I can detect the data insertion with the listener as.
Map<String, Object> data = new HashMap<>();
data.put("name", "Tokyo");
data.put("country", "Japan");
db.collection("cities")
.add(data)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
I also need to detect that is added when the app is offline. So how I can? Because these listeners only works when the data is inserted in the server and app get the response from the server.
The addOnSuccessListener only gets called once the data is committed to the server. That's its explicit goal. If you local client also need the data after it's added locally, you'll do that with a regular addSnapshotListener.
From the documentation on events for local changes:
Local writes in your app will invoke snapshot listeners immediately. This is because of an important feature called "latency compensation." When you perform a write, your listeners will be notified with the new data before the data is sent to the backend.
Retrieved documents have a metadata.hasPendingWrites property that indicates whether the document has local changes that haven't been written to the backend yet. You can use this property to determine the source of events received by your snapshot listener.
See the linked documentation for sample code of how to process this.
Update: if you're just trying to get the ID of a new document, you can simply do:
DocumentReference newDoc = db.collection("cities").document();
System.out.println(newDoc.getId());
newDoc.set(data);
See CollectionReference.document().
Offline insert listener.
Based on my code test, it is just fine to remove both the success and snapshot listeners. simply check if a document id is not null and its length is greater than 10. that was enough for me to conclude a successful insert. code :
docRef.collection("collectioname").document("docname").set(aMap);
String id = docRef.collection("collectioname").document("docname").getId();
if( id != null & !id.isEmpty() & id.length() > 10) {
//Action here
}
Thank you Frank for your earlier clue. The add() will always write the document to the local .....in my case the set() method.

How to make Parse's "done" callback to be called immediately when network is unavailable

The title says it all.
I have a table on the Parse server, I fill it manually, then in my app I want to count then query it like this:
mCurrentParseQuery = ParseQuery.getQuery(FavoriteTable.NAME);
mCurrentParseQuery.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
mCurrentParseQuery.countInBackground(new CountCallback() {
#Override
public void done(int count, ParseException e) {
}
});
As I have tested, it seems Parse listens on event when network become available then starts the query, so when network is unavailable (either Wifi or data connection) done callback never gets called.
My question is how to make Parse call done callback immediately when network is unavailable, even with a ParseException.

Two WearableListenerService. How can one send request only to other device?

please help.
I have two WearableListenerServices. The first on phone and the second on a watch. if I send asset data from wear to phone, for example:
PutDataMapRequest dataMap = PutDataMapRequest.create(AUDIO_PATH);
dataMap.getDataMap().putAsset(REC_AUDIO_KEY, asset);
dataMap.getDataMap().putLong(TIME, new Date().getTime());
dataMap.getDataMap().putString(NAME, name);
PutDataRequest request = dataMap.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
#Override
public void onResult(DataApi.DataItemResult dataItemResult) {
if (!dataItemResult.getStatus().isSuccess()) {
failRecord();
}else{
successRecord(true);
}
}
});
I expect that result will be from phone, but in fact response come back from itself. The problem is that on the watch executed method onDataChanged earlier then on the Phone. How can I send request only to other device?
Thanks.
Have u cross checked the path (AUDIO_PATH) ? It should be same in onDataChanged of wearableListenerService located in phone and not to be same in onDataChanged of wearableListenerService located in Wear.
In your question you have already mentioned that u need to send request to only one side then u dont have to implement onDataChangedMethod in Sending side.

Categories

Resources