Android File sharing via Bluetooth
<p>I have an requirement to list all available device on bluetooth and share any file from android device to selected device. I tried to use the below code </p>

<pre><code> private void sharefile(String deviceAddress,String filePath){
        File f =  new File(filePath);
        BluetoothAdapter objBluetoothAdapter = BluetoothAdapter
                        .getDefaultAdapter();

                BluetoothDevice device = objBluetoothAdapter
                        .getRemoteDevice(deviceAddress);
                Log.v("Device", "" + device.getName());

                final UUID uuid = UUID
                        .fromString("00001101-0000-1000-8000-00805F9B34FB");

                try {
                    byte[] buffer = new byte[(int) f.length()];
                    FileInputStream fis = new FileInputStream(f);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(buffer, 0, (int) f.length());

                    BluetoothSocket socket = device
                            .createRfcommSocketToServiceRecord(uuid);

                    OutputStream os = socket.getOutputStream();
                    os.write(buffer, 0, buffer.length);
                    os.flush();
                    os.close();
                    socket.close();   
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }
</code></pre>

<p>While executing the above code shows an <strong>Transport endpoint not connected</strong> message on Logcat.</p>

<p><strong>06-11 18:26:49.960: W/System.err(9812): java.io.IOException: Transport endpoint is not connected
06-11 18:26:49.960: W/System.err(9812):     at android.bluetooth.BluetoothSocket.writeNative(Native Method)
06-11 18:26:49.960: W/System.err(9812):     at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:346)
06-11 18:26:49.960: W/System.err(9812):     at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
06-11 18:26:49.960: W/System.err(9812):     at com.cit.bt.ShareMessageActivity.connectDevice(ShareMessageActivity.java:138)
06-11 18:26:49.960: W/System.err(9812):     at com.cit.bt.ShareMessageActivity.shareFile(ShareMessageActivity.java:83)
06-11 18:26:49.960: W/System.err(9812):     at com.cit.bt.ShareMessageActivity.onListItemClick(ShareMessageActivity.java:73)
06-11 18:26:49.960: W/System.err(9812):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
06-11 18:26:49.960: W/System.err(9812):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-11 18:26:49.960: W/System.err(9812):     at android.widget.ListView.performItemClick(ListView.java:3736)
06-11 18:26:49.960: W/System.err(9812):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1974)
06-11 18:26:49.960: W/System.err(9812):     at android.os.Handler.handleCallback(Handler.java:587)
06-11 18:26:49.960: W/System.err(9812):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 18:26:49.960: W/System.err(9812):     at android.os.Looper.loop(Looper.java:130)
06-11 18:26:49.960: W/System.err(9812):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-11 18:26:49.960: W/System.err(9812):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:26:49.960: W/System.err(9812):     at java.lang.reflect.Method.invoke(Method.java:507)
06-11 18:26:49.960: W/System.err(9812):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-11 18:26:49.960: W/System.err(9812):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-11 18:26:49.960: W/System.err(9812):     at dalvik.system.NativeStart.main(Native Method)</strong></p>

<p>and also tried to use below alternative code to connect the device,it connect the device but failed to transfer the file.</p>

<pre><code>BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter()
                .getRemoteDevice(deviceAddress);
        Method m;

            m = hxm.getClass().getMethod("createRfcommSocket",
                    new Class[] { int.class });
            BluetoothSocket socket = (BluetoothSocket) m.invoke(hxm,
                    Integer.valueOf(1));
            socket.connect();
</code></pre>

<p>I have provided required permission in android manifest too, but it's failed to connect.
Anyone help me to fix this problem.</p>

<p>Can you anyone provide me the logic behind the Bluetooth File Transfer from Android market.</p>

<p>Thanks!</p>
