DMS ANDROID API

设置androdManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

初始化

static public void init(Context ctx,String pubKey ,String subKey,MqttCallback callback )

    使用DMS的pubkey 和subkey 进行初始化
    DMS.init(getApplicationContext(), "demo", "demo",new MqttCallback() {
        @Override
        public void messageArrived(String topic, MqttMessage msg) throws Exception {
           //收到话题消息
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {

        }

        @Override
        public void connectionLost(Throwable t) {
         //连接中断
        }
    });

连接

static public IMqttToken connect(IMqttActionListener callback) throws MqttException

连接到DMS服务器,连接成功后才能关注和推送消息
DMS.connect(new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {


    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

    }
});

推送消息

static public IMqttDeliveryToken publish(String topic, byte[] payload,  IMqttActionListener callback)throws MqttException, MqttPersistenceException

推送消息到对应话题

DMS.publish("go", "test".getBytes(), new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {

    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

    }
});

关注话题

static public IMqttToken subscribe(String topic,IMqttActionListener callback) throws MqttException 
static public IMqttToken subscribe(String[] topic,IMqttActionListener callback) throws MqttException 
关注某个话题的消息和批量关注
将从init设置的callback中收到pubish到这个话题的消息
DMS.subscribe("go", new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {


    }
    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

    }
});

取消关注话题

static public IMqttToken unsubscribe(String topic, IMqttActionListener callback) throws MqttException 
static public IMqttToken unsubscribe(String[] topic, IMqttActionListener callback) throws MqttException 
取消关注的话题,将不会收到该话题的消息
  DMS.subscribe("go", new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {


    }
    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

    }
});

断开连接

static public IMqttToken disconnect(IMqttActionListener callback) throws MqttException

 DMS.disconnect( new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {


    }
    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {

    }
});