2012-03-17 58 views
8

ブルートゥースソケットを介して大きなファイルを送信する際に問題が発生しています。小さなファイルが正しく転送されます。私は最大161280バイトが正しく転送されると信じています。Bluetoothファイル転送Android

EDIT:私はもう少しテストを行い、原因を絞りました。そのように見えるのは、送信コード部分に161280バイトを超えて書かれていないと思われる

outStream.write(mybytearray, 0, mybytearray.length); 

です。私はソケット接続を閉じないことでこの動作を見たので、受信側のreadが161280バイトで "ブロック"する原因となりました。ここでBluetoothの出力ストリームに何が間違っていますか?私は間違って何をしていますか?

編集2: これを実行すると、

for(int i = 0 ; i < mybytearray.length ; i++){ 
    outStream.write(mybytearray[i]); 
} 

送信コード:

try { 
     outStream = mBluetoothSocket.getOutputStream(); 
     Log.d(TAG,"outStream created success!"); 
    } catch (IOException e) { 
     Log.d(TAG, 
       "ON RESUME: Output stream creation failed.", 
       e); 
    } 

    File myFile = new File(file_name); 
    Log.d(TAG,"file /source.pdf created success!"); 

    byte[] mybytearray = new byte[(int)myFile.length()]; 
    Log.d(TAG,"file length() =" + (int)myFile.length()); 

    FileInputStream fis = new FileInputStream(myFile); 
    Log.d(TAG,"fis created"); 

    BufferedInputStream bis = new BufferedInputStream(fis,1272254); 
    Log.d(TAG,"bis created success"); 

    bis.read(mybytearray,0,mybytearray.length); 
    Log.d(TAG,"ALL Bytes read from bis"); 

    outStream.write(mybytearray, 0, mybytearray.length); 
    Log.d(TAG,"BYTES WRITTEN to OUTSTREAM of socket"); 


    outStream.flush(); 
    Log.d(TAG,"bytes flushed"); 
    outStream.close(); 

受信コード:

// Attach the i/p stream to the socket 
    try { 
     InputStream in = socket.getInputStream(); 
     mIn = in; 
     Log.d(TAG, "input stream acquired"); 

    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
    // Create output streams & write to file 
    FileOutputStream fos = new FileOutputStream(
      Environment.getExternalStorageDirectory() 
        + "/copy.pdf"); 
    try { 
     bytesRead = mIn.read(buffer, 0, buffer.length); 
     Log.d(TAG, "bytesRead first time =" + bytesRead); 
     current = bytesRead; 

     do { 
      Log.d(TAG, "do-while -- current: " + current); 
      bytesRead = mIn.read(buffer, current, 
        buffer.length - current); 
      Log.d(TAG, "bytesRead: =" + bytesRead); 

      if (bytesRead >= 0) 
       current += bytesRead; 
     } while (bytesRead > -1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.d(TAG, "do while end:-- buffer len= " 
       + buffer.length + " current: " + current); 

     fos.write(buffer); 
     Log.d(TAG, "fos.write success! buffer: " 
       + buffer.length + " current: " + current); 

     fos.flush(); 
     fos.close(); 
    } 
} 
socket.close(); 

Logcat:

D/ReceiveService(5761): do-while -- current: 155232 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 156240 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 157248 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 158256 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 159264 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 160272 
D/ReceiveService(5761): bytesRead: =1008 
D/ReceiveService(5761): do-while -- current: 161280 
W/System.err(5761): java.io.IOException: Software caused connection abort 
W/System.err(5761): at android.bluetooth.BluetoothSocket.readNative(Native Method) 
W/System.err(5761): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:307) 
W/System.err(5761): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 
W/System.err(5761): at com.bt.server.ReceiveService$AcceptThread.run(ReceiveService.java:141) 

私はMotorola Milestoneを使用しています。 Android 2.1

+0

タイムスタンプを含む送信デバイスのログを貼り付けます。希望の送受信は別のスレッドで行われています。 –

+0

「ソフトウェアによる接続の中止が発生しました。 –

+0

はい、別のスレッドにあります。異なる電話。送信側デバイスは、ほぼ即時にデータを送信します。 – shiraz

答えて

2

私は、Bluetoothのアウトストリームに小さなチャンクを送信することでこの問題を解決できました。 8 * 1024は良好なバッファサイズであり、受信側でデータの破損を防ぐだけでなく、ストリーム上でシームレスにデータを送信するのに役立ちました。

BufferedInputStream bis = new BufferedInputStream(fis, 8 * 1024); 


byte[] buffer = new byte[8192]; 
int len 
while ((len = bis.read(buffer)) != -1) { 
    outStream.write(buffer, 0, len); 
} 
+1

ブルートゥースのチャットサンプルを使用してBluetooth経由で簡単な画像ファイルを送信する際に問題が発生しています。実際には、私の要件は、任意のファイルoevrブルートゥースを送信した。このために、どのようにバッファサイズを設定し、小さなチャンクを受信し、それを受信側で結合するかを設定できます。あなたのコードを私に教えてください。 –

+0

あなたはこれを見ていただけますか?http://stackoverflow.com/questions/16954082/send-text-file-using-bluetooth-sockets-in-android – neerajDorle

+0

私はあなたのアプローチに基づいている解決策を開発していますが、私はoutStream.write(mybytearray、0、mybytearray.length)に固執しています;単に出力ストリームにデータを書き込むことはありません。エラーはありません。サイズが0バイトのファイルが作成され、bis.read(mybytearray、0、mybytearray.length)の直後にログが停止します。何か案は? –

関連する問題