2012-03-17 48 views
8

Tôi đang gặp sự cố khi gửi tệp lớn qua ổ cắm bluetooth. Các tệp nhỏ hơn được chuyển chính xác. Tôi tin rằng tối đa 161280 byte được chuyển một cách chính xác.Truyền tệp Bluetooth Android

EDIT: Tôi đã thực hiện thêm một số thử nghiệm và thu hẹp nguyên nhân. Có vẻ như

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

trong phần gửi mã KHÔNG được viết quá 161280 byte. Tôi thấy hành vi này bằng cách không đóng kết nối ổ cắm, do đó gây ra read trong phần tiếp nhận để "chặn" trên 161280 byte. Điều gì là sai với dòng đầu ra bluetooth ở đây? Tôi đang làm gì sai?

CHỈNH SỬA 2: Thực hiện việc này cho phép.

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

đang gử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(); 

đang nhận:

// 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) 

Tôi đang sử dụng Motorola Milestone. Android 2.1

+0

Dán nhật ký của thiết bị gửi bao gồm dấu thời gian. Hy vọng gửi và nhận đang được thực hiện trên một chủ đề riêng biệt. –

+0

Tìm kiếm SO cho "Phần mềm gây ra ngắt kết nối". –

+0

Có chúng nằm trên các chủ đề riêng biệt. Điện thoại khác nhau. Thiết bị gửi sẽ gửi dữ liệu gần như ngay lập tức. – shiraz

Trả lời

2

Tôi đã có thể giải quyết vấn đề này bằng cách gửi các khối dữ liệu nhỏ ra ngoài luồng bluetooth. Nó bật ra rằng 8 * 1024 là một kích thước bộ đệm tốt, giúp trong việc gửi dữ liệu liền mạch trên dòng cũng như ngăn chặn tham nhũng của dữ liệu ở cuối nhận.

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

Tôi gặp sự cố khi gửi tệp hình ảnh đơn giản qua bluetooth sử dụng mẫu trò chuyện qua bluetooth. Trên thực tế, yêu cầu của tôi là gửi bất kỳ tập tin bluetooth nào. Đối với điều này làm thế nào tôi có thể thiết lập kích thước bộ đệm, làm thế nào để nhận được đoạn nhỏ dữ liệu và kết hợp nó ở phía người nhận. Bạn có thể chia sẻ mã của tôi không. –

+0

bạn có thể xem http://stackoverflow.com/questions/16954082/send-text-file-using-bluetooth-sockets-in-android – neerajDorle

+0

Tôi đang phát triển một giải pháp dựa trên phương pháp tiếp cận của bạn, nhưng Tôi bị kẹt trên outStream.write (mybytearray, 0, mybytearray.length) ;, nó đơn giản sẽ không ghi dữ liệu vào luồng đầu ra.Không có lỗi, tệp được tạo với kích thước 0 byte và nhật ký dừng ngay sau bis.read (mybytearray, 0, mybytearray.length). Bất kỳ ý tưởng? –

Các vấn đề liên quan