【问题标题】:how to transfer the records in rms(j2me) to j2se through bluetooth如何通过蓝牙将rms(j2me)中的记录传输到j2se
【发布时间】:2011-03-05 15:00:17
【问题描述】:

现在这里是 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,如果有,请帮助我....请尽快回复...

【问题讨论】:

    标签: java java-me bluetooth rms


    【解决方案1】:

    您在此处写入和读取的方式,只有长度不超过 255 个字符的字符串被正确写入,而且在默认编码中也只占用相同数量的字节。

    在写作方面:

    1. 语句byte size=(byte) s.length();将字符串的长度转换为一个字节,因此只取长度的低8位。因此,只有不超过 255 的长度是正确的。
    2. 然后您将字符串转换为带有s.getBytes() 的字节数组 - 该数组可能比原始字符串长(以字节为单位)。此转换使用您发送设备的默认编码。

    在阅读方面:

    1. 语句int size=is.read();读取之前写入的长度,然后你正在创建一个字节数组。
    2. is.read(b, 0, size); 将一些字节读入此数组 - 它不一定填满整个数组。
    3. 然后您将使用接收设备的默认编码将字节数组(甚至可能没有完全填充)转换为字符串。

    所以,我们有:

    1. 所有超过 255 个字符的字符串都写错了。
    2. 如果发送端和接收端使用不同的编码,可能会得到错误的输出。
    3. 如果发送方使用 UTF-8 之类的编码,其中某些字符占用超过一个字节,则字符串在末尾被截断(如果出现此类字符)。

    如何解决:

    • 如果您可以在两侧使用 DataInputStream 和 DataOutputStream(我对 J2ME 一无所知),请使用它们的 readUTFwriteUTF 方法。它们解决了您的所有问题(如果您的字符串在此处使用的修改后的 UTF-8 编码中最多占用 65535 个字节)。
    • 如果不是:
      • 决定字符串的长度,并使用正确的字节数对长度进行编码。每个 Java 字符串 4 个字节就足够了。
      • 在转换为 byte[] 之后测量长度,而不是之前。
      • 使用循环读取数组,确保捕获整个字符串。
      • 对于getBytes()new String(...),使用具有显式编码名称并赋予它们相同编码的变体(我推荐"UTF-8")。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多