Error sending files via Bluetooth in Android?

I reviewed this question, but all mention of the solution did not help me.

I am making an application similar to a rainbow application. This application will be installed on the device, which should send all contacts to another device. The application must be installed in only one device. I can connect to a remote device through this piece of code.

// BluetoothConnector ( Full 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 the connection request is made and the connection is completed, I try to send data on the output stream to another device through this code fragment.

// Code of the output stream ( Full 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 can not send data as soon as mmOutStream.write (buffer); called giving the following error.

// Error log ( Full 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 

Tell me what I am doing wrong in the above code. Is it possible to transfer files via Bluetooth without implementing the code on the server side?

+5
source share
1 answer

From your logs, it looks like ConnectThread starts up again while the connected thread is running. Refer to the 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 seems your single ConnectThread thread is being called multiple times. Try checking your code so that the thread runs only once. Hope this works.

+3
source

Source: https://habr.com/ru/post/1203071/


All Articles