Skip to content

Commit 993cbde

Browse files
committed
add support android
1 parent 727de31 commit 993cbde

File tree

7 files changed

+532
-195
lines changed

7 files changed

+532
-195
lines changed

README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,60 +47,60 @@ new RCTMqttPackage() // for newest version of react-native
4747

4848
- Append the following lines to `android/settings.gradle` before `include ':app'`:
4949

50-
```java
50+
```
5151
include ':react-native-mqtt'
5252
project(':react-native-mqtt').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-mqtt/android')
53+
5354
```
5455

56+
5557
- Insert the following lines inside the dependencies block in `android/app/build.gradle`, don't missing `apply plugin:'java'` on top:
5658

57-
```java
59+
```
5860
compile project(':react-native-mqtt')
5961
```
6062

6163
Notes:
6264

63-
```java
65+
```
6466
dependencies {
6567
compile project(':react-native-mqtt')
6668
}
6769
```
6870

6971

70-
But not like this
71-
72-
```java
73-
buildscript {
74-
...
75-
dependencies {
76-
compile project(':react-native-mqtt')
77-
}
78-
}
79-
```
80-
8172
## Usage
8273

8374
```javascript
8475
var mqtt = require('react-native-mqtt');
8576

86-
var client = mqtt.Client({
87-
uri: 'mqtt://mqtt.yourhost.com:1883'
88-
});
89-
90-
client.on('connect', function () {
91-
client.subscribe('/topic/qos0');
92-
client.subscribe('/topic/qos1', 1);
93-
client.subscribe('/topic/qos2', 2);
94-
95-
client.publish('/topic/qos0', 'publish with string');
96-
client.publish('/topic/qos1', new ArrayBuffer(10)); //publish with arraybuffer
97-
});
98-
99-
client.on('message', function (topic, message) {
100-
// message is ArrayBuffer
101-
//topic is string
102-
//console.log(message.toString());
103-
client.disconnect();
77+
/* create mqtt client */
78+
mqtt.createClient({
79+
uri: 'mqtt://test.mosquitto.org:1883',
80+
clientId: 'your_client_id'
81+
}).then(function(client) {
82+
83+
client.on('closed', function() {
84+
console.log('mqtt.event.closed');
85+
86+
});
87+
88+
client.on('error', function(msg) {
89+
console.log('mqtt.event.error', msg);
90+
91+
});
92+
93+
client.on('message', function(msg) {
94+
console.log('mqtt.event.message', msg);
95+
});
96+
97+
client.on('connect', function() {
98+
console.log('connected');
99+
client.subscribe('/device_00059E18/data', 1);
100+
client.publish('/device_00059E18/data', "test", 1, false);
101+
});
102+
103+
client.connect();
104104
});
105105

106106
```

android/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ repositories {
3333
}
3434

3535
dependencies {
36-
compile "com.facebook.react:react-native:0.19.+"
37-
compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
36+
compile 'com.facebook.react:react-native:0.19.+'
37+
compile('org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2') {
3838
exclude module: 'support-v4'
3939
}
40+
compile 'org.bouncycastle:bcprov-jdk15on:1.54'
41+
4042
}
4143

0 commit comments

Comments
 (0)