Skip to content

Instantly share code, notes, and snippets.

@nikiizvorski
Last active April 8, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikiizvorski/616a3c38cc808ecf24e2a3538fc6c2c4 to your computer and use it in GitHub Desktop.
Save nikiizvorski/616a3c38cc808ecf24e2a3538fc6c2c4 to your computer and use it in GitHub Desktop.
ViableGrid 6LOWPAN Implementation as a Full Receiver over UDP or TCP you can find here
private val KITCHEN = "224.0.0.251"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
To start using our sdk please run the sender first and get the channel, panid and the network you want it to be on.
after you find all the things you need by running the sample for the sender with the following command to verify
adb shell lowpanctl status and you verify all the above. Add it to the receiver and restart the sender.
You are all set.
Don't run receiver and sender at the same time its pointless that's why they are designed to run with both functionality.
They can send and receive for each client or server. Use the provided methods.
*/
startReceiver()
}
private fun startReceiver() {
ViableLowpanSDK.registerLowpanManager(object : DeviceInterfaceCallback() {
override fun onInterfaceRemoved(removed: Boolean) {
Log.i("Interface", "Removed")
}
override fun onInterfaceAdded(status: Int, added: Boolean) {
Log.i("Interface", "Added " + added)
Log.i("Interface", "Added " + status)
if (status == LowpanInterface.STATE_ATTACHED) {
Log.i("Interface", "Attached")
ViableLowpanSDK.formLowpanNetwork("demotestnetwork", NETWORK_KEY, 24, 0xFA22)
receiveUDP()
//receiveTCP()
//receiveGroup()
}
}
})
}
private fun receiveGroup() {
ViableLowpanSDK.receiveLowPanGroupConnection(this@MainActivity, 1234, KITCHEN, object : ReceivedDataCallback() {
override fun onReceivedData(dataReceived: ByteArray?) {
Log.i("Received Byte", "Image received size " + Arrays.toString(dataReceived))
val str = String(dataReceived!!, 0 , dataReceived.size)
textView!!.setText("Data size " + str)
}
})
}
private fun receiveUDP() {
ViableLowpanSDK.receiveLowPanUDPConnection(1234, object : ReceivedDataCallback() {
override fun onReceivedData(dataReceived: ByteArray?) {
Log.i("Received Byte", "Image received size " + Arrays.toString(dataReceived))
val str = String(dataReceived!!, 0 , dataReceived.size)
textView!!.setText("Data size " + str)
}
})
}
private fun receiveTCP(){
ViableLowpanSDK.receiveLowPanConnection(SERVER_PORT, object : ReceiverConnectionCallback() {
override fun onConnected(connected: Boolean, socket: Socket?) {
if (connected) {
Log.i("Connection", "Connected")
if (socket != null) {
Log.i("Connection", "Socket is here")
if (socket.getInputStream() != null) {
Log.i("Connection", "Socket is stream not null")
receiveData(socket)
} else {
Log.i("Connection", "Socket is here nothing to add")
}
//ViableLowpanSDK.receiverSendData(socket, receiver_address)
}
} else {
Log.i("Interface", "not connected")
}
}
})
}
private fun receiveData(socket: Socket?){
ViableLowpanSDK.receiverSocket(socket, object : ReceivedMessageCallback() {
override fun onReceived(data: String?) {
Log.i("Received Data", data)
textView!!.setText("Image received size " + data)
if (data.equals("https://viablegrid.com/assets/img/logo-top.png")) {
imageView?.let {
Glide.with(this@MainActivity).load(data).into(
it
)
}
}
}})
}
override fun onDestroy() {
super.onDestroy()
ViableLowpanSDK.unregisterLowpanManager()
ViableLowpanSDK.stopSearchingLowpan()
ViableLowpanSDK.stopLowpanNetwork()
}
override fun onStart() {
super.onStart()
ViableLowpanSDK.registerLowpanDriver(UART_PORT, UART_BAUD)
}
override fun onStop() {
super.onStop()
ViableLowpanSDK.unregisterLowpanDriver()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment