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

发布于 2024-10-20 14:21:39 字数 1847 浏览 3 评论 0原文

现在这里是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-10-27 14:21:39

按照您在此处写入和读取的方式,只有长度不超过 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”)。

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:

  1. 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.
  2. 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:

  1. The statement int size=is.read(); reads the length written before, then you are creating a byte array.
  2. is.read(b, 0, size); reads some bytes into this array - it does not necessarily fills the complete array.
  3. 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:

  1. All strings longer than 255 characters are written wrongly.
  2. If sending and receiving side are using different encodings, you may get a wrong output.
  3. 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 and writeUTF 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() and new String(...), use the variants which take an explicit encoding name and give them the same encoding (I recommend "UTF-8").
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文