данные не отправляются через Bluetooth-сокет (Android)

Я пытаюсь отправить файл с помощью Bluetooth-сокетов Android. Я получаю запросы на сопряжение, но, к сожалению, данные не передаются на стороне сервера, а сторона клиента выглядит следующим образом:

Сервер: (поскольку мой сервер приложений получает данные)

public void run() {
super.run();

    BluetoothAdapter mBtadapter = BluetoothAdapter.getDefaultAdapter();
    Log.i("bluetooth Adapter", "got adapter "+ mBtadapter.getAddress());

    try {
        Ssocket = mBtadapter.listenUsingRfcommWithServiceRecord("CardXchange", uuid);


        Log.i("Socket", "Socket Acquired "+ Ssocket.toString());
    } catch (IOException e) {

        e.printStackTrace();
    } 



    while(true){

        if(read_flag()==1){

            try {
                Toast.makeText(c, "flag is eventually 1", Toast.LENGTH_LONG).show();
                Ssocket.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            break;

        }





        try {
            client_socket = Ssocket.accept();
            if(client_socket!=null){

//this function is used to handle the connection when sockets get connected
            manageConnection1(client_socket);

                Ssocket.close();

                break;
            }
        } catch (IOException e) {

            e.printStackTrace();
        }



    }

    Log.i("breaked ", "oh god!! out of the loop ");
}


//implementation of the function
private void manageConnection1(BluetoothSocket client_socket2) {
    // TODO Auto-generated method stub
    int bytes;

    Log.i("serevr chya manage connection madhe gela", "in servers manage connection ");

    File file =  new File("/sdcard/myfolder/received.Xcard");

    try {
        FileOutputStream fos = new FileOutputStream(file);
        try {
            InputStream is = client_socket2.getInputStream();



            while((bytes = is.read(buffer, 0, 1024))>0){

                fos.write(buffer, 0, 1024);

            }


            is.close();
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    try {
        client_socket2.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

и клиентская сторона (отправляющая сторона выглядит так):

public void run(){

                try {

                    Method m = send_dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                    Csocket = (BluetoothSocket) m.invoke(send_dev, 1);
                    Csocket.connect();
                    manageSendConnection(Csocket);

                }catch(Exception e){

                    e.printStackTrace();
                }


            }

   //function to handle after connection is established.

            private void manageSendConnection(BluetoothSocket csocket) {
                // TODO Auto-generated method stub


                File f = new File("/sdcard/myfolder/card3.Xcard");
                byte[] buffer = new byte[(int)f.length()];
                try {
                    FileInputStream fis = new FileInputStream(f);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    try {
                        bis.read(buffer, 0, (int)f.length());

                        OutputStream os = csocket.getOutputStream();
                        os.write(buffer);
                        os.close();
                        fis.close();
                        bis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } 

                try {
                    csocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

Я получаю запрос на сопряжение, но данные не отправляются. может ли кто-нибудь помочь мне найти проблему и дать намек на решение ??


person neerajDorle    schedule 06.06.2013    source источник