Comments (2)
I don't have a full fledged example. From the BLE side, SensorTag sample app shows how to use Kable in a multiplatform way. Since the SensorTag doesn't use CoAP at all (that I'm aware of) there wasn't a need to utilize KoAP for that sample.
I'll share a bit about how we're using KoAP w/ Kable, it won't be an end-to-end sample, but might be enough to get you started? Although, it all depends on how the peripheral (firmware) side implements CoAP as to how you should probably go about it on the central (e.g. phone) side.
CoAP feels very much like HTTP (request then response) style protocol, which differs from BLE a bit (which has a collection of services/characteristics/descriptors that can be written/read/observed).
We bridged the paradigms by having a dedicated "send" characteristic (which we use purely for writing) and a dedicated "response" characteristic (which we "observe" via BLE notifications).
As far as how KoAP builds on Kable for that:
You can have a write
function, similar to:
suspend fun send(request: Message.Tcp) {
peripheral.write(sendCharacteristic, request.encode())
}
Then, for processing responses:
val responses = peripheral.observe(responseCharacteristic)
.map { bytes -> bytes.decode<Tcp>() }
And, to bring it together for a request+response function:
// note: every CoAP request should have a unique token
suspend fun sendForResponse(request: Message.Tcp): Message.Tcp {
val response = responses
.onSubscription { send(request) }
.first { response.token == request.token }
check(response.code is Message.Code.Response) { "Received invalid response code: ${response.code}" }
return response
}
You could additionally have helper functions to emulate an API with a HTTP feel:
suspend fun get(model: YourRequestModel): YourResponseModel {
// e.g. "POST /example/path"
val request = Message.Tcp(
code = POST,
token = generateRandomToken(),
options = listOf(
UriPath("example"), UriPath("path"),
Accept(CBOR),
),
// e.g. use kotlinx.serialization to encode as CBOR, but other formats should work if your firmware supports it
payload = Cbor.encodeToByteArray(YourRequestModel.serializer(), model),
)
val response = sendForResponse(request)
// todo: handle `Message.Tcp.option`s that are applicable
return Cbor.decodeFromByteArray(YourResponseModel.serializer(), response.payload)
}
Note, the above code was written by hand so there could be many compilation or logical errors. Use as psuedo code. 🤷
Best of luck!
from koap.
Thank you writing this up! Hope to play around with this over the weekend.
As a side note, did y'all review the IETF draft https://datatracker.ietf.org/doc/draft-amsuess-core-coap-over-gatt/? It went stale but looks like the author is trying to pick it up again. It follows a similar approach to what we did at a presence company.
from koap.
Related Issues (12)
- [CRITICAL] Public Repositories are Disabled for this Org HOT 1
- Provide pre-defined Content-Format constants HOT 1
- How to receive payload data? HOT 14
- Use unsigned integers when stable HOT 2
- Parse unknown options as opaque instead of failing the whole message HOT 1
- Koap github pages deployment down HOT 4
- Public API Not Directly Usable From Javascript HOT 3
- Online CoAP decoder HOT 1
- Optimize online CoAP decoder HOT 2
- Support JavaScript IR Compiler
- java.lang.IllegalStateException: Unsupported option number 21 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koap.