1

i am trying ti send file from android to another device by using the following code

        socket = device.createRfcommSocketToServiceRecord(uuid);
        socket.connect();
        OutputStream os = socket.getOutputStream();
        File f = new File(strPath);
        byte [] buffer = new byte[(int)f.length()];
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(buffer,0,buffer.length);
        os.write(buffer,0,buffer.length);
        os.flush();
        os.close();
        socket.close();

I added BLUETOOTH and BLUETOOTH_ADMIN to user permissions in AndroidManifest.xml

But file is not transferring, connection is establishing b/w devices

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Vishnu V
  • 382
  • 5
  • 16

2 Answers2

2

I don't know why your way isn't working, if someone knows the answer please post I would like to know. But below is how I got mine to work, I'm basically sending the file in chunks of 1024 bytes.

/*Transmit*/
private OutputStream mOut;
byte[] mBuffer = byte[1024]
mBtSocket = _socket;
mOut = mBtSocket.getOutputStream();
InputStream inFile = new FileInputStream(file);
while((mLen = inFile.read(mBuffer, 0, 1024)) > 0){
         mOut.write(mBuffer, 0, mLen);
}

/*Receive*/
private InputStream mIn;
byte[] mBuffer = byte[1024]
File file = new File(fileName);
OutputStream outFile = new FileOutputStream(file);
long bytesReceived = 0;
while (bytesReceived < fileSize) {  // I send fileSize as msg prior to this file transmit
    mLen = mIn.read(mBuffer);
if(mLen > 0) {
    bytesReceived+=mLen;
    outFile.write(mBuffer, 0, mLen);
} else {
    Log.d(TAG,"Read received -1, breaking");
    break;
}
}
outFile.close();
broody
  • 697
  • 1
  • 6
  • 17
  • Thanks for the replay. Is it possible to send a file to other phones (not an android phone) the transmit method ? – Vishnu V Feb 08 '12 at 07:05
  • Yes, so long as youre both doing rfcomm – broody Feb 08 '12 at 15:22
  • Can you post your full source code so that other can easily understood. Thanks. – anddev Mar 22 '12 at 12:02
  • If user sends more than 1 file, how to reset the `InputStream` to call `read(byte[])` from zero? (`InputStream` of bluetooth socket). –  Jun 01 '16 at 01:48
0

vishnu please go to following link

It is regarding your question I guess you might get help from this , this and this.

Community
  • 1
  • 1
dreamcoder
  • 1,233
  • 1
  • 11
  • 25
  • sorry, those links don't help me :-( – Vishnu V Feb 08 '12 at 06:40
  • Igot Exception says 02-08 12:55:37.659: W/System.err(20167): java.io.IOException: Service discovery failed at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:410) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213) at com.myapp.bts.BTSActivity.onClick(BTSActivity.java:83) at android.view.View.performClick(View.java:2485) at android.view.View$PerformClick.run(View.java:9080) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:123) – Vishnu V Feb 08 '12 at 07:28
  • That means youre not able to connect to the other device. you sure your uuid and mac addr is correct? also i'd put socket.connect in a for loop to try 3 times. – broody Feb 08 '12 at 15:31