Thursday, December 05, 2013

Android Tip - Transferring File via Bluetooth

If you want to easily transfer a file from your application to another Android device, you can make use of Bluetooth.

Beginning with Android 4.1, you can now make use of the built-in Bluetooth Sharing app available on the device. The following code snippet shows how to use an Intent object to manually invoke the Bluetooth Sharing app to send an image from one device to another:

import java.io.File;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

...

        
        //---assuming you have a file named MyPhoto.jpg in
        // your SD card---
        File file = new File(
            Environment.getExternalStorageDirectory().
            getPath() + "/MyPhoto.jpg");
        Intent i = new
            Intent(android.content.Intent.ACTION_SEND);
        i.setType("image/*");
        i.setComponent(
            new ComponentName("com.android.bluetooth",                         
   "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
        i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(i);      

    }

No comments: