2012-03-17 16 views
8

मुझे ब्लूटूथ सॉकेट पर बड़ी फ़ाइलों को भेजने में समस्या का सामना करना पड़ रहा है। छोटी फाइलें सही ढंग से स्थानांतरित हो जाती हैं। मेरा मानना ​​है कि 161280 बाइट्स सही ढंग से स्थानांतरित हो जाते हैं।ब्लूटूथ फ़ाइल स्थानांतरण एंड्रॉइड

संपादित करें: मैंने कुछ और परीक्षण किया और कारण को कम कर दिया। ऐसा लगता है कि

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

भेजने कोड भाग में 161280 बाइट्स से अधिक नहीं लिख रहा है। मैंने सॉकेट कनेक्शन को बंद न करके इस व्यवहार को देखा, जिससे प्राप्त करने वाले हिस्से में read को 161280 बाइट्स पर "ब्लॉक" करने का कारण बन गया। ब्लूटूथ आउटपुट स्ट्रीम में क्या गलत है? मैं क्या गलत कर रहा हूं?

संपादित 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) 

मैं मोटोरोला मील का पत्थर उपयोग कर रहा हूं। एंड्रॉइड 2.1

+0

टाइमस्टैम्प सहित भेजने वाले डिवाइस के लॉग पेस्ट करें। उम्मीद है कि एक अलग धागे पर भेजने और प्राप्त किया जा रहा है। –

+0

"सॉफ़्टवेयर के कारण कनेक्शन निरस्त" के लिए एसओ खोजें। –

+0

हां वे अलग धागे पर हैं। विभिन्न फोन प्रेषण डिवाइस लगभग तुरंत डेटा भेजता है। – shiraz

उत्तर

2

मैं ब्लूटूथ आउटस्ट्रीम में डेटा के छोटे हिस्से भेजकर इस समस्या को हल करने में सक्षम था। यह पता चला कि 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

मुझे ब्लूटूथ चैट नमूना का उपयोग करके ब्लूटूथ पर सरल छवि फ़ाइल भेजने में कोई समस्या है। असल में, मेरी आवश्यकता किसी भी फाइल ओवर ब्लूटूथ भेजना था। इसके लिए मैं बफर आकार कैसे सेट कर सकता हूं, डेटा के छोटे हिस्से को कैसे प्राप्त कर सकता हूं और इसे रिसीवर पक्ष में जोड़ सकता हूं। क्या आप कृपया मुझे अपना कोड साझा कर सकते हैं। –

+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) के बाद लॉग रोकें। कोई विचार? –

संबंधित मुद्दे