简体   繁体   English

如何使用Java中的RFCOMM通过蓝牙将图像从PC传输到移动设备?

[英]how can i transfer image from PC to Mobile via bluetooth using RFCOMM in java?

I did following coding @ PC side (I tried to sent data in chunks): 我在PC端进行了以下编码(我尝试以块的形式发送数据):

try {
    for (int i = 0; i < sdata.length / 2; i++)
    m_Output.write(sdata[i]);
} catch (IOException ex) {
    Logger.getLogger(SPPServer.class.getName()).log(Level.SEVERE, null, ex);
}
int i = sdata.length / 2;
try {
    m_Output.flush();
} catch (IOException ex) {
    Logger.getLogger(SPPServer.class.getName()).log(Level.SEVERE, null, ex);
}
for (int k = i; k < sdata.length; k++) {
    try {
        m_Output.write(sdata[i]);
        m_Output.flush();
        m_Output.close();
    } catch (IOException ex) {
        Logger.getLogger(SPPServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

While at mobile side I receive it as: 在移动端,我收到以下消息:

public Image recdata() {

    Image image1 = null;
    int i = 0;
    try {
        // input = StrmConn.openInputStream();
        length = input.read();
        data1 = new byte[length];
        length = 0;
        int ch = 1;

        while (length != data1.length) {
            ch = input.read(data1, length, data1.length - length);

            if (ch == -1) {
                throw new IOException("Can't read data");
            }
            length += ch;
        }
        try {
            //  int len = ch;
            input.wait(2000);

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        length = input.read();
        byte[] data11 = new byte[length];
        try {
            this.wait(1700);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        while (length != data11.length) {
            ch = input.read(data11, length, data11.length - length);

            if (ch == -1) {
                throw new IOException("Can't read data");
            }
            length += ch;
        }
        length = data1.length + data11.length;
        data12 = new byte[length];
        for (i = 0; i < data1.length; i++) {
            data12[i] = data[i];
        }
        for (int k = i; k < length; k++)
        data12[k] = data[k];
    }
    catch (IOException e) {
        System.err.println("U must correct");
    }

    if (image1 == null) {
        S = "Imagr is null in recive data";
    }
    return image1;
}

The problem might be here on the mobile side 问题可能在移动端

length = input.read();

You are (probably) assigning a byte to an integer, so when you use the value later, you're using the integer representation of the byte and not the integer value. 您(可能)将一个字节分配给整数,因此以后使用该值时,将使用该字节的整数表示形式,而不是整数值。 Here is a link with code to let you convert the integer to a byte array, and then back from a byte array to an integer. 这是代码链接,可让您将整数转换为字节数组,然后从字节数组转换为整数。 Java integer to byte array Java整数到字节数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM