Java Code Examples for org.eclipse.paho.android.service.MqttAndroidClient#setCallback()

The following examples show how to use org.eclipse.paho.android.service.MqttAndroidClient#setCallback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MQTTManager.java    From EMQ-Android-Toolkit with Apache License 2.0 6 votes vote down vote up
public MqttAndroidClient createClient(String id, String serverURI, String clientId) {
    MqttClientPersistence mqttClientPersistence = new MemoryPersistence();
    MqttAndroidClient client = new MqttAndroidClient(MyApplication.getContext(), serverURI, clientId, mqttClientPersistence);
    client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable cause) {
            LogUtil.e("connectionLost");
            EventBus.getDefault().post(new MQTTActionEvent(Constant.MQTTStatusConstant.CONNECTION_LOST, null, cause));

        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            LogUtil.d("topic is " + topic + ",message is " + message.toString() + ", qos is " + message.getQos());
            EventBus.getDefault().postSticky(new MessageEvent(new EmqMessage(topic, message)));

        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
            LogUtil.d("deliveryComplete");


        }
    });

    mClients.put(id, client);

    return client;

}
 
Example 2
Source File: MainActivity.java    From ActiveMQ-MQTT-Android with Apache License 2.0 5 votes vote down vote up
private void startConnect(String clientID, String serverIP, String port) {
    //服务器地址
    String  uri ="tcp://";
    uri=uri+serverIP+":"+port;
    Log.d("MainActivity",uri+"  "+clientID);
    /**
     * 连接的选项
     */
    MqttConnectOptions conOpt = new MqttConnectOptions();
    /**设计连接超时时间*/
    conOpt.setConnectionTimeout(3000);
    /**设计心跳间隔时间300秒*/
    conOpt.setKeepAliveInterval(300);
    /**
     * 创建连接对象
     */
     client = new MqttAndroidClient(this,uri, clientID);
    /**
     * 连接后设计一个回调
     */
    client.setCallback(new MqttCallbackHandler(this, clientID));
    /**
     * 开始连接服务器,参数:ConnectionOptions,  IMqttActionListener
     */
    try {
        client.connect(conOpt, null, new ConnectCallBackHandler(this));
    } catch (MqttException e) {
        e.printStackTrace();
    }

}
 
Example 3
Source File: TrafficService.java    From AirMapSDK-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize an TrafficService to receive traffic alerts and situational awareness
 *
 * @param context An Android Context
 */
public TrafficService(Context context) {
    String clientId = UUID.randomUUID().toString();
    client = new MqttAndroidClient(context, mqttBaseUrl, clientId);
    client.setCallback(new MqttEventCallback());
    options = new MqttConnectOptions();
    options.setCleanSession(true);
    options.setKeepAliveInterval(15);
    options.setPassword(AirMap.getInstance().getAuthToken().toCharArray());
    connectionState = ConnectionState.Disconnected;
    allTraffic = new CopyOnWriteArrayList<>(); //Thread safe list
    listeners = new ArrayList<>();
    checkForUpdatedFlight = false;
    currentFlightCallback = new CurrentFlightAirMapCallback();
    actionListener = new MqttActionCallback();
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            clearOldTraffic();
            updateTrafficProjections();
        }
    }, 0, 1000); //Clear old traffic every second

    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if (checkForUpdatedFlight) {
                AirMap.getCurrentFlight(new AirMapCallback<AirMapFlight>() {
                    @Override
                    public void onSuccess(AirMapFlight response) {
                        if (response != null && !response.getFlightId().equals(flightId)) {
                            connect();
                        }
                    }

                    @Override
                    public void onError(AirMapException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }, 0, 1000 * 60); //Update current flight every minute

    handler = new Handler(Looper.getMainLooper());
}