Blackberry - send sms timeout - blackberry

I have simple code to send sms. It works fine. Just little problem. How can I figure out that sms can not be send? Some timeout for Connection or other way? Let's say if there is no network, no sim card or no credit. Thanks
Here is code:
public static void sendSMS(String content, String number) {
MessageConnection mc = null;
TextMessage msg;
try {
mc = (MessageConnection) Connector.open("sms://" + number,Connector.WRITE,true);
msg = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(content);
mc.send(msg);
} catch (final Exception e) {
e.printStackTrace();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(e.getMessage());
}
});
} finally {
try {
if (mc != null) {
mc.close();
}
} catch (IOException e) {
}
}
}

private void sendSMS(final String no, final String msg) {
try {
new Thread() {
public void run() {
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
DatagramConnection dc = null;
try {
dc = (DatagramConnection) Connector.open("sms://" + no);
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
dc.send(dg);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
System.out.println("Message Sent Successfully : Datagram");
Dialog.alert("Message Sent Successfully");
} catch (Exception e) {
System.out.println("Exception **1 : " + e.toString());
e.printStackTrace();
}
}
});
} catch (Exception e) {
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
} finally {
try {
dc.close();
dc = null;
} catch (IOException e) {
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
} else {
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector.open("sms://" + no);
//generate a new text message
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set the message text and the address
tmsg.setAddress("sms://" + no);
tmsg.setPayloadText(msg);
//finally send our message
conn.send(tmsg);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
System.out.println("Message Sent Successfully : TextMessage");
Dialog.alert("Message Sent Successfully : TextMessage");
} catch (Exception e) {
System.out.println("Exception **1 : " + e.toString());
e.printStackTrace();
}
}
});
} catch (Exception e) {
System.out.println("Exception 3 : " + e.toString());
e.printStackTrace();
} finally {
try {
conn.close();
conn = null;
} catch (IOException e) {
System.out.println("Exception 4 : " + e.toString());
e.printStackTrace();
}
}
}
}
}.start();
} catch (Exception e) {
System.out.println("Exception 5 : " + e.toString());
e.printStackTrace();
}

Related

when use mina-sshd to execute command ,timeout occurred,how can i deal with it

Here follows is my code,how can i deal with it
public ConnectResponse runCommand(UserInfo userInfo, String command, long timeout) {
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
ChannelExec execChannel = null;
try (ClientSession session = sshClient
.connect(userInfo.getUsername(), userInfo.getTargetIp(), 22)
.verify(CLIENT_VERIFY_TIMEOUT)
.getClientSession();) {
session.addPasswordIdentity(userInfo.getAuthentication());
session.auth().verify(SESSION_VERIFY_TIMEOUT);
execChannel = session.createExecChannel(command);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
execChannel.setOut(out);
execChannel.setErr(err);
execChannel.open();
Set<ClientChannelEvent> events = execChannel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(timeout));
session.close(false);
if (events.contains(ClientChannelEvent.TIMEOUT)) {
throw new BusinessException(500, String.format("执行命令 {%s} 超时了!", command));
}
return new ConnectResponse(out.toString(), err.toString(), execChannel.getExitStatus());
} catch (Exception e) {
throw new BusinessException(e.getCause(), 500, String.format("执行命令 {%s} 出现了一行了!", command));
} finally {
if (!Objects.isNull(execChannel)) {
try {
execChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

How to get a BareJid in smack 4.2.0

I used Roster to create Roster Entry by Roster's method createEntry(BareJid user, String name, String[] groups),but I don't know how to get a BareJid. Anybody could help me?Here are my code,my userJid is a String:
Roster roster = XmppConnectionManager.getInstance().getRoster();
if (roster != null) {
try {
// String[] jids = userJid.split("#");
roster.createEntry(userJid, nickname, null);
} catch (SmackException.NotLoggedInException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Log.w(TAG,"roster is null");
}
I just found it by Google, there is a JID helper class JidCreate:
JidCreate.bareFrom(userJid)

Print in Android using a printer with IP address and Port number

My query is I have a printer connected to the Wifi and I am able to connect to that printer using the IP address of the printer but I am not able to print from that printer.
MY code:-
class MakeConnection extends AsyncTask<String, Void, String>{
String check = "NO";
#Override
protected String doInBackground(String... params) {
Socket sock = null;
PrintWriter oStream = null;
try
{
sock = new Socket(ipAddress, 9100);
oStream = new PrintWriter(sock.getOutputStream());
oStream.write("HI,test from Android Device");
check = "yes";
oStream.flush();
}
catch (IOException e)
{
e.printStackTrace();
Logger.LogError(TAG, e.toString());
AppToast.showShortToast(mContext, e.toString());
}
finally {
oStream.close();
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String s) {
Logger.LogError(TAG, check);
super.onPostExecute(s);
}
}
where I am wrong?

Clear packets sent/ received

Is it possible to clear the amount packets sent/ received and start from 0 again??
The Code to get sent or received packets:
long no_of_packet_Sent = RadioInfo.getNumberOfPacketsSent();
long no_of_packet_Received = RadioInfo.getNumberOfPacketsReceived();
I never found the answer to this but another option is to write the data to a text file then minus the data in the text file from the "get number of packets" .
private static String fileFormatString(String filename) {
return filename.replace(" ".charAt(0),"_".charAt(0));
}
public static String readTextFile(String fName) {
fName = fileFormatString(fName);
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
} catch (IOException e) {
System.out.println("Error on read: "+fName+" - " + e.getMessage());
} finally {
try {
if (null != is) is.close();
if (null != fconn) fconn.close();
} catch (IOException e) {
System.out.println("Error on read IO: "+fName+" - " + e.getMessage());
}
}
return result;
}
public static void writeTextFile(String fName, String text) {
fName = fileFormatString(fName);
DataOutputStream os = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
if (fconn.exists());
if (!fconn.exists()) fconn.create();
os = fconn.openDataOutputStream();
os.write(text.getBytes());
} catch (IOException e) {
System.out.println("Error on write: "+fName+" - " + e.getMessage());
} finally {
try {
if (null != os) os.close();
if (null != fconn) fconn.close();
} catch (IOException e) {
System.out.println("Error on write IO: "+fName+" - " + e.getMessage());
}
}
}
long no_of_packet = RadioInfo.getNumberOfPacketsSent()+RadioInfo.getNumberOfPacketsReceived();
DTHelper.writeTextFile(text_file_name,""+no_of_packet );
String readnumberofkbytes=readTextFile(text_file_name);
long Longreadnumberofbytes = Long.parseLong(readnumberofbytes);
long CurrentNumberofDataUsed= no_of_packet -Longreadnumberofbytes;

UnZip/Extract Zip file in blackberry

I have a zip file containing a folder and inside the folder I have some image file.I wish to extract these images.However I have not been able to find anything.I have been looking at zipMe but have not been able to find any relevant help.
Below is the code that I have developed so far.
ZipEntry dataZE;
InputStream isData;
StringBuffer sbData;
ZipInputStream dataZIS;
String src = "file:///store/home/user/images.zip";
String path = "file:///store/home/";
String fileName = "";
FileConnection f_Conn;
public UnZipper() {
debugger("Unzipper constructor");
try {
f_Conn = (FileConnection) Connector.open(src);
} catch (IOException e) {
debugger("f_conn error :" + e.getMessage());
}
try {
isData = f_Conn.openInputStream();
} catch (IOException e) {
debugger("f_conn error getting ip_stream:" + e.getMessage());
}
sbData = new StringBuffer();
dataZIS = new ZipInputStream(isData);
debugger("got all thing initialized");
}
public void run() {
debugger("unzipper run");
try {
startUnziping();
} catch (IOException e) {
debugger("Error unzipping " + e.getMessage());
}
debugger("finished...");
}
private void startUnziping() throws IOException {
debugger("startUnziping");
dataZE = dataZIS.getNextEntry();
fileName = dataZE.getName();
writeFile();
dataZIS.closeEntry();
debugger(">>>>>>>>>>> : " + fileName);
}
private void readFile() throws IOException {
debugger("readFile");
int ch;
int i = 0;
while ((ch = dataZIS.read()) != -1) {
debugger((i++) + " : " + sbData.toString()
+ " >>> writting this..");
sbData.append(ch);
}
}
private void writeFile() {
debugger("writting file...");
FileConnection f_Conn = null;
byte[] file_bytes = new byte[sbData.length()];
file_bytes = sbData.toString().getBytes();
try {
readFile();
} catch (IOException e) {
debugger("Error while reading " + e.getMessage());
}
try {
f_Conn = (FileConnection) Connector.open(path + fileName);
} catch (IOException e) {
debugger("getting f_conn" + e.getMessage());
}
if (!f_Conn.exists()) {
// create the file first
debugger("I know file does not exists");
try {
f_Conn.mkdir();
} catch (IOException e) {
debugger("Oops!!! error creating fle : " + e.getMessage());
}
}
try {
f_Conn.setWritable(true);
debugger("file is nt writeable");
} catch (IOException e) {
debugger("cannot make it writeable : " + e.getMessage());
}
OutputStream lo_OS = null;
try {
lo_OS = f_Conn.openOutputStream();
debugger("got out Stream hero!!!");
} catch (IOException e) {
debugger("cant get out Stream !!!");
}
try {
lo_OS.write(file_bytes);
debugger("yess...writtent everything");
} catch (IOException e) {
add(new LabelField("Error writing file ..." + e.getMessage()));
}
try {
lo_OS.close();
debugger("now closing connection...");
} catch (IOException e) {
debugger("error closing out stream : " + e.getMessage());
}
}
}
I have been able to get ZipEntry representing folder that contains images however I have not been able to figure out how i must extract those images.
Thanks for help.
Iterate over all ZipEntry in zip file in you startUnzipping (you're working only with first one in your code). The item corresponded to child file should have name like "foldername/filename".

Resources