mqtt

MQTT 是一种轻量级的、灵活的物联网消息交换和数据传递协议,致力于为 IoT 开发人员实现灵活性与硬件/网络资源的平衡。

1. Android(Kotlin)

/etc/mosquitto/mosquitto.conf mosquitto.conf

1
2
3
4
# Starting in local only mode.
# https://mosquitto.org/blog/2020/12/version-2-0-0-released/
listener 1883
allow_anonymous true

build.gradle.kts

1
2
3
4
5
dependencies {
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
//implementation("org.eclipse.paho:org.eclipse.paho.android.service:1.1.1")
implementation("io.github.muxiaolin:org.eclipse.paho.android.service:1.1.4")
}

AndroidManifest.xml

1
2
3
4
5
6
7
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application>
<service android:name="org.eclipse.paho.android.service.MqttService" />
</application>