I have code
DatagramConnection _dc =(DatagramConnection)Connector.open("sms://");
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d); //Receive the sms
byte[] bytes = d.getData();
String address = d.getAddress(); //The address of the sms is put on a string.
String msg = new String(bytes);
Does the above code listen for incoming SMS's on a continuous basis, or does it just listen for 1 sms?
If it just listens for 1 SMS can you please provide me with code to listen for SMS's on a continuous basis.
Your code reads only a single SMS. If you need to read every SMS delivered, you need a loop like this one posted in the official knowledge base article:
DatagramConnection _dc = (DatagramConnection)Connector.open("sms://");
for(;;) {
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
byte[] bytes = d.getData();
String address = d.getAddress();
String msg = new String(bytes);
System.out.println( "Received SMS text from " + address + " : " + msg);
}
Now a question arises: Is the BB OS delivering SMSs to listening apps in a serial way? If it were (I think so but never tested to that extent), then all you need to do is to forward the message as quickly as possible to a consumer (otherwise you would be hogging the connection listener thread during the SMS processing).
Related
Sniffing the packets transmitted for a simple echo service Python gRPC example shows that there's 17 packets transmitted for a single message exchange (including SYNs) if the channel is new and 6-8 packets when a channel is reused.
Here's the example protofile:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
And here's the Python code to run it:
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
channel = grpc.insecure_channel('localhost:50051')
stub = helloworld_pb2_grpc.GreeterStub(channel)
# Try with a brand new channel first
print(stub.SayHello(helloworld_pb2.HelloRequest(name='you'))) # 17 packets
# Let's reuse the channel this time
print(stub.SayHello(helloworld_pb2.HelloRequest(name='you'))) # 6-8 packets; still too much :-(
A Wireshark capture shows that all the packets be between 66 and 176 bytes long, so there's obviously no need to transmit a huge amount of data in total. They could well fit into 1 for each request and response (totalling to 2 packets), but for some reason I'm consistently seeing them fragmented like this (note: testing on loopback, Linux 5.4).
Is there any way to reduce the number of packets?
I'm using TComPort Component in Delphi to send Bulk SMS messages via GSM Modem. I just need to make sure that The Modem Reply "OK" before sending the next sms.
Here Is My Code :
with cds_kontak do
if not cds_kontak.Eof then
begin
sProgressBar1.Position := sProgressBar1.Position + 1;
ComPort1.WriteStr('at+cmgs="' + cds_kontak.FieldValues['NO_SELULER'] + '"'+#13);
ComPort1.WriteStr(mmo_sms.Text+#26);
cds_kontak.Next;
end
I would like to insert code to check the Modem Reply, if the Reply is "OK" then continue to next list.
Device side mqtt publishes data to mosquitto broker installed on Raspberry Pi to a topic.
how to subscribe the data from the mosquitto broker in 2 ways.
Normal google example search gives me code samples. But what i want is 2 ways i thought one is MQTT API to subscribe providing topic which listens every time checking whether data has arrived or not which reduces the CPU performance and speed.
So, Other is if a message has arrived in the topic then it should call back saying message arrived instead of me going and looking into topic everytime checking is cumbersome. The second way increases CPU performance.
MemoryPersistence memoryPersistence = new MemoryPersistence();
MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setUserName("mqttuser");
conOpt.setPassword(new String("mqttpassword").toCharArray());
conOpt.setCleanSession(true);
try {
MqttAsyncClient mqttAsyncClient = new MqttAsyncClient("tcp://localhost:1883", "1883", memoryPersistence);
mqttAsyncClient.setCallback(new MqttConnectionCallback());
if (!mqttAsyncClient.isConnected()) {
IMqttToken token = mqttAsyncClient.connect(conOpt);
logger.info();
System.out.println("Connected");
}
} catch (MqttException e) {
e.printStackTrace();
System.out.println("Unable to set up client: "+e.toString());
System.exit(1);
}
this is my listener class overridden method
#Override
public void messageArrived(String topic, MqttMessage msgData) throws Exception {
String time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println("Time:\t" +time +
" Topic:\t" + topic +
" Message:\t" + new String(msgData.getPayload()) +
" QoS:\t" + msgData.getQos());
}
Need some clearance from coding side how this is accompolished in asynchronous ways.
Please correct if my understanding is wrong, specifically on how listener listenes on topicname and how the binding is done.
You need to use the subscribe() method on the mqttAsyncClient object (after connection has completed)
You can call this multiple times to subscribe to multiple topics.
Messages will be delivered to the messageArrived() callback and you can use the topic argument to determine which subscription the message arrived from.
I have to publish data via MQTT on thingsboard.io using Arduino and simultaneously fetch the data on the same board.
Is it possible to do so? if yes then how?
Otherwise, I would need a sample code for client subscription to a topic on thingsboard.io
yes, you can do so.
Thingsboard, use the same topic but it differentiates on the basis of Token no assign to your device.
For publishing the payload to thingsboard:-
// Prepare a JSON payload string
String payload = "{";
payload += "\"temperature\":"; payload += temperature; payload += ",";
payload += "\"humidity\":"; payload += humidity;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
For Subscribing:-
client.susbcribe("v1/devices/me/telemetry")
As your device will both connect to MQTT broker and authorised via Token no, so Token is only responsible for one to one connectivity with MQTT Broker
I have build a socket to transfer message between client and server on IOS.
if(CFReadStreamSetClient(readStream, registeredEvents, readCallBack, &myContext))
{
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
if (!CFReadStreamOpen(readStream)) {
CCLog("Error Open Read Stream");
/* error handling */
}
and readCallBack function
void readCallBack(CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo)
{
switch(eventType) {
case kCFStreamEventHasBytesAvailable:{
UInt8 bufr[10240];
int bytesRead = CFReadStreamRead(stream, bufr, sizeof(bufr));
if(bytesRead >0 ){
NSLog(#"Read: %d", bytesRead);
}
break;
}
case kCFStreamEventErrorOccurred:
NSLog(#"A Read Stream Error Has Occurred!");
case kCFStreamEventEndEncountered:
NSLog(#"A Read Stream Event End!");
default:
break;
}
}
But when client send multi message to server by multi time.
Server always read it as one message.
Example:
Client send message 1st: Message1
Client send message 2nd: Message2
But when server read message from client:
Result is: Message1Message2
How can i split it as 2 messages. (I don't know the size of each message)
Thanks.
You have to make up a protocol of your own. For example, clients can append \n to each message so that the server can split messages by \n. However if your messages can have \n character in them, you can modify your protocol to first send the length of the message, again split by \n:
Client sends: 8\nMessage1
Client sends: 14\nAnotherMessage
Server receives: 8\nMessage114\nAnotherMessage
So you read up to first \n and get the content length. Then you read that many characters.
Be careful with the difference between byte streams and text streams though. You can google about TCP text streams to learn more about them. Your best bet is to send number of bytes being sent, instead of number of characters.
And be aware that sometimes, you will not receive a message as a whole. For example the following is possible:
Client sends: 8\nMessage1
Client sends: 14\nAnotherMessage
Server receives: 8\nMessage11
Server receives: 4\nAnotherMessage