Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] automatically subscribe after a successful connection

In order to automatically subscribe after a successful connection. I write a broadcastreceiver  to receive  the intents  of  "MqttService.callbackToActivity.v0". I think this should be easy.

-------------------------------------------------------------
private void registerReceiverMqtt() {
broadcastReceiver = new BroadcastReceiver() {
static final String TAG = "MqttService";
static final String CALLBACK_ACTION = TAG + ".callbackAction";
static final String CONNECT_ACTION = "connect";
static final String CALLBACK_STATUS = TAG + ".callbackStatus";
@Override
public void onReceive(Context context, Intent intent) {
String action="">
Bundle data = "">
Status status= (Status) data.getSerializable(CALLBACK_STATUS);
if (CONNECT_ACTION.equals(action)) {
if(status ==Status.OK){
try {
mqttClient.subscribe(Constants.TOPIC, 1);
} catch (MqttSecurityException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
}
}};
IntentFilter filter = new IntentFilter();
String  fileName="MqttService.callbackToActivity.v0";
filter.addAction(fileName );
registerReceiver(broadcastReceiver, filter);
}
-------------------------------------------------------------

------------------

Jacarri Chan


Back to the top