Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while sending files through bluetooth in android?

I followed up this question but all mention solution didn't work for me.

I am making an application similar to rainbow application. This application will be installed in the device which has to send all contacts to other device.The application is to be installed only in one device.I am able to connect to the remote device through this piece of code

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

after pairing request is made and connection is done,I try to send data by outputstream to other device through this piece of code.

//Output-stream Code (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);
            }

but I am not able to send data as soon as mmOutStream.write(buffer); is called it gives following error.

// Error 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 :[B@4265cd70
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

Kindly tell me what I am doing wrong in above code. Is it possible to transfer files over Bluetooth without implementing server side code?

like image 269
Java_begins Avatar asked Oct 21 '22 01:10

Java_begins


1 Answers

From your Logs, it appears that ConnectThread runs again while your Connected thread is running. Refer to logs :

    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 :[B@425c9958
    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()

It appears your single thread ConnectThread is getting invoked multiple times. Try checking your code such that thread runs only for one time. Hope that will work.

like image 174
twister_void Avatar answered Oct 29 '22 00:10

twister_void