2010-11-13 38 views
0

我下面的編碼@ PC端(我想發送的塊數據):如何使用java中的RFCOMM通過藍牙將圖像從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); 
    } 
} 

雖然在移動端我收到如下:

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; 
} 
+0

問題是......? http://tinyurl.com/so-hints – 2010-11-13 18:34:45

+0

我寫了上面的代碼,但無法接收。我只能接收到5000字節以上的321個字節。你能幫我嗎 – 2010-11-15 16:47:36

回答

0

的問題可能是這裏的移動側

length = input.read(); 

你(可能)指派給整數字節,所以當你以後使用的值,你使用的字節的整數表示,而不是整數VAL UE。這裏是一個代碼鏈接,讓您將整數轉換爲字節數組,然後從字節數組返回到整數。 Java integer to byte array

+0

感謝您的回放,我有閱讀整個數據的問題。我可以讀取多達10個字節的數據。自從最近幾個月以來,我一直在遭受同樣的問題。請寄給我一些更好的解決方案。先生,你太好了 – 2010-12-01 14:20:24

相關問題