2014-09-21 15 views
5

Tôi theo dõi this câu hỏi nhưng tất cả đề cập đến giải pháp không hiệu quả đối với tôi.Lỗi khi gửi tệp qua bluetooth trong android?

Tôi đang tạo một ứng dụng tương tự như ứng dụng cầu vồng. Ứng dụng này sẽ được cài đặt trong thiết bị mà phải gửi tất cả địa chỉ liên lạc đến thiết bị khác. Ứng dụng này chỉ được cài đặt trong một thiết bị. Tôi có thể kết nối với thiết bị từ xa thông qua đoạn mã này

// BluetoothConnector (Complete Code)

Class<?> clazz = tmp.getRemoteDevice().getClass(); 
       Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE}; 
       Method m = clazz.getMethod("createRfcommSocket", paramTypes); 
       Object[] params = new Object[] {Integer.valueOf(1)}; 
       fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params); 

sau khi ghép nối yêu cầu được thực hiện và kết nối được thực hiện, tôi cố gắng để gửi dữ liệu bằng cách OutputStream vào thiết bị khác thông qua đoạn mã này.

// Output-stream Mã (Complete code)

public void write(byte[] buffer) { 
      try { 
       Log.i(TAG, "write"); 
       mmOutStream.write(buffer); 
      } catch (IOException e) { 
       Log.e(TAG, "Exception during write", e); 
      } 

nhưng tôi không thể để gửi dữ liệu càng sớm càng mmOutStream.write (đệm);. Được gọi là nó mang lại sau lỗi

// Lỗi Log (Complete Log)

09-21 16:21:52.829 6262-6262/com.example.aadi.myapplication D/BT_app﹕ connection_done 
09-21 16:21:52.829 6262-6871/com.example.aadi.myapplication I/BT_app﹕ BEGIN mConnectedThread 
09-21 16:21:52.829 6262-6871/com.example.aadi.myapplication I/BT_app﹕ write 
09-21 16:21:52.829 6262-6262/com.example.aadi.myapplication D/BT_app﹕ msg write :[[email protected] 
09-21 16:22:50.149 6262-6823/com.example.aadi.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback 
09-21 16:22:50.159 6262-6823/com.example.aadi.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[81]} 
09-21 16:22:50.679 6262-6823/com.example.aadi.myapplication W/BT_app﹕ Fallback failed. Cancelling. 
    java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
      at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505) 
      at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482) 
      at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324) 
      at com.example.aadi.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:202) 
      at com.example.aadi.myapplication.BluetoothConnector.connect(BluetoothConnector.java:64) 
      at com.example.aadi.myapplication.BluetoothService$ConnectThread.run(BluetoothService.java:218) 
09-21 16:22:50.679 6262-6823/com.example.aadi.myapplication I/BT_app﹕ Attempting to connect to Protocol: 0000112f-0000-1000-8000-00805f9b34fb 

Vui lòng cho tôi biết những gì tôi đang làm sai trong mã trên. Có thể truyền tệp qua Bluetooth mà không cần triển khai mã phía máy chủ không?

+0

Tôi trả lời cho câu hỏi cuối cùng của bạn, có thể chuyển tệp qua Bluetooth mà không cần phía máy chủ –

+0

@NadirB bất kỳ câu trả lời giải thích nào sẽ hữu ích –

+0

BlootothDevice của bạn có hỗ trợ xác thực không? Tài liệu trong tệp nguồn chỉ ra rằng bạn chỉ có thể sử dụng 'createRfcommSocket' với một deivice từ xa, hỗ trợ tự động xác thực. Trong trường hợp này nó nói bạn nên sử dụng 'createInsecureRfcommSocket'. – Soana

Trả lời

3

Từ nhật ký của bạn, có vẻ như ConnectThread chạy lại trong khi chuỗi Kết nối của bạn đang chạy. Tham khảo nhật ký:

09-21 16:21:47.329 6262-6822/com.example.gauravdubey.myapplication I/BT_app﹕ BEGIN mConnectedThread 
    09-21 16:21:47.329 6262-6822/com.example.gauravdubey.myapplication I/BT_app﹕ write 
    09-21 16:21:47.329 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ msg write :[[email protected] 
    09-21 16:21:47.329 6262-6763/com.example.gauravdubey.myapplication D/BT_app﹕ setState() 2 -> 3 

    after a while 
    09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ ConnectThread 
    09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ setState() 0 -> 2 
    09-21 16:21:47.359 6262-6262/com.example.gauravdubey.myapplication D/BT_app﹕ state is :null 
    09-21 16:21:47.359 6262-6823/com.example.gauravdubey.myapplication D/ 

BT_app﹕ ConnectThread---->run() 

Dường như chuỗi chủ đề ConnectThread của bạn được gọi nhiều lần. Thử kiểm tra mã của bạn để chuỗi chỉ chạy trong một lần. Hy vọng rằng sẽ làm việc.

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