4

Good day all, Based on a question i asked earlier, i have been able to convert files to byte Array in other to use the write method using this:

public void sendFile(){
        Log.d(TAG, "sending data");
        InputStream inputStream = null;

      Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "" +    filesID.get(0));
        Log.d(TAG, "obtained input stream here in recentDevices Activity");
        try {

            inputStream = getContentResolver().openInputStream(uri);
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();


            int buffersize = 1024;
            byte[] buffer = new byte[buffersize];

            int len = 0;
            while((len = inputStream.read(buffer)) != -1){
                byteBuffer.write(buffer, 0, len);
            }

            Log.d(TAG, "sending data to connected thread");
            bluetooth_service.write(byteBuffer.toByteArray());

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

        }

        finally{
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }

and from the BluetoothChatService Example i have this write method in the connected thread:

   public void write(byte[] out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            r = connectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }


/* Call this from the main Activity to send data to the remote device */
        public void write(byte[] bytes) {

            try {
                mmOutStream.write(bytes); // basically stuck here. nothing happens here
            } catch (IOException e) { }

            mHandler.sendEmptyMessage(RecentDevices.TRANSFER_COMPLETED);

        }

Am basically stuck in the write method of the connected Thread, nothing seems to happen there. i put a progress dialog to dismiss it after the handlers sends a TRANSFER_COMPLETED Method, but program is just stuck here. Have tried sending to a pc or another android device. no luck! Anyone had any success or with a solution?

Zeke
  • 1,974
  • 18
  • 33
irobotxx
  • 5,963
  • 11
  • 62
  • 91

1 Answers1

2

What about your read input stream method?

Based on many posts all over SO, I have used this code to read input stream:

public void run() {
    Log.i(TAG, "BEGIN mConnectedThread");
    int bufferSize = 4096;
    byte[] buffer = new byte[bufferSize];
    // Keep listening to the InputStream while connected
    while (true) {
        try {
            // Read from the InputStream
            int bytesRead = -1;
            String message = "";
            if (mmInStream.available() > 0) {
                bytesRead = mmInStream.read(buffer);
                if (bytesRead > 0) {
                    while ((bytesRead == bufferSize) && (buffer[bufferSize - 1] != 0)) {
                        message = message + new String(buffer, 0, bytesRead);
                        bytesRead = mmInStream.read(buffer);
                    }
                    if((buffer[bytesRead - 1] != 0)) {
                        message = message + new String(buffer, 0, bytesRead); 
                    } else {
                        message = message + new String(buffer, 0, bytesRead - 1);    
                    }
                    mHandler.obtainMessage(ViewContactList.MESSAGE_READ, message.getBytes().length, -1, message).sendToTarget();
                } 
            }
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost(e.getMessage());
            break;
        }
    }
}

Try it as it is in ConnectedThread of BluetoothChatService.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • i am using the BluetoothChat Example as a guideline to transfer files between devices. Am not trying to implement the chat service just trying to send files to other devices, so i don't know if i really need the readInputStream there, but i have the one shown in the example in my code as well. – irobotxx Dec 28 '11 at 09:08
  • The BluetoothChat application allows two Android devices to carry out two-way byteArray over Bluetooth. This app establish RFCOMM channels/sockets, then connect to a remote device to enable data transfering over Bluetooth. As per my understanding, a listening RFCOMM Bluetooth socket (listenUsingRfcommWithServiceRecord in AcceptThread) is mandatory to receive the bytes written in your write method. The solution which you desire can be achieved using File Transfer Profile (OBEX FTP) and Object Push Profile (OBEX OPP). – Sourab Sharma Dec 28 '11 at 10:12
  • Look out for examples like: http://stackoverflow.com/questions/6326716/bluetooth-file-transfer http://stackoverflow.com/questions/4573761/bluetooth-file-transfer-in-android http://stackoverflow.com/questions/7380402/transfering-files-over-bluetooth-like-in-android-bluetooth-chat-example – Sourab Sharma Dec 28 '11 at 10:12
  • will check those and let you know how it goes. – irobotxx Dec 28 '11 at 12:49