I am trying to create web application to send sms by gsm modem in JSP first I put destination mobile number and sms text in url and get by request.getparameter and first message sent with no problem but when send a message again by referenshing the same page i get this exception:
org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: gnu.io.PortInUseException: org.smslib
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
at org.smslib.Service$1Starter.run(Service.java:276)
I tried to stop gateway and stop service but no hope
My code:
public boolean sendMessage(String strMobileNo,String strSMSText)
{
try
{
OutboundMessage outboundMessage=new OutboundMessage();
SMS message=new SMS();
SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setSmscNumber("+9647701144010");
Service.getInstance().setOutboundMessageNotification(message);
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
outboundMessage.setText(strSMSText);
outboundMessage.setRecipient(strMobileNo);
outboundMessage.setEncoding(Message.MessageEncodings.ENCUCS2);
//outboundMessage.setDeliveryDelay(5000);
Service.getInstance().sendMessage(outboundMessage);
System.out.println(outboundMessage);
gateway.stopGateway();
Service.getInstance().stopService();
Thread.sleep(10000);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
The problem in your code is every time a request is made a new SerialModemGateway is created, which should not be done.
Try to have SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200"); statement only called once your application run, instead of every time a request is made.
try out this Service.getInstance().stopService() in last of your code its working and you can also terminate your program before running it again
I solved this problem with
Service.getInstance().removeGateway(gateway);
Related
I am trying to connect to Azure Service Bus from my webjob using Managed Identities. But unfortunately, I am getting the following error
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "10/10/2020 6:50:38 AM +00:00"
I have enabled Identity and assigned Contributor role for connecting to Service bus. Also, in the code, I have used ClientSettings to create a connection with Service bus.
ClientFactory.createMessageReceiverFromEntityPathAsync(namespace, queueName,new ClientSettings(TokenProvider.createManagedServiceIdentityTokenProvider()), ReceiveMode.RECEIVEANDDELETE).get();
Any idea how to resolve this exception, as it is thrown from built-in class ManagedServiceIdentityTokenProvider. Any help would be really helpful.
Thanks in advance.
EDIT -
I updated the service bus package to 3.2.0. Now I can't see NumberFormatException but I am getting java.lang.RuntimeException: java.net.SocketException: Permission denied: connect even though I have provided the 'Owner' role to the webjob.
If you want to connect Azure Service bus with Azure Managed Identity, we need to assign some special role(Azure Service Bus Data Owner, Azure Service Bus Data Sender and Azure Service Bus Data Receiver ) to the MSI. For more details, please refer to here.
For example
Assing role to the MSI
Code
public static void main( String[] args )
{
IMessageReceiver receiver = null;
try {
receiver= ClientFactory.createMessageReceiverFromEntityPathAsync("bowman1012",
"myqueue",
new ClientSettings(TokenProvider.createManagedIdentityTokenProvider()),
ReceiveMode.PEEKLOCK).get();
IMessage message = receiver.receiveAsync().get();
String content = new String(message.getBody(), UTF_8);
System.out.println(content);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}finally {
Boolean f= receiver.closeAsync().complete(null);
System.out.println(f);
}
}
I am using Paho java client library for my on android App. Code provided below.
MqttAndroidClient client_Panic = new MqttAndroidClient(this.getApplicationContext(), serverURL, CLIENT_ID);
try {
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
options.setCleanSession(false);
options.setKeepAliveInterval(90);
options.setConnectionTimeout(100);
IMqttToken token = client_Panic.connect(options);
//Few callbacks
} catch (MqttException e) {
e.printStackTrace();
}
And publishing messages, when required
String msg = "messages";
MqttMessage message = new MqttMessage();
message.setPayload(msg.getBytes());
try {
client_Panic.publish(topic, message);
} catch (MqttException e) {
e.printStackTrace();
}
It is working fine, but somehow incomplete. What I need is, whenever other client subscribed to same topic, should get the last retained message, which publisher might had published earlier.
For that I searched on their documentation, I got
protected void setWill(String topic,
MqttMessage msg,
int qos,
boolean retained)
So setWill as per documentation have the option to enable the retained option. So I tried with
options.setConnectionTimeout(100);
options.setWill(topic,null,2,true); // This place I added
IMqttToken token = client_Panic.connect(options);
But got error java.lang.IllegalArgumentException at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateWill on the line containing options.setWill.
Is setWill is the correct method for enabling retained true on android, if yes then what parameters need to be provided or else their is any other method for enabling it on android? TIA.
The Will is a very specific message that is only published if the client disconnects uncleanly from the broker (e.g. network drops).
You can not set a null message as the Will message which is what the error is about.
The retained state is specific to a given message so you do not set it globally, it is set on each message. To mark a message as retained when you just call the setRetained(boolean) e.g.
String msg = "messages";
MqttMessage message = new MqttMessage();
message.setRetained(true);
message.setPayload(msg.getBytes());
try {
client_Panic.publish(topic, message);
} catch (MqttException e) {
e.printStackTrace();
}
I'm developing an app to exchange data between an Android phone and a pc. I've chosen xml-rpc as a way to communicate, and as a library i downloaded android-xmlrpc (https://code.google.com/p/android-xmlrpc/).
When I call remote procedures from my phone, I use the XMLRPCClient and it works almost fine.
But then I have to synchronize data from my server to the application, so I need to listen on a given port and wait for a xmlrpc request.
I tried the following code:
protected Void doInBackground(Void... params) {
try { ServerSocket socket = new ServerSocket(8888);
XMLRPCServer server = new XMLRPCServer();
while (true) {
Socket client = socket.accept();
MethodCall call = server.readMethodCall(client);
String name = call.getMethodName();
}
} catch (Exception e) { Log.v(LOG_TAG, "Server: Error", e); }
return null;
}
But it seems it never receives any procedure call. I mean, to debug it I'm trying to use my XMLRPCClient as follow:
XMLRPCClient client = new XMLRPCClient("http://127.0.0.1:8888/");
Object o = client.call("add",1,3);
Log.v(LOG_TAG, o.toString());
And I always get no response. It's stuck on readMethodCall and never reads that the method I requested is "add"... It's strange it gets stuck there. Can't figure the reason why it is stucked here. Do you have any idea ?
I am looking to get/read the Data I passed after I connected a couple of Android Devices, so far I pair, connect and transmit the information between them, but not sure how to implement the reading part, here I am not sure if I should use createRfcommSocketToServiceRecord or listenUsingRfcommWithServiceRecord to create the reading socket for this purpose.
I have two screens, one where the user push a button and transmit the info and the other where the receiver press another button and read the data, I wonder if the sync is incorrect and after I push the "send" button and then the "read" button the connection is unavailable or if this implementation is just not recomendable all together.
These are my two attempts:
Attempt 1:
//Executed after the user press the read data button
private void connectToServerSocket(BluetoothDevice device, UUID uuid) {
try{
BluetoothServerSocket serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(device.getName(),uuid);
//Here is where I get the error:
//io to Server Socket JSR82 Connection is not created, failed or aborted
BluetoothSocket clientSocket = serverSocket.accept();
// Start listening for messages.
StringBuilder incoming = new StringBuilder();
listenForMessages(clientSocket, incoming);
// Add a reference to the socket used to send messages.
transferSocket = clientSocket;
} catch (IOException ioe) {
this.printToast("Excep io toServerSocket:" + ioe.getMessage());
} catch (Exception e) {
this.printToast("Excep toServerSocket:" + e.getMessage());
}
}
Attempt 2:
private void connectToServerSocket(BluetoothDevice device, UUID uuid) {
try{
BluetoothServerSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid);
//clientSocket without method and invoke is not working either
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
clientSocket = (BluetoothSocket) m.invoke(device, 1);
//Here is where I get the error:
//io to Server Socket JSR82 Connection is not created, failed or aborted
clientSocket.connect();
// Start listening for messages.
StringBuilder incoming = new StringBuilder();
listenForMessages(clientSocket, incoming);
// Add a reference to the socket used to send messages.
transferSocket = clientSocket;
} catch (IOException ioe) {
this.printToast("Excep io toServerSocket:" + ioe.getMessage());
} catch (Exception e) {
this.printToast("Excep toServerSocket:" + e.getMessage());
}
}
On serverSocket.accept() or clientSocket.connect() I get the exception:
Connection is not created, failed or aborted
I would appreciate if anyone could guide me towards getting the data reading part working. Thanks.
Take a look at Android's BluetoothChat example included with the Android SDK. I think it does exactly what you want.
$ANDROID_SDK/samples/android-19/legacy/BluetoothChat/src/com/example/android/BluetoothChat
Read the managing the connection part.
Its clearly written in the documentation how to exchange (read/write) info between devices through Bluetooth. http://developer.android.com/guide/topics/connectivity/bluetooth.html
I am trying to learn to use SMSlib to send sms using ZTE MF631 usb modem. Here is my code:
Service.getInstance().startService();
SerialModemGateway gateway = new SerialModemGateway("modem.com5","COM5", 115200, "ZTE", "MF631");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setSmscNumber("+9779800009000");
try {
Service.getInstance().addGateway(gateway);
OutboundMessage outboundMsg = new OutboundMessage("+9779843669921", "Hello there!");
Service.getInstance().sendMessage(outboundMsg);
} finally {
gateway.stopGateway();
Service.getInstance().removeGateway(gateway);
Service.getInstance().stopService();
}
When I run my code I get the following error:
org.smslib.SMSLibException: No gateways are defined.
at org.smslib.Service.startService_Internal(Service.java:295)
at org.smslib.Service.startService(Service.java:229)
at org.smslib.Service.startService(Service.java:196)
at com.mail.utility.SendSmsUtility.sendSMS(SendSmsUtility.java:10)
at com.mail.action.SendSmsAction.sendSms(SendSmsAction.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
I think you are missing something like this before you start service:
Service.getInstance().addGateway(gateway);
Here is SMSlib example.