WearSocket


Source link: https://github.com/jrejaud/WearSocket

WearSocket

A wrapper for socket communication between Android Wear and Android Mobile. There is a lot of boiler plate to send data between Android Wear and Android Mobile devices. This library (inspired by Socket.IO), aims to streamline both message sending and synching data updates.

Installation

Use JitPack

allprojects {
  repositories {

maven {
 url "https://jitpack.io" 
}
  
}
 
}
 
dependencies {

compile 'com.github.jrejaud:WearSocket:1.0.+' 
}
 

Setup

First, get an instance of the WearSocket and set it up by passing Context and the wear app capabilities

WearSocket wearSocket = WearSocket.getInstance();
 String androidWearCapability = ...; wearSocket.setupAndConnect(context, androidWearCapability, new onErrorListener() {

@Override

public void onError(Throwable throwable) {

 //Throws an error here if there is a problem connecting to the other device.

}
 
}
);

Advertising Capabilities (copied from official Google documentation)

Create an XML configuration file in the res/values/ directory of your project and name it wear.xml. Add a resource named android_wear_capabilities to wear.xml. Define capabilities that the device provides. Note: Capabilities are custom strings that you define and must be unique within your app.

The following example shows how to add a capability named voice_transcription to wear.xml:

<resources>
  <string-array name="android_wear_capabilities">

<item>voice_transcription</item>
  </string-array> </resources> 

Sending and Receiving Messages between Wear and Mobile

Sending Message

Pick a path (think of it like a subject) for your message.

wearSocket.sendMessage("myPath","myMessage");

Receiving a Message

Start your message listener and listen for the path that you are associating with your messages.

wearSocket.startMessageListener(context,"myPath");

Implement WearSocket.MessageListener and override messageReceived to start listening for messages

public class MyActivity implements WearSocket.MessageListener
 @Override
  public void messageReceived(String path, String message) {

//Do things here!
  
}

Synching Data Updates between Wear and Mobile

Updating a data item

Note: Data Path should begin with /, thus I recommend using one different from message sending. dataItem is an object (or collection of objects).

wearSocket.updateDataItem("myDataPath","myKey",dataItem);

Receiving a data item update

Start your data change listener and listen for the path that you are associating with your data changes.

wearSocket.startDataListener(context,"myDataPath");

Additionally, you need to associate a Type with a specific Key. For example, if the key "myKey" is associated with a List<String> object, set that Type accordingly:

wearSocket.setKeyDataType("myKey",new TypeToken<List<String>>() {

}
.getType());

Change List<String> to whatever class (or collection of classes) that dataItem is.

Implement WearSocket.MessageListener and override messageReceived to start listening for messages

public class MyActivity implements WearSocket.MessageListener
 @Override
  public void dataChanged(String key, Object data) {

//Cast "data" as whatever class you sent it as

ArrayList<String> dataItem = (ArrayList<String>) data;

//Do things here!
  
}

License and Attribution

MIT License

WearSocket uses: Gson, Android Wearable APIs, Google Play Services

Resources

An ImageView subclass that draws a spannable text on top of the image.

It is a java wrapper for querying books information on Google Books Api.

CountryPicker is an Android library created to show a custom fragment which allows to choose a country.

HoverTouchView simulates Apple's Force Touch or 3D Touch on Android App with Hover Gesture.

PageStateLayout could let you show Loading / Empty / Error / Succeed / Requesting state in Activity, Fragment, ViewGroup as you want.

SimpleWaveform is a widget to show a sequence data in waveform or bar chart.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes