Java 如何通过蓝牙将rms(j2me)中的记录传输到j2se
下面是j2me mobile发送字符串的编码:Java 如何通过蓝牙将rms(j2me)中的记录传输到j2se,java,java-me,bluetooth,rms,Java,Java Me,Bluetooth,Rms,下面是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) Connec
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,如果有,请帮助我。。。。请尽快回复 按照您在此处的写入和读取方式,只有长度不超过255个字符的字符串才能正确写入,这些字符串在您的默认编码中仅占用相同的字节数 在写作方面:
byte size=(byte)s.length()代码>转换字符串的长度(以字节为单位),因此只取长度的较低8位。因此,只有长度达到255的才是正确的
s.getBytes()
将字符串转换为字节数组-此数组可能比原始字符串的长度(以字节为单位)更长(以字符为单位)。此转换使用发送设备的默认编码int size=is.read()代码>读取之前写入的长度,然后创建字节数组
is.read(b,0,size)代码>将一些字节读入该数组-它不一定会填充整个数组
- 如果您可以在两侧使用DataInputStream和DataOutputStream(我对J2ME一无所知),请在那里使用它们,以及它们的
和readUTF
方法。它们解决了您的所有问题(如果您的字符串在这里使用的修改UTF-8编码中最多占用65535字节)writeUTF
- 如果没有:
- 决定字符串的长度,并使用正确的字节数对长度进行编码。每个Java字符串4个字节就足够了
- 在转换为字节[]后测量长度,而不是之前
- 使用循环读取数组,确保捕获整个字符串
- 对于
和getBytes()
,请使用具有显式编码名称的变体,并为其提供相同的编码(我建议新字符串(…)
)“UTF-8”