diff --git a/wear-app/app/build.gradle b/wear-app/app/build.gradle index c8ad457..193df64 100644 --- a/wear-app/app/build.gradle +++ b/wear-app/app/build.gradle @@ -35,5 +35,6 @@ dependencies { implementation 'androidx.recyclerview:recyclerview:1.2.0' implementation 'androidx.wear:wear:1.0.0' implementation 'com.google.android.gms:play-services-location:17.0.0' + implementation 'com.google.code.gson:gson:2.9.0' compileOnly 'com.google.android.wearable:wearable:2.8.1' } \ No newline at end of file diff --git a/wear-app/app/src/main/AndroidManifest.xml b/wear-app/app/src/main/AndroidManifest.xml index 62a2b81..953be54 100644 --- a/wear-app/app/src/main/AndroidManifest.xml +++ b/wear-app/app/src/main/AndroidManifest.xml @@ -63,6 +63,10 @@ android:host="*" android:pathPattern="/.*/stop" android:scheme="wear" /> + diff --git a/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessage.java b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessage.java new file mode 100644 index 0000000..7d37af5 --- /dev/null +++ b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessage.java @@ -0,0 +1,49 @@ +package org.nativescript.demo.freemessage; + +import com.google.gson.Gson; + +public class FreeMessage { + private String message; + private FreeMessage inResponseTo; + + public FreeMessage(String message) { + this.message = message; + } + + public FreeMessage(String message, FreeMessage inResponseTo) { + this.message = message; + this.inResponseTo = inResponseTo; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public FreeMessage getInResponseTo() { + return inResponseTo; + } + + public void setInResponseTo(FreeMessage inResponseTo) { + this.inResponseTo = inResponseTo; + } + + @Override + public String toString() { + return "FreeMessage{" + + "message='" + message + '\'' + + ", inResponseTo=" + inResponseTo + + '}'; + } + + public static String encodeFreeMessage(FreeMessage freeMessage) { + return new Gson().toJson(freeMessage); + } + + public static FreeMessage decodeFreeMessage(String stringMessage) { + return new Gson().fromJson(stringMessage, FreeMessage.class); + } +} diff --git a/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageClient.java b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageClient.java new file mode 100644 index 0000000..d3e64af --- /dev/null +++ b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageClient.java @@ -0,0 +1,47 @@ +package org.nativescript.demo.freemessage; + +import android.content.Context; +import android.util.Log; + +import androidx.annotation.NonNull; + +import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.tasks.Task; +import com.google.android.gms.tasks.Tasks; +import com.google.android.gms.wearable.CapabilityClient; +import com.google.android.gms.wearable.CapabilityInfo; +import com.google.android.gms.wearable.MessageClient; +import com.google.android.gms.wearable.Node; +import com.google.android.gms.wearable.Wearable; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.util.Set; + +public class FreeMessageClient { + + private CapabilityClient capabilityClient; + private MessageClient messageClient; + private FreeMessageProtocol protocol; + + public FreeMessageClient(Context context) { + this.capabilityClient = Wearable.getCapabilityClient(context); + this.messageClient = Wearable.getMessageClient(context); + this.protocol = FreeMessageProtocol.getProtocol(); + } + + public void send(FreeMessage freeMessage) { + capabilityClient.getCapability("main-node", CapabilityClient.FILTER_ALL) + .addOnSuccessListener(capabilityInfo -> { + Set mainNodes = capabilityInfo.getNodes(); + if (mainNodes.isEmpty()) + return; + + Node mainNode = mainNodes.iterator().next(); + String jsonString = FreeMessage.encodeFreeMessage(freeMessage); + messageClient.sendMessage(mainNode.getId(), this.protocol.getWithoutResponsePath(), jsonString.getBytes()); + }) + .addOnFailureListener(e -> Log.d("Failure", e.getMessage())); + } +} diff --git a/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageHandler.java b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageHandler.java new file mode 100644 index 0000000..ee47f73 --- /dev/null +++ b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageHandler.java @@ -0,0 +1,43 @@ +package org.nativescript.demo.freemessage; + +import android.content.Context; +import android.util.Log; + +import com.google.android.gms.wearable.MessageEvent; + +public class FreeMessageHandler { + + private Context context; + private FreeMessageProtocol protocol; + + public FreeMessageHandler(Context context) { + this.context = context; + this.protocol = FreeMessageProtocol.getProtocol(); + } + + public void handleMessage(MessageEvent event) { + String path = event.getPath(); + + if (!path.equals(this.protocol.getExpectingResponsePath()) + && !path.equals(this.protocol.getWithoutResponsePath())) { + return; + } + + if (event.getData() == null) { + return; + } + + String sourceNodeId = event.getSourceNodeId(); + String encodedMessage = new String(event.getData()); + + FreeMessage message = FreeMessage.decodeFreeMessage(encodedMessage); + ReceivedMessage receivedMessage = new ReceivedMessage( + sourceNodeId, + message, + path.equals(this.protocol.getExpectingResponsePath()) + ); + Log.d("FreeMessageHandler", receivedMessage.toString()); + + // TODO: Listener callback + } +} diff --git a/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageProtocol.java b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageProtocol.java new file mode 100644 index 0000000..1e6309a --- /dev/null +++ b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageProtocol.java @@ -0,0 +1,26 @@ +package org.nativescript.demo.freemessage; + +public class FreeMessageProtocol { + private String withoutResponsePath; + private String expectingResponsePath; + + public FreeMessageProtocol(String withoutResponsePath, String expectingResponsePath) { + this.withoutResponsePath = withoutResponsePath; + this.expectingResponsePath = expectingResponsePath; + } + + public String getWithoutResponsePath() { + return withoutResponsePath; + } + + public String getExpectingResponsePath() { + return expectingResponsePath; + } + + public static FreeMessageProtocol getProtocol() { + return new FreeMessageProtocol( + "free-message-no-response", + "free-message-expecting-response" + ); + } +} diff --git a/wear-app/app/src/main/java/org/nativescript/demo/freemessage/ReceivedMessage.java b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/ReceivedMessage.java new file mode 100644 index 0000000..fb64ffb --- /dev/null +++ b/wear-app/app/src/main/java/org/nativescript/demo/freemessage/ReceivedMessage.java @@ -0,0 +1,46 @@ +package org.nativescript.demo.freemessage; + +public class ReceivedMessage { + private String senderNodeId; + private FreeMessage freeMessage; + private boolean requiresResponse; + + public ReceivedMessage(String senderNodeId, FreeMessage freeMessage, boolean requiresResponse) { + this.senderNodeId = senderNodeId; + this.freeMessage = freeMessage; + this.requiresResponse = requiresResponse; + } + + public String getSenderNodeId() { + return senderNodeId; + } + + public void setSenderNodeId(String senderNodeId) { + this.senderNodeId = senderNodeId; + } + + public FreeMessage getFreeMessage() { + return freeMessage; + } + + public void setFreeMessage(FreeMessage freeMessage) { + this.freeMessage = freeMessage; + } + + public boolean isRequiresResponse() { + return requiresResponse; + } + + public void setRequiresResponse(boolean requiresResponse) { + this.requiresResponse = requiresResponse; + } + + @Override + public String toString() { + return "ReceivedMessage{" + + "senderNodeId='" + senderNodeId + '\'' + + ", freeMessage=" + freeMessage + + ", requiresResponse=" + requiresResponse + + '}'; + } +} diff --git a/wear-app/app/src/main/java/org/nativescript/demo/services/SensorMessagingListenerService.java b/wear-app/app/src/main/java/org/nativescript/demo/services/SensorMessagingListenerService.java index c13c237..3c0cf7e 100644 --- a/wear-app/app/src/main/java/org/nativescript/demo/services/SensorMessagingListenerService.java +++ b/wear-app/app/src/main/java/org/nativescript/demo/services/SensorMessagingListenerService.java @@ -4,6 +4,7 @@ import com.google.android.gms.wearable.WearableListenerService; import org.nativescript.demo.capabilities.CapabilityAdvertisementHandler; +import org.nativescript.demo.freemessage.FreeMessageHandler; import org.nativescript.demo.messaging.handlers.AccelerometerMessagingHandler; import org.nativescript.demo.messaging.handlers.GyroscopeMessagingHandler; import org.nativescript.demo.messaging.handlers.HeartRateMessagingHandler; @@ -29,6 +30,8 @@ public void onMessageReceived(MessageEvent event) { new HeartRateMessagingHandler(this).handleMessage(event); } else if (path.contains("location")) { new LocationMessagingHandler(this).handleMessage(event); + } else if (path.contains("free-message")) { + new FreeMessageHandler(this).handleMessage(event); } } }