Receive text message using J2ME

I am trying to make a J2ME application to send text messages SEND and RECEIVE. I ended up with part of it sending, but I cannot receive any message.

Below I tried to get a text message;

try { MessageConnection conn = (MessageConnection) Connector.open("sms://:50001"); conn.setMessageListener(new MessageListener() { public void notifyIncomingMessage(MessageConnection conn) { try { Message msg; msg = conn.receive(); if (msg instanceof TextMessage) { TextMessage tmsg = (TextMessage) msg; stringItem.setText("Msg: " + tmsg.getPayloadText()); System.out.println(tmsg.getPayloadText()); } // else if(msg instanceof BinaryMessage) { // ..... // } else { // ...... // } } catch (IOException ex) { ex.printStackTrace(); } finally { try { conn.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }); } catch (Exception e1) { System.out.println(e1); } 

But this does not work ... Errors do not occur ... what am I doing wrong? ... Can we receive a message using J2ME?

Code for sending message: (UPDATED)

 MessageConnection conn = (MessageConnection) Connector.open("sms://:50001"); TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); tmsg.setPayloadText(message); tmsg.setAddress("sms://" + number); conn.send(); 

I have send and receive functions in two different forms. What I did was install and run the application on two different mobile phones, send a message from one mobile to another and receive in another.

The message is sent and received successfully, but not in the application. The message is sent to the inbox of another mobile device.

What can I do?

+4
source share
3 answers

try port number 5000.

some phone has this port as an sms listener

+1
source

Try replacing tmsg.setAddress("sms://" + number); at tmsg.setAddress("sms://" + number + ":50001"); .

0
source

The best you can do is to start the stream at the time you receive the message, and make sure your ports are open before listening to the message. Then inside the thread, just conn.receive(); method conn.receive(); to read the message and do whatever you want to do with it.

0
source

Source: https://habr.com/ru/post/1347608/


All Articles