how to transfer the records in rms(j2me) to j2se through bluetooth
NOW here is the coding for j2me mobile for sending the string:
String s="hai";
try{
String url = "btspp://001F81000250:1;authenticate=false;encrypt=false;master=false";
StreamConnection stream = null;
InputStream in;
OutputStream out;
stream = (StreamConnection) Connector.open(url);
out=stream.openOutputStream();
String s=tf.getString();
byte size=(byte) s.length();
out.write(size);
out.write(s.getBytes());
out.flush();
out.close();
stream.close();
}
catch(Exception e){
}
NOW the coding for j2se for receiving the String :
StreamConnectionNotifier notifier=null;
try{
String url = "btspp://localhost:"+new UUID("1101", true).toString()+";name=PCServerCOMM;authenticate=false";
System.out.println(LocalDevice.getLocalDevice().开发者_如何学JAVAgetBluetoothAddress()+"\nCreate server by uri: " + url);
notifier= (StreamConnectionNotifier) Connector.open(url);
while(true){
System.out.println("waiting....");
StreamConnection con = notifier.acceptAndOpen();
System.out.println("Got connection..");
InputStream is=con.openInputStream();
//byte b[]=new byte[40];
/*
while(is.available()>0){
System.out.print((char)is.read());
}*/
//is.read(b, 0, 40);
int size=is.read();
byte b[]=new byte[size];
is.read(b, 0, size);
File f=new File("d://test.xml");
FileOutputStream fo=new FileOutputStream(f);
fo.write(b,0,b.length);
fo.close();
con.close();
System.out.println(new String (b));
}
//printing(f);
} catch(Exception e){
JOptionPane.showConfirmDialog(new JFrame(), e.getMessage());
}
I tried this coding for data transfer but it is not a successful one because when the string which we sent is too long then there is problem in receiving side. How can I solve this?
Is there any other way to transfer the data in rms to j2se, if so please help me.... please make your reply quick...
The way you are writing and reading here, only strings up to 255 characters in length, which additionally only take the same number of bytes in your default encoding, are written right.
On the writing side:
- The statement
byte size=(byte) s.length();
converts the length of the string in a byte, thus only takes the lower 8 bits of the length. So, only lengths up to 255 are written right. - Then you are converting the String to a byte array with
s.getBytes()
- this array could be longer (in bytes) than the original string in characters. This conversion uses the default encoding of your sending device.
On the reading side:
- The statement
int size=is.read();
reads the length written before, then you are creating a byte array. is.read(b, 0, size);
reads some bytes into this array - it does not necessarily fills the complete array.- Then you are converting your byte array (which may not even be filled completely) to a string, using the default encoding of the receiving device.
So, we have:
- All strings longer than 255 characters are written wrongly.
- If sending and receiving side are using different encodings, you may get a wrong output.
- If the sending side uses an encoding like UTF-8 where some characters take more than one byte, the string is cut off at the end (if such characters occur).
How to solve this:
- If you can use a DataInputStream and DataOutputStream on both sides (I don't know anything about J2ME), use them there, with their
readUTF
andwriteUTF
methods. They solve all your problems (if your strings take at most 65535 bytes in the modified UTF-8 encoding used here). - If not:
- make a decision on how long the strings can be, and encode your length with the right number of bytes. 4 bytes are enough for every Java String.
- measure the length after converting to a byte[], not before.
- use a loop for reading into the array, to be sure to capture the whole string.
- for the
getBytes()
andnew String(...)
, use the variants which take an explicit encoding name and give them the same encoding (I recommend"UTF-8"
).
精彩评论