Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
add maybe interface to retrieve data
add service to support background usage
  • Loading branch information
jaumard authored and jaumard committed May 13, 2018
1 parent cb45b42 commit 56ce356
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 46 deletions.
135 changes: 134 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,140 @@ allprojects {
```

## Basic usage
TODO
What can I do with this library ?

You can send and recieve messages, data, assets. You can listen for new capability on the wear network and send messages per capability

### Foreground usage (Activity/Fragment/ViewModel...)
```
class MainActivity : AppCompatActivity() {
private val rxWearBridge = RxWearBridge(this)
fun sendMessage() {
rxWearBridge.sendMessage(MESSAGE_PATH, DataMap().apply{ putInt("test", 5) })
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
//an error occured
}, onComplete = {
//message sent
})
}
fun listenForMessage() {
//don't forget to dispose it later
rxWearBridge.messageSubject.subscribe {
Log.d(TAG, "${it.path} from ${it.sourceNodeId}")
}
}
fun listenForCapability() {
//don't forget to dispose it later
rxWearBridge.capabilitySubject.subscribe { (path, dataMap) ->
Log.d(TAG, "${path} with ${dataMap}")
}
}
fun listenForData() {
//don't forget to dispose it later
rxWearBridge.dataSubject.subscribe {
Log.d(TAG, "CapabilityInfo = ${it}")
}
}
override fun onStart() {
super.onStart()
rxWearBridge.bind()
}
override fun onStop() {
super.onStop()
rxWearBridge.unbind()
}
}
```

### Background usage (WearableListenerService)
Pretty much similar as the foreground usage except you need to extend the `RxWearBridgeListenerService` class.

After that you'll have access to `rxWearBridge` property that'll allow you to listen for (or send/sync) new messages/data/capability

```
class MyWearableListenerService : RxWearBridgeListenerService() {
fun listenForMessage() {
//don't forget to dispose it later
rxWearBridge.messageSubject.subscribe {
Log.d(TAG, "${it.path} from ${it.sourceNodeId}")
}
}
fun listenForCapability() {
//don't forget to dispose it later
rxWearBridge.capabilitySubject.subscribe { (path, dataMap) ->
Log.d(TAG, "${path} with ${dataMap}")
}
}
fun listenForData() {
//don't forget to dispose it later
rxWearBridge.dataSubject.subscribe {
Log.d(TAG, "CapabilityInfo = ${it}")
}
}
}
```

Don't forget to specify your service on you manifest with correct filter info to recieve data from wear device

## API
`RxWearBridge::messageSubject -> PublishSuject<MessageEvent>`
will give you all recieved messages

`RxWearBridge::dataSubject -> PublishSuject<Pair<String, DataMap>>`
will give you all recieved data, as path and data

`RxWearBridge::capabilitySubject -> PublishSuject<CapabilityInfo>`
will give you all recieved capability

`RxWearBridge::bindCapability(vararg capabilities: String)`
allow you to listen for capabilities by String, response will arrived on `capabilitySubject`

`RxWearBridge::unbindCapability(vararg capabilities: String)`
allow you to remove the listener for capabilities by String

`RxWearBridge::bindCapability(vararg data: Pair<Uri, Int>)`
allow you to listen for capabilities by Uri and filter, response will arrived on `capabilitySubject`

`RxWearBridge::bind()`
allow you to bind data and message reception

`RxWearBridge::unbind()`
allow you to unbind data and message reception

`RxWearBridge::sendMessage(path: String, dataMap: DataMap = DataMap(), capability: String? = null): Completable`
allow you to send a message with data to connected device, with the possibility to filter by capability

`RxWearBridge::syncData(path: String, dataMap: DataMap): Single<DataItem>`
allow you to sync data with connected devices, and return you the updated data

`RxWearBridge::getData(path: String, locally: Boolean = false): Maybe<DataMap>`
allow you to retrieve data from connected device

`RxWearBridge::syncDataArray(path: String, dataMaps: ArrayList<DataMap>): Single<DataItem>`
allow you to sync an array of data with connected devices, and return you the updated data

`RxWearBridge::getDataArray(path: String, locally: Boolean = false): Single<List<DataMap>>`
allow you to retrieve an array of data from connected devices or locally

`RxWearBridge::getAllData(path: String? = null): Single<List<DataMap>>`
allow you to retrieve all data from connected devices or specific one by putting his path

`RxWearBridge::syncBitmap(path: String, assetName: String, bitmap: Bitmap): Single<DataItem>`
allow you to sync a bitmap with connected devices, and return you the updated data

`RxWearBridge::getBitmap(path: String, assetName: String): Maybe<Bitmap>`
allow you to retrieve a bitmap from connected device

## Examples
This library come with a small example app, feel free to check it to see all the above cases.
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/com/jaumard/common/Contants.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.jaumard.common

import android.util.Log

const val MESSAGE_PATH = "/message"
const val DATA_PATH = "/data"
const val DATA_ARRAY_PATH = "/dataarray"
const val BITMAP_PATH = "/bitmap"
const val BITMAP_KEY = "image"
const val DATA_KEY = "data"
const val DATA_INT_KEY = "dataInt"

fun Any.debug(message: Any) {
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, message.toString())
}

fun Any.error(message: String? = null, throwable: Throwable) {
Log.e(this::class.java.simpleName, message, throwable)
}
103 changes: 87 additions & 16 deletions mobile/src/main/java/com/jaumard/wearbridge/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jaumard.wearbridge
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.google.android.gms.wearable.DataMap
import com.jaumard.common.*
Expand All @@ -14,10 +13,6 @@ import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "RxWearBridge"
}

private val rxWearBridge = RxWearBridge(this)

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -26,12 +21,88 @@ class MainActivity : AppCompatActivity() {

rxWearBridge.messageSubject.subscribe {
message.text = "${message.text}\n${it.path} from ${it.sourceNodeId}"
Log.d(TAG, "${it.path} from ${it.sourceNodeId}")
debug("${it.path} from ${it.sourceNodeId}")
}

rxWearBridge.dataSubject.subscribe { (path, data) ->

}

syncData()
syncDataArray()
syncBitmap()
getUnknownData()
getUnknownBitmap()
getUnknownDataArray()
getAllDataUnknown()
}

private fun getUnknownBitmap() {
rxWearBridge.getBitmap("/unknown/path", "test")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown bitmap", it)
}, onSuccess = {
debug(it)
assert(false)//must not return a value
})
}

private fun getAllDataUnknown() {
rxWearBridge.getAllData("/unknown/path")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown all data", it)
}, onSuccess = {
debug(it)
assert(it.isEmpty())
})
}

private fun getUnknownDataArray() {
rxWearBridge.getDataArray("/unknown/path", true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown data array local", it)
}, onSuccess = {
debug(it)
assert(it.isEmpty())
})

rxWearBridge.getDataArray("/unknown/path")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown data array", it)
}, onSuccess = {
debug(it)
assert(it.isEmpty())
})
}

private fun getUnknownData() {
rxWearBridge.getData("/unknown/path", true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown data local", it)
}, onSuccess = {
debug(it)
assert(false)//must not return a value
})

rxWearBridge.getData("/unknown/path")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
error("unknown data", it)
}, onSuccess = {
debug(it)
assert(false)//must not return a value
})
}

private fun syncBitmap() {
Expand All @@ -40,9 +111,9 @@ class MainActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
Log.e(TAG, "syncBitmap", it)
error("syncBitmap", it)
}, onSuccess = {
Log.i(TAG, "syncBitmap ok $it")
debug("syncBitmap ok $it")
})
}

Expand All @@ -58,9 +129,9 @@ class MainActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
Log.e(TAG, "syncDataArray", it)
error("syncDataArray", it)
}, onSuccess = {
Log.i(TAG, "syncDataArray ok $it")
debug("syncDataArray ok $it")
})
}

Expand All @@ -72,9 +143,9 @@ class MainActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
Log.e(TAG, "syncData", it)
error("syncData", it)
}, onSuccess = {
Log.i(TAG, "syncData ok $it")
debug("syncData ok $it")
})
}

Expand All @@ -86,9 +157,9 @@ class MainActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
Log.e(TAG, "updateData", it)
error("updateData", it)
}, onSuccess = {
Log.i(TAG, "updateData ok $it")
debug("updateData ok $it")
})
}

Expand All @@ -97,9 +168,9 @@ class MainActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
Log.e(TAG, "sendMessage", it)
error("sendMessage", it)
}, onComplete = {
Log.i(TAG, "sendMessage ok")
debug("sendMessage ok")
})
}

Expand Down
2 changes: 1 addition & 1 deletion rxwearbridge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ext {
siteUrl = 'https://github.com/jaumard/RxWearBridge'
gitUrl = 'https://github.com/jaumard/RxWearBridge.git'

libraryVersion = '1.0.1'
libraryVersion = '1.1.0'

developerId = 'jaumard'
developerName = 'Jimmy Aumard'
Expand Down
Loading

0 comments on commit 56ce356

Please sign in to comment.