Vishnu V
Vishnu V

Reputation: 412

Send file to other Bluetooth device from android

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

Upvotes: 1

Views: 5560

Answers (2)

broody
broody

Reputation: 707

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();

Upvotes: 2

dreamcoder
dreamcoder

Reputation: 1253

vishnu please go to following link

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

Upvotes: 0

Related Questions