在android中通过蓝牙发送文件时出错?

Jav*_*ins 5 java connection android bluetooth

我跟进了这个问题,但所有提到的解决方案对我都不起作用.

我正在制作类似于彩虹应用程序的应用程序.此应用程序将安装在必须将所有联系人发送到其他设备的设备中.该应用程序仅安装在一个设备中.我可以通过这段代码连接到远程设备

// BluetoothConnector(完整代码)

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);
Run Code Online (Sandbox Code Playgroud)

在完成配对请求并完成连接后,我尝试通过这段代码通过outputstream将数据发送到其他设备.

//输出流代码(完整代码)

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

但我无法尽快发送mmOutStream.write(缓冲区)数据; 被称为它给出以下错误.

//错误日志(完整日志)

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
Run Code Online (Sandbox Code Playgroud)

请告诉我在上面的代码中我做错了什么.是否可以通过蓝牙传输文件而无需实现服务器端代码?

twi*_*oid 3

从您的日志中可以看出,当您的已连接线程运行时,ConnectThread 再次运行。\n请参阅日志:

\n\n
    09-21 16:21:47.329    6262-6822/com.example.gauravdubey.myapplication I/BT_app\xef\xb9\x95 BEGIN mConnectedThread\n    09-21 16:21:47.329    6262-6822/com.example.gauravdubey.myapplication I/BT_app\xef\xb9\x95 write\n    09-21 16:21:47.329    6262-6262/com.example.gauravdubey.myapplication D/BT_app\xef\xb9\x95 msg write :[B@425c9958\n    09-21 16:21:47.329    6262-6763/com.example.gauravdubey.myapplication D/BT_app\xef\xb9\x95 setState() 2 -> 3\n\n    after a while\n    09-21 16:21:47.359    6262-6262/com.example.gauravdubey.myapplication D/BT_app\xef\xb9\x95 ConnectThread\n    09-21 16:21:47.359    6262-6262/com.example.gauravdubey.myapplication D/BT_app\xef\xb9\x95 setState() 0 -> 2\n    09-21 16:21:47.359    6262-6262/com.example.gauravdubey.myapplication D/BT_app\xef\xb9\x95 state is :null\n    09-21 16:21:47.359    6262-6823/com.example.gauravdubey.myapplication D/\n\nBT_app\xef\xb9\x95 ConnectThread---->run()\n
Run Code Online (Sandbox Code Playgroud)\n\n

您的单线程 ConnectThread 似乎被调用了多次。\n请尝试检查您的代码,使线程仅运行一次。\n希望这会起作用。

\n