无法通过蓝牙android传输传输图像

时间:2012-02-09 17:00:26

标签: android bluetooth objectoutputstream objectinputstream

我正在开发一个分享联系信息+图片的Android应用程序 我有一个包含整个信息的课程。

import java.io.Serializable;

public class Stuff implements Serializable {

    private String name;
    private String company;
    private String title;
    private String address;
    private String phone;
    private String cel;
    private String email;
    private int template;
    private boolean isPopulate;
    private byte[] picture;

    public Stuff(String n, String c, String t, String a, String ph, String cl,
            String e, int tmp, byte[] pic, boolean check) {
        this.name = n;
        this.company = c;
        this.title = t;
        this.address = a;
        this.phone = ph;
        this.cel = cl;
        this.email = e;
        this.template = tmp;
        this.isPopulate = check;
        this.picture = pic;
    }

    public String getName() {
        return this.name;
    }

    public String getCompany() {
        return this.company;
    }

    public String getTitle() {
        return this.title;
    }

    public String getAddress() {
        return this.address;
    }

    public String getPhone() {
        return this.phone;
    }

    public String getCel() {
        return this.cel;
    }

    public String getEmail() {
        return this.email;
    }

    public int getTemplate() {
        return this.template;
    }

    public byte[] getPicture() {
        return this.picture;
    }

    public boolean getCheck() {
        return this.isPopulate;
    }
}

这是我通过套接字传输图像的线程

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream = null;
    private final OutputStream mmOutStream = null;
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;
        Log.d("connected thread ", "connected thread constructor inside");
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");

        // Keep listening to the InputStream while connected

        try {
            Log.d("Connected thread run ", "start while");

            switch (ROLE) {

            case 1:
                // read from client
                Log.d("Connected thread 1 ", "ois before");
                ObjectInputStream tmpIn1 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 1  ", "ois after");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn1.readObject();
                    Log.d("Connected thread 1  ", "after read");
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);

                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // write to client

                ObjectOutputStream tmpOut1 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut1.writeObject(person);
                tmpOut1.flush();
                Log.d("Connected thread 1  ", "oos created");
                tmpIn1.close();
                tmpOut1.close();

                break;
            case 2:
                // write to server

                ObjectOutputStream tmpOut2 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut2.writeObject(person);
                tmpOut2.flush();
                Log.d("Connected thread 2  ", "oos created");
                // read from server
                ObjectInputStream tmpIn2 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 2  ", "ois created");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn2.readObject();
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);
                    Log.d("Connected thread 2  ", "send message");
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                tmpOut2.close();
                tmpIn2.close();

                break;
            }
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(Stuff object) {
        try {
            Log.d("BTS", "inside write before" + object.getName());
            oos.writeObject(object);
            Log.d("BTS", "inside write after" + object.getName());
            oos.flush();
            oos.close();

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

现在图像不会在第二个设备上传输,从而导致问题,甚至数据都没有。问题出在哪里。

0 个答案:

没有答案