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

时间:2014-09-21 10:59:25

标签: java android bluetooth connection

我跟进this问题,但所有提及解决方案对我都不起作用。

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

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

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

//输出流代码(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);
            }

但我无法尽快发送数据mmOutStream.write(buffer);被称为它会产生以下错误。

//错误日志(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

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

1 个答案:

答案 0 :(得分:3)

在您的日志中,当您的Connected线程正在运行时,ConnectThread似乎再次运行。 请参阅日志:

    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()

您的单线程ConnectThread似乎被多次调用。 尝试检查您的代码,使线程只运行一次。 希望能有效。