Accessing Local broker via IP address using Paho Android client2019 Community Moderator ElectionAndroid Asynchronous Http Client : @Override onSuccess(int statusCode, Header[] headers, String content)MQTT+Mosquitto+Javascript in windowsNot receiving mqtt message on mobile web-client via paho mqttws31.jsPaho MQTT client behaviour when broker times out / client disconnectsScaling Mqtt connections to mosquitto brokerError copying object to RealmPaho MQTT client local address and portPaho clients disconnecting on Mosquitto BrokerMqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection headerIs there a way to send messages to MQTT mosquitto broker when a database change happens?

Is there any common country to visit for uk and schengen visa?

Why do I have a large white artefact on the rendered image?

Pre-Employment Background Check With Consent For Future Checks

What kind of footwear is suitable for walking in micro gravity environment?

Why are there no stars visible in cislunar space?

Norwegian Refugee travel document

Are hand made posters acceptable in Academia?

How are passwords stolen from companies if they only store hashes?

Isn't the word "experience" wrongly used in this context?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

"Marked down as someone wanting to sell shares." What does that mean?

Weird lines in Microsoft Word

Can "few" be used as a subject? If so, what is the rule?

PTIJ: Why do we make a Lulav holder?

10 year ban after applying for a UK student visa

Gauss brackets with double vertical lines

PTIJ: Which Dr. Seuss books should one obtain?

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Why is "la Gestapo" feminine?

Symbolism of 18 Journeyers

How to balance a monster modification (zombie)?

How to find the largest number(s) in a list of elements, possibly non-unique?

UK Tourist Visa- Enquiry

What is the tangent at a sharp point on a curve?



Accessing Local broker via IP address using Paho Android client



2019 Community Moderator ElectionAndroid Asynchronous Http Client : @Override onSuccess(int statusCode, Header[] headers, String content)MQTT+Mosquitto+Javascript in windowsNot receiving mqtt message on mobile web-client via paho mqttws31.jsPaho MQTT client behaviour when broker times out / client disconnectsScaling Mqtt connections to mosquitto brokerError copying object to RealmPaho MQTT client local address and portPaho clients disconnecting on Mosquitto BrokerMqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection headerIs there a way to send messages to MQTT mosquitto broker when a database change happens?










1















I want to connect to a local broker like tcp://192.168.1.25:80 but the android mqtt client wont only string address, like tcp://test.mosquitto.org. For now, I change the emulator hosts file, but in a smartphone without the root it is impossible. There is a bug-fix for this?
There is my code:



private MqttAndroidClient mqttAndroidClient;

private final String serverUri = "wss://192.168.1.25:443";

private final String clientId = "AndroidID";
private final String subscriptionTopicData = "/data";


private final String username = "UUUUU";
private final String password = "PPPPP";

public MqttHelper(Context context)
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId, new MemoryPersistence(), MqttAndroidClient.Ack.AUTO_ACK);
mqttAndroidClient.setCallback(new MqttCallbackExtended()
@Override
public void connectComplete(boolean b, String s)
Log.w("mqtt", s);


@Override
public void connectionLost(Throwable throwable)
Log.w("mqtt", throwable.toString());


@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
Log.w("Mqtt", mqttMessage.toString());


@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)


);
connect();


public void setCallback(MqttCallbackExtended callback)
mqttAndroidClient.setCallback(callback);


private void connect()
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setSocketFactory(SSLSocketFactory.getDefault());
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
try
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("MQTT", "Failed to connect to: " + serverUri + ": "+exception.toString());
Log.w("Mqtt", exception.fillInStackTrace());


);

catch (MqttException ex)
Log.d("MQTT-------",Log.getStackTraceString(ex), ex.getCause());




private void subscribeToTopic()
try
mqttAndroidClient.subscribe(subscriptionTopicData, 0, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Subscribed fail!");

);
doReconnect=true;
catch (MqttException ex)
System.err.println("Exception whilst subscribing");
ex.printStackTrace();




doReconnect is for a workaround for the re-subscribe of all the topic.



The error:



 W/Mqtt: MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection header
at helper.MqttHelper$2.onFailure(MqttHelper.java:99)
at org.eclipse.paho.android.service.MqttTokenAndroid.notifyFailure(MqttTokenAndroid.java:146)
at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction(MqttAndroidClient.java:1501)
at org.eclipse.paho.android.service.MqttAndroidClient.connectAction(MqttAndroidClient.java:1439)
at org.eclipse.paho.android.service.MqttAndroidClient.onReceive(MqttAndroidClient.java:1368)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.IOException: WebSocket Response header: Incorrect connection header
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:143)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:76)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:63)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)


UPDATE



I tried to connect to cloudMQTT with the ip address, traslated by a dns, and it works, also over WSS protocol. So, I think, that it's a broker config or a local access to my lan devices by android devices.
I also tried to connect to my local device using chrome and it sees the device, so I think is also a broker config.



UPDATE 2



I made a simple mosquitto broker, in my computer, for testing. Same Issiue, but I can put on stack the configuration:



bind_address localhost
port 1883
listener 443 192.168.1.25/my.broker.io
protocol websockets
message_size_limit 1500


This time if I try to access to the ip, the connection go in timeout, than if I try to connet to my.broker.io the error is:



W/Mqtt: Failed to connect to: ws://my.broker.io:443MqttException (0) - java.net.UnknownHostException: my.broker.io









share|improve this question
























  • Could you check the stacktrace of the error passed in onFailure?

    – lelloman
    Mar 11 at 8:33











  • As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

    – hardillb
    Mar 11 at 9:00











  • Add The stackTrace. The Ip address is correct and working.

    – Matteo Sausto
    Mar 11 at 9:03












  • yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

    – hardillb
    Mar 11 at 9:21











  • I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

    – Matteo Sausto
    Mar 11 at 9:31
















1















I want to connect to a local broker like tcp://192.168.1.25:80 but the android mqtt client wont only string address, like tcp://test.mosquitto.org. For now, I change the emulator hosts file, but in a smartphone without the root it is impossible. There is a bug-fix for this?
There is my code:



private MqttAndroidClient mqttAndroidClient;

private final String serverUri = "wss://192.168.1.25:443";

private final String clientId = "AndroidID";
private final String subscriptionTopicData = "/data";


private final String username = "UUUUU";
private final String password = "PPPPP";

public MqttHelper(Context context)
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId, new MemoryPersistence(), MqttAndroidClient.Ack.AUTO_ACK);
mqttAndroidClient.setCallback(new MqttCallbackExtended()
@Override
public void connectComplete(boolean b, String s)
Log.w("mqtt", s);


@Override
public void connectionLost(Throwable throwable)
Log.w("mqtt", throwable.toString());


@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
Log.w("Mqtt", mqttMessage.toString());


@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)


);
connect();


public void setCallback(MqttCallbackExtended callback)
mqttAndroidClient.setCallback(callback);


private void connect()
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setSocketFactory(SSLSocketFactory.getDefault());
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
try
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("MQTT", "Failed to connect to: " + serverUri + ": "+exception.toString());
Log.w("Mqtt", exception.fillInStackTrace());


);

catch (MqttException ex)
Log.d("MQTT-------",Log.getStackTraceString(ex), ex.getCause());




private void subscribeToTopic()
try
mqttAndroidClient.subscribe(subscriptionTopicData, 0, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Subscribed fail!");

);
doReconnect=true;
catch (MqttException ex)
System.err.println("Exception whilst subscribing");
ex.printStackTrace();




doReconnect is for a workaround for the re-subscribe of all the topic.



The error:



 W/Mqtt: MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection header
at helper.MqttHelper$2.onFailure(MqttHelper.java:99)
at org.eclipse.paho.android.service.MqttTokenAndroid.notifyFailure(MqttTokenAndroid.java:146)
at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction(MqttAndroidClient.java:1501)
at org.eclipse.paho.android.service.MqttAndroidClient.connectAction(MqttAndroidClient.java:1439)
at org.eclipse.paho.android.service.MqttAndroidClient.onReceive(MqttAndroidClient.java:1368)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.IOException: WebSocket Response header: Incorrect connection header
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:143)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:76)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:63)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)


UPDATE



I tried to connect to cloudMQTT with the ip address, traslated by a dns, and it works, also over WSS protocol. So, I think, that it's a broker config or a local access to my lan devices by android devices.
I also tried to connect to my local device using chrome and it sees the device, so I think is also a broker config.



UPDATE 2



I made a simple mosquitto broker, in my computer, for testing. Same Issiue, but I can put on stack the configuration:



bind_address localhost
port 1883
listener 443 192.168.1.25/my.broker.io
protocol websockets
message_size_limit 1500


This time if I try to access to the ip, the connection go in timeout, than if I try to connet to my.broker.io the error is:



W/Mqtt: Failed to connect to: ws://my.broker.io:443MqttException (0) - java.net.UnknownHostException: my.broker.io









share|improve this question
























  • Could you check the stacktrace of the error passed in onFailure?

    – lelloman
    Mar 11 at 8:33











  • As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

    – hardillb
    Mar 11 at 9:00











  • Add The stackTrace. The Ip address is correct and working.

    – Matteo Sausto
    Mar 11 at 9:03












  • yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

    – hardillb
    Mar 11 at 9:21











  • I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

    – Matteo Sausto
    Mar 11 at 9:31














1












1








1


1






I want to connect to a local broker like tcp://192.168.1.25:80 but the android mqtt client wont only string address, like tcp://test.mosquitto.org. For now, I change the emulator hosts file, but in a smartphone without the root it is impossible. There is a bug-fix for this?
There is my code:



private MqttAndroidClient mqttAndroidClient;

private final String serverUri = "wss://192.168.1.25:443";

private final String clientId = "AndroidID";
private final String subscriptionTopicData = "/data";


private final String username = "UUUUU";
private final String password = "PPPPP";

public MqttHelper(Context context)
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId, new MemoryPersistence(), MqttAndroidClient.Ack.AUTO_ACK);
mqttAndroidClient.setCallback(new MqttCallbackExtended()
@Override
public void connectComplete(boolean b, String s)
Log.w("mqtt", s);


@Override
public void connectionLost(Throwable throwable)
Log.w("mqtt", throwable.toString());


@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
Log.w("Mqtt", mqttMessage.toString());


@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)


);
connect();


public void setCallback(MqttCallbackExtended callback)
mqttAndroidClient.setCallback(callback);


private void connect()
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setSocketFactory(SSLSocketFactory.getDefault());
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
try
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("MQTT", "Failed to connect to: " + serverUri + ": "+exception.toString());
Log.w("Mqtt", exception.fillInStackTrace());


);

catch (MqttException ex)
Log.d("MQTT-------",Log.getStackTraceString(ex), ex.getCause());




private void subscribeToTopic()
try
mqttAndroidClient.subscribe(subscriptionTopicData, 0, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Subscribed fail!");

);
doReconnect=true;
catch (MqttException ex)
System.err.println("Exception whilst subscribing");
ex.printStackTrace();




doReconnect is for a workaround for the re-subscribe of all the topic.



The error:



 W/Mqtt: MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection header
at helper.MqttHelper$2.onFailure(MqttHelper.java:99)
at org.eclipse.paho.android.service.MqttTokenAndroid.notifyFailure(MqttTokenAndroid.java:146)
at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction(MqttAndroidClient.java:1501)
at org.eclipse.paho.android.service.MqttAndroidClient.connectAction(MqttAndroidClient.java:1439)
at org.eclipse.paho.android.service.MqttAndroidClient.onReceive(MqttAndroidClient.java:1368)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.IOException: WebSocket Response header: Incorrect connection header
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:143)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:76)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:63)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)


UPDATE



I tried to connect to cloudMQTT with the ip address, traslated by a dns, and it works, also over WSS protocol. So, I think, that it's a broker config or a local access to my lan devices by android devices.
I also tried to connect to my local device using chrome and it sees the device, so I think is also a broker config.



UPDATE 2



I made a simple mosquitto broker, in my computer, for testing. Same Issiue, but I can put on stack the configuration:



bind_address localhost
port 1883
listener 443 192.168.1.25/my.broker.io
protocol websockets
message_size_limit 1500


This time if I try to access to the ip, the connection go in timeout, than if I try to connet to my.broker.io the error is:



W/Mqtt: Failed to connect to: ws://my.broker.io:443MqttException (0) - java.net.UnknownHostException: my.broker.io









share|improve this question
















I want to connect to a local broker like tcp://192.168.1.25:80 but the android mqtt client wont only string address, like tcp://test.mosquitto.org. For now, I change the emulator hosts file, but in a smartphone without the root it is impossible. There is a bug-fix for this?
There is my code:



private MqttAndroidClient mqttAndroidClient;

private final String serverUri = "wss://192.168.1.25:443";

private final String clientId = "AndroidID";
private final String subscriptionTopicData = "/data";


private final String username = "UUUUU";
private final String password = "PPPPP";

public MqttHelper(Context context)
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId, new MemoryPersistence(), MqttAndroidClient.Ack.AUTO_ACK);
mqttAndroidClient.setCallback(new MqttCallbackExtended()
@Override
public void connectComplete(boolean b, String s)
Log.w("mqtt", s);


@Override
public void connectionLost(Throwable throwable)
Log.w("mqtt", throwable.toString());


@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
Log.w("Mqtt", mqttMessage.toString());


@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)


);
connect();


public void setCallback(MqttCallbackExtended callback)
mqttAndroidClient.setCallback(callback);


private void connect()
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setSocketFactory(SSLSocketFactory.getDefault());
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
try
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("MQTT", "Failed to connect to: " + serverUri + ": "+exception.toString());
Log.w("Mqtt", exception.fillInStackTrace());


);

catch (MqttException ex)
Log.d("MQTT-------",Log.getStackTraceString(ex), ex.getCause());




private void subscribeToTopic()
try
mqttAndroidClient.subscribe(subscriptionTopicData, 0, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Subscribed fail!");

);
doReconnect=true;
catch (MqttException ex)
System.err.println("Exception whilst subscribing");
ex.printStackTrace();




doReconnect is for a workaround for the re-subscribe of all the topic.



The error:



 W/Mqtt: MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection header
at helper.MqttHelper$2.onFailure(MqttHelper.java:99)
at org.eclipse.paho.android.service.MqttTokenAndroid.notifyFailure(MqttTokenAndroid.java:146)
at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction(MqttAndroidClient.java:1501)
at org.eclipse.paho.android.service.MqttAndroidClient.connectAction(MqttAndroidClient.java:1439)
at org.eclipse.paho.android.service.MqttAndroidClient.onReceive(MqttAndroidClient.java:1368)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.IOException: WebSocket Response header: Incorrect connection header
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:143)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:76)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:63)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)


UPDATE



I tried to connect to cloudMQTT with the ip address, traslated by a dns, and it works, also over WSS protocol. So, I think, that it's a broker config or a local access to my lan devices by android devices.
I also tried to connect to my local device using chrome and it sees the device, so I think is also a broker config.



UPDATE 2



I made a simple mosquitto broker, in my computer, for testing. Same Issiue, but I can put on stack the configuration:



bind_address localhost
port 1883
listener 443 192.168.1.25/my.broker.io
protocol websockets
message_size_limit 1500


This time if I try to access to the ip, the connection go in timeout, than if I try to connet to my.broker.io the error is:



W/Mqtt: Failed to connect to: ws://my.broker.io:443MqttException (0) - java.net.UnknownHostException: my.broker.io






android mqtt paho






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 12:55







Matteo Sausto

















asked Mar 7 at 17:00









Matteo SaustoMatteo Sausto

134




134












  • Could you check the stacktrace of the error passed in onFailure?

    – lelloman
    Mar 11 at 8:33











  • As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

    – hardillb
    Mar 11 at 9:00











  • Add The stackTrace. The Ip address is correct and working.

    – Matteo Sausto
    Mar 11 at 9:03












  • yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

    – hardillb
    Mar 11 at 9:21











  • I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

    – Matteo Sausto
    Mar 11 at 9:31


















  • Could you check the stacktrace of the error passed in onFailure?

    – lelloman
    Mar 11 at 8:33











  • As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

    – hardillb
    Mar 11 at 9:00











  • Add The stackTrace. The Ip address is correct and working.

    – Matteo Sausto
    Mar 11 at 9:03












  • yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

    – hardillb
    Mar 11 at 9:21











  • I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

    – Matteo Sausto
    Mar 11 at 9:31

















Could you check the stacktrace of the error passed in onFailure?

– lelloman
Mar 11 at 8:33





Could you check the stacktrace of the error passed in onFailure?

– lelloman
Mar 11 at 8:33













As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

– hardillb
Mar 11 at 9:00





As you are using an ip address, does the certificate for that broker include the IP address as a CN or SAN entry?

– hardillb
Mar 11 at 9:00













Add The stackTrace. The Ip address is correct and working.

– Matteo Sausto
Mar 11 at 9:03






Add The stackTrace. The Ip address is correct and working.

– Matteo Sausto
Mar 11 at 9:03














yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

– hardillb
Mar 11 at 9:21





yes, but I'm suggesting that because this is a TLS (wss://) connection that the certificate might not verify because it doesn't have a domain name and you are using the built in CA certs. Also given the actual error include details of how the broker is configured.

– hardillb
Mar 11 at 9:21













I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

– Matteo Sausto
Mar 11 at 9:31






I don't know how to take a better error than that. The CA that I install is in the device, but the broker's builder remind me that the application it works also whitout the CA certs. I added the SSLSocketFactory.getDefault() to a socketFactory because the Broker send to me same certificates. Also if I edit the hosts file with the ip address and add the string in my code it works. The problem, I think, is on the paho client android that not accept the ip address like the address I put in my code.

– Matteo Sausto
Mar 11 at 9:31













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049202%2faccessing-local-broker-via-ip-address-using-paho-android-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049202%2faccessing-local-broker-via-ip-address-using-paho-android-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme