如何通过蓝牙将rms(j2me)中的记录传输到j2se
现在这里是 j2me mobile 用于发送字符串的编码:
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){
}
现在是 j2se 用于接收字符串的编码:
StreamConnectionNotifier notifier=null;
try{
String url = "btspp://localhost:"+new UUID("1101", true).toString()+";name=PCServerCOMM;authenticate=false";
System.out.println(LocalDevice.getLocalDevice().getBluetoothAddress()+"\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());
}
我尝试了这种数据传输编码,但它不是成功的,因为当我们发送的字符串太长时,就会有接收方的问题。我该如何解决这个问题?
有没有其他方法可以将rms中的数据传输到j2se,如果是的话请帮助我......请尽快回复......
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().getBluetoothAddress()+"\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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照您在此处写入和读取的方式,只有长度不超过 255 个字符的字符串才会被正确写入,而且这些字符串在默认编码中仅占用相同数量的字节。
在写入方面:
byte size=(byte) s.length();
将字符串的长度转换为字节,因此只取长度的低8位。因此,只有 255 以内的长度才是正确的。在读取方面:
int size=is.read();
读取之前写入的长度,然后您正在创建一个字节数组。is.read(b, 0, size);
将一些字节读入此数组 - 它不一定填充整个数组。所以,我们有:
如何解决这个问题:
readUTF
和writeUTF
方法。它们解决了您的所有问题(如果您的字符串在此处使用的修改后的 UTF-8 编码中最多占用 65535 个字节)。getBytes()
和new String(...)
,使用采用显式编码名称并为它们提供相同编码的变体(我建议<代码>“UTF-8”)。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:
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.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:
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.So, we have:
How to solve this:
readUTF
andwriteUTF
methods. They solve all your problems (if your strings take at most 65535 bytes in the modified UTF-8 encoding used here).getBytes()
andnew String(...)
, use the variants which take an explicit encoding name and give them the same encoding (I recommend"UTF-8"
).