Java 如何通过蓝牙将rms(j2me)中的记录传输到j2se

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

下面是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,如果有,请帮助我。。。。请尽快回复

按照您在此处的写入和读取方式,只有长度不超过255个字符的字符串才能正确写入,这些字符串在您的默认编码中仅占用相同的字节数

在写作方面:

  • 语句
    byte size=(byte)s.length()转换字符串的长度(以字节为单位),因此只取长度的较低8位。因此,只有长度达到255的才是正确的
  • 然后使用
    s.getBytes()
    将字符串转换为字节数组-此数组可能比原始字符串的长度(以字节为单位)更长(以字符为单位)。此转换使用发送设备的默认编码
  • 在阅读方面:

  • 语句
    int size=is.read()读取之前写入的长度,然后创建字节数组
  • is.read(b,0,size)将一些字节读入该数组-它不一定会填充整个数组
  • 然后,使用接收设备的默认编码,将字节数组(甚至可能没有完全填充)转换为字符串
  • 因此,我们有:

  • 所有长度超过255个字符的字符串都写错了
  • 若发送端和接收端使用不同的编码,则可能会得到错误的输出
  • 如果发送端使用UTF-8之类的编码,其中某些字符占用多个字节,则字符串将在结尾处被截断(如果出现此类字符)
  • 如何解决这个问题:

    • 如果您可以在两侧使用DataInputStream和DataOutputStream(我对J2ME一无所知),请在那里使用它们,以及它们的
      readUTF
      writeUTF
      方法。它们解决了您的所有问题(如果您的字符串在这里使用的修改UTF-8编码中最多占用65535字节)
    • 如果没有:
      • 决定字符串的长度,并使用正确的字节数对长度进行编码。每个Java字符串4个字节就足够了
      • 在转换为字节[]后测量长度,而不是之前
      • 使用循环读取数组,确保捕获整个字符串
      • 对于
        getBytes()
        新字符串(…)
        ,请使用具有显式编码名称的变体,并为其提供相同的编码(我建议
        “UTF-8”