项目作者: adonmo

项目描述 :
MQTT android client
高级语言: Kotlin
项目地址: git://github.com/adonmo/killerbee.git
创建时间: 2021-05-23T10:30:29Z
项目社区:https://github.com/adonmo/killerbee

开源协议:

下载


killerbee

MQTT android client

For comparison with Paho Client check the blog on Killer Bee vs Paho.

Adding as a Dependency

This package is currently hosted as a github package. Github currently supports public package hosting but requires github personal access token to fetch them.
In short you need to have a github account to use this as a dependency.

Setting up github credentials

Add maven repository for github package

Add the maven repo for KillerBee as shown below in build.gradle of the project root.

  1. def githubProperties = new Properties()
  2. githubProperties.load(new FileInputStream(rootProject.file("github.properties")))
  3. allprojects {
  4. repositories {
  5. google()
  6. mavenCentral()
  7. //Use for local testing of library after assemble and publishToMavenLocal
  8. //mavenLocal()
  9. maven {
  10. name = "KillerBee"
  11. url = uri("https://maven.pkg.github.com/adonmo/killerbee")
  12. credentials {
  13. username = githubProperties['GITHUB_USER']
  14. password = githubProperties['GITHUB_PERSONAL_ACCESS_TOKEN']
  15. }
  16. }
  17. }
  18. }

Add dependency to build.gradle in app folder

  1. implementation 'com.adonmo.libraries:killerbee:1.0.1'

Sample Implementation

  1. package com.adonmo.sample.killerbee
  2. import android.os.Bundle
  3. import android.os.Handler
  4. import android.os.HandlerThread
  5. import android.util.Log
  6. import androidx.appcompat.app.AppCompatActivity
  7. import com.adonmo.killerbee.AndroidMQTTClient
  8. import com.adonmo.killerbee.IMQTTConnectionCallback
  9. import com.adonmo.killerbee.action.MQTTActionStatus
  10. import com.adonmo.killerbee.adapter.ConnectOptions
  11. import com.adonmo.killerbee.helper.Constants.LOG_TAG
  12. import java.util.concurrent.ScheduledThreadPoolExecutor
  13. class MainActivity : AppCompatActivity(), IMQTTConnectionCallback {
  14. private lateinit var mqttThread: HandlerThread
  15. private lateinit var mqttHandler: Handler
  16. private lateinit var mqttClient: AndroidMQTTClient
  17. private lateinit var executor: ScheduledThreadPoolExecutor
  18. override fun onCreate(savedInstanceState: Bundle?) {
  19. Log.v(LOG_TAG, "Running on thread [${Thread.currentThread()}]")
  20. super.onCreate(savedInstanceState)
  21. setContentView(R.layout.activity_main)
  22. mqttThread = HandlerThread("mqttThread")
  23. mqttThread.start()
  24. mqttHandler = Handler(mqttThread.looper)
  25. /* As it stands a minimum of 4 threads seems to be necessary to let the MQTT client run
  26. as it blocks a few of them(3 based on testing) with a looper most likely */
  27. executor = ScheduledThreadPoolExecutor(4)
  28. mqttClient = AndroidMQTTClient(
  29. ConnectOptions(
  30. clientID = "OG",
  31. serverURI = "tcp://broker.hivemq.com:1883",
  32. cleanSession = true,
  33. keepAliveInterval = 30,
  34. maxReconnectDelay = 60000,
  35. automaticReconnect = true,
  36. ),
  37. mqttHandler,
  38. this,
  39. executorService = executor
  40. )
  41. mqttClient.connect()
  42. }
  43. override fun connectActionFinished(
  44. status: MQTTActionStatus,
  45. connectOptions: ConnectOptions,
  46. throwable: Throwable?
  47. ) {
  48. if (status == MQTTActionStatus.SUCCESS) {
  49. mqttClient.subscribe("Jello", 1)
  50. mqttClient.subscribe(arrayOf("HelloBee", "BeeHello"), intArrayOf(1, 0))
  51. } else {
  52. Log.e(
  53. LOG_TAG,
  54. "Connection Action Failed for [${connectOptions.clientID}] to [${connectOptions.serverURI}]"
  55. )
  56. }
  57. }
  58. override fun disconnectActionFinished(status: MQTTActionStatus, throwable: Throwable?) {
  59. Log.d(LOG_TAG, "Disconnect Action Status: [$status]")
  60. }
  61. override fun publishActionFinished(
  62. status: MQTTActionStatus,
  63. messagePayload: ByteArray,
  64. throwable: Throwable?
  65. ) {
  66. if (status == MQTTActionStatus.SUCCESS) {
  67. Log.d(LOG_TAG, "Published message $messagePayload")
  68. }
  69. }
  70. override fun subscribeActionFinished(
  71. status: MQTTActionStatus,
  72. topic: String,
  73. throwable: Throwable?
  74. ) {
  75. if (status == MQTTActionStatus.SUCCESS) {
  76. mqttClient.publish("HelloBee", "World".toByteArray(), 1, false)
  77. }
  78. }
  79. override fun subscribeMultipleActionFinished(
  80. status: MQTTActionStatus,
  81. topics: Array<String>,
  82. throwable: Throwable?
  83. ) {
  84. if (status == MQTTActionStatus.SUCCESS) {
  85. mqttClient.publish("HelloBee", "World".toByteArray(), 1, false)
  86. }
  87. }
  88. override fun connectionLost(connectOptions: ConnectOptions, throwable: Throwable?) {
  89. Log.d(
  90. LOG_TAG,
  91. "Connection lost for [${connectOptions.clientID}] from [${connectOptions.serverURI}]"
  92. )
  93. }
  94. override fun messageArrived(
  95. topic: String?,
  96. message: ByteArray?
  97. ) {
  98. message?.let {
  99. Log.d(LOG_TAG, "Received message [$message]")
  100. }
  101. //mqttClient.disconnect()
  102. }
  103. }