java.lang.IOException:管道坏了

时间:2017-01-29 10:45:28

标签: java android android-bluetooth

我正在开发一个蓝牙文件传输应用程序。在这个应用程序中,我想通过蓝牙和传输文件连接两个Android设备。但是,我能够显示所有最近的设备,配对设备并能够与它们连接但是当我传输图像时,bluetoothSocket.write()会抛出管道损坏的异常。我已经搜索了解决方案,并参考了stackoverflow的所有答案,之后我理解的是,当接收器蓝牙服务器套接字关闭时,会引发此异常,但我无法解决此问题。以下是我连接设备和传输图像的代码。提前致谢

package com.example.jayesh.bluetoothusagedemo.ConnectAsyncTask;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import com.example.jayesh.bluetoothusagedemo.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import java.util.UUID;

/**
 * Created by Jayesh on 26-01-2017.
 */

public class ConnectDevice extends AsyncTask<BluetoothDevice, Void, String> {
    BluetoothSocket bluetoothSocket;
    Context context;
    String uuid;
    OutputStream outputStream;
    byte[] bytes;

    public ConnectDevice(Context context,String uuid)
    {
        this.context = context;
        this.uuid = uuid;
    }

    @Override
    protected String doInBackground(BluetoothDevice... bluetoothDevices) {

        try {
            bluetoothSocket = bluetoothDevices[0].createRfcommSocketToServiceRecord(UUID.fromString(uuid));

            if (bluetoothSocket != null) {
                bluetoothSocket.connect();
            }
        } catch (SocketTimeoutException e) {
            //e.printStackTrace();
            Log.d("socket=======","socket timeout");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        if(bluetoothSocket.isConnected()){
            convertImageToByteArray();

            try {
                //Log.d("bytessize========", String.valueOf(bytes.length));
                for(Byte b:bytes){
                    if(bluetoothSocket.isConnected()){
                        outputStream.write(b);
                        //Thread.sleep(500);
                    }
                    else{
                        Log.d("closed=============","true");
                        break;
                    }
                    //outputStream.flush();
                }
                //outputStream.write(bytes);
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } //catch (InterruptedException e) {
                //e.printStackTrace();
            //}
        }

        return "completed...";
    }

    @Override
    protected void onPostExecute(String s) {
        //super.onPostExecute(s);
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();

    }

    void convertImageToByteArray(){
        try {
            outputStream = bluetoothSocket.getOutputStream();
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.pic1);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
            bytes = stream.toByteArray();
            Log.d("bytessize========", String.valueOf(bytes.length));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案