Does anyone know of an MQTT broker that runs on an Android smartphone? I tried to Google and found nothing, and on the app store there seems to be only one app with just 10 downloads, so I'm not sure how well it works.
Add these dependencies to the gradle
dependencies{
compile 'io.moquette:moquette-netty-parser:0.8.1'
compile 'io.moquette:moquette-broker:0.8.1'
compile 'io.moquette:moquette-parser-commons:0.8.1'
}
And use
io.moquette.server.Server server = new io.moquette.server.Server();
server.startServer();
to start broker server. the default URI is tcp://localhost:1883
For me server.startServer(); gave me exception as it is unable to create a file BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME.
So, I changed the destination of the BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME
using this code below code and it worked for me.
try {
MemoryConfig memoryConfig = new MemoryConfig(new Properties());
memoryConfig.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME);
server.startServer(memoryConfig);
// server.startServer();//is not working due to DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME;
Log.d(TAG,"Server Started");
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e){ e.printStackTrace(); }
And Use Paho libraries for android
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
To create a client and connect to tcp://localhost:1883 and subscribe for a topic and start publishing and receiving messages.
moquette library for android.
I have developed an App specifically for this , please download here -
It has inbuilt broker and client too..all for free , connect your
devices to android phone via hotspot or wifi.
https://play.google.com/store/apps/details?id=server.com.mqtt
You can run the mosquitto mqtt broker within the Termux terminal.
Install Termux using e.g. F-Droid
Open/run the Termux Terminal emulator
Install mosquitto
pkg install mosquitto
Start mosquitto within the terminal
mosquitto
That's it. The server will listen on the default port 1883.
Configuration see at mosquitto.
Here is an MQTT broker library I have adapted to Android: https://github.com/interaktionsbyran/moquette
You'll have to make your own Android app though, it is just a library.
Related
I'm trying to develope an android application (with min-API-Level 28) with Java that communicates with an other device over a socket connection. One device is creating a Server instance and the other device should connect to this server.
For this purpose, I'm using Android-Emulator instances provided by Android Studio.
I followed the instructions at https://developer.android.com/studio/run/emulator-networking#connecting , but encountered that the provided IP-Adress ranges are not the same for every API-Level.
One Emulator-Instance running API 28 is using 192.168.232.0/24 address range, the other one running API-30 is using the provided 10.0.2.0/24 range.
So given the following scenario:
the Server (running on an API-28 Emulator-Instance) is creating the ServerSocket:
this.serverSocket = new ServerSocket();
// 192.168.232.2 is the device's IP-Address IF(!) the Emulator-Instance uses API Level 28
// 55555 the specific ServerPort
this.serverSocket.bind(new InetSocketAddress("192.168.232.2", 55555));
and after that accept incoming connections:
client = this.serverSocket.accept();
the Client (running on an API-30 Emulator-Instance) is trying to connect to this Server:
this.server = new Socket("10.0.2.2", 55556);
as described in the provided docs, 10.0.2.2 is the developement Device aka my Computer and 55556 a chosen local-port.
On the Server-Instance I added a redirect via the android emulator console
redir add tcp:55556:55555
But the Client can't connect to the Server. So far I'm clueless.
Any ideas what went wrong here?
#Edit - Tried to clarify that I'm using Android Emulators instead of real Devices
I am trying to connect to a bluetooth device, which i can pair to when setting the target device in pair mode, but trying to reconnect with the following method causes errors.
The Code that causes it:
StreamConnection streamConnection = null;
try {
streamConnection = (StreamConnection)Connector.open("btspp://A0E6F8FECB26:1;authenticate=false;encrypt=false;master=false");
callback.onSocketCreated(streamConnection);
callback.onConnectionSucceed();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The Exception That occurs
java.io.IOException: Failed to connect. [13] Permission denied
at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl(Native Method)
at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnection(BluetoothStackBlueZ.java:574)
at com.intel.bluetooth.BluetoothRFCommClientConnection.<init>(BluetoothRFCommClientConnection.java:37)
at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:379)
at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:162)
at javax.microedition.io.Connector.open(Connector.java:83)
at insight.utils.ConnectionEstablisher.run(ConnectionEstablisher.java:170)
I have to use Java and this is the only Library I found (That somewhat works) for this purpose:
BlueCove version 2.1.1-SNAPSHOT on bluez
I am on Arch Linux with the newest bluez Version and I have already tried This Fix to no avail.
Any help is appreciated
So essentially, the issue was the entire Bluetooth Stack / how it propagates "pairing requests" to the Bluetooth Stack.
As a sidenote I was using BlueZ (5.56) on Arch Linux and the bluecove library for Java.
I was looking into alternative Bluetooth Stacks and thought about trying the same code on Windows 10, which doesnt use the BlueZ Stack.
Windows then popped up a window where it asked if i wanted to pair to this device.
This was the main difference between Windows and Linux.
Windows / its bluetooth stack or whatever is responsible for it, actually registered the device as "paired".
This then allowed me to re-connect to the device without any "Permission Denied" issues.
Hope this helps anyone that has to deal with this library or has similar issues.
I'm developing a web app in Java (with Spark) and I want it to use JmDNS to be able to find this server in my Android app. I was following steps from JmDNS page with service registration, and my code looks like this:
try {
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Register a service
ServiceInfo serviceInfo = ServiceInfo.create("_http._tcp.local.", "smart_dom", 4567, "path=index.html");
jmdns.registerService(serviceInfo);
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
I implemented the part about discovering services in my Android app, but it finds nothing. I made some changes according to this question, and even downloaded ZeroConf Browser (it relies on JmDNS), but even this app doesn't see my web app.
My server runs on Windows PC. I know that Windows doesn't support mDNS, but Java should be independant from the platform. What can I be doing wrong?
If anyone was wondering what was wrong, I found the answer.
In my case the problem was with JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());, especially with InetAddress.getLocalHost() which returned 127.0.0.1 not my LAN adapter wi-fi address. The problem was solved using this solution. It bases on looking for the correct interface via NetworkInterface.getNetworkInterfaces().
I am unable to connect IBM websphere MQ
Can anyone tell me what are the initial setting need to be done in MQ to acces it from remote machine using simple java code?
Besides the MQ samples shipped with the product, there is also a long list of Java & Java/JMS samples here.
The recommended practice for MQ Java (non-JMS) is:
Hashtable mqht = new Hashtable();
mqht.put(CMQC.CHANNEL_PROPERTY, channelName);
mqht.put(CMQC.HOST_NAME_PROPERTY, hostName);
mqht.put(CMQC.PORT_PROPERTY, new Integer(portNumber));
try
{
MQQueueManager qMgr = new MQQueueManager(qMgrName, mqht);
System.out.println("Successfully connected to "+ qMgrName);
}
catch (com.ibm.mq.MQException mqex)
{
System.out.println("MQException cc=" +mqex.completionCode + " : rc=" + mqex.reasonCode);
}
A few points to make sure you are able to connect to Websphere MQ using Java code :
Have you validated your MQ server deployment using these steps "Verifying Installation" in following deployment instructions http://sadockobeth.blogspot.in/2014/03/how-to-install-ibm-websphere-mq-75-on.html
Have you started a listener for a Queue Manager that is configured on your MQ server. You can find command to start Listener here http://bencane.com/2013/04/22/websphere-mq-cheat-sheet-for-system-administrators/
With default installation the connection from a Java application over TCP will fail because of channel authentication enforced by default. You can confirm this is the case by looking at Queue manager error logs. Would suggest you to look at appropriate ways to configure channel authentication to allow the Java application to connect. One method ok for Non-Production or test set up is to disable channel (Please use this with caution).
If all of the above are taken care and still you are not able to connect than you will need to look at your Java client code and get more information from error codes that your code is seeing. If there are any genuine network connectivity issues or some thing else like firewall preventing connection etc.
I am making an application using phonejs. I want to create socket to get live data from server to display. I found HTML5 WebSocket working fine with web browser but not with mobile device. I used phonegap-build from http://build.phonegap.com to build application.
I tried to add 3rd party plugin(com.ququplay.websocket.websocket) of phonegap and run application with android mobile device but the application got crashed.
Here is how I work with WebSocket:
var initSocket = function(){
try{
var socket = new WebSocket(websocket.host);
socket.onopen = function(msg){ console.log("Welcome - Live Data"); };
socket.onmessage = function(msg){
console.log('-------- live data ------');
// Here some work
}
}
catch(ex){ console.log(ex); }
};
Is there any other way to make socket run on android phone from javascript? Or any connector that can create socket on android phone from javascript?
I created cordova project with android platform and added WebSocket plugin from https://github.com/knowledgecode/WebSocket-for-Android.
I merged my application code in assets. This plugin handled socket related operations without need of changing my WebSocket related code.