RAAAAM
RAAAAM

Reputation: 3380

How to send file through Bluetooth using Intent

I want to send set of string via bluetooth. I googled for sending text via bluetooth using intent action, but I didn't get any proper answer. also I tried in developer.android.com, but I confused about there codings. How to transfer the file via bluetooth using intent?

Upvotes: 1

Views: 3593

Answers (3)

Rafsanjani
Rafsanjani

Reputation: 5443

val intent = Intent(Intent.ACTION_SEND).apply {
            type = "text/plain"
            `package` = "com.android.bluetooth"
            putExtra(Intent.EXTRA_STREAM, Uri.parse("some Uri string")
        }

        startActivity(Intent.createChooser(intent, "Share File"))

Instead of having the user choose the Bluetooth application from the chooser list, we call the setPackage("com.android.bluetooth") or Intent.`package`="com.android.bluetooth (kotlin). This will let the user to only choose the destination device and the system will handle the transfer asynchronously

Upvotes: 0

Roy Samuel
Roy Samuel

Reputation: 790

You can make an explicit call to ACTION_SEND using intents as shown below.

You can send a file to a paired device through obex in a couple of ways:

With the ACTION_SEND intent, that will popup a menu with the application that can handle the file type you want to send, from which the user will need to select bluetooth, and then the device.

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg"));
startActivity(Intent.createChooser(i, "Send Image"));

Upvotes: 5

Morrison Chang
Morrison Chang

Reputation: 12121

I don't think you can send text or a file via Bluetooth via an Intent unless you have a supporting app. Intents are for invoking/calling between applications/activites on the device. As there is no predefined Intent for such a thing you would need to write it yourself. For an idea of how to use Bluetooth on Android look at the Bluetooth Chat sample program in the samples directory of the SDK.

Upvotes: 1

Related Questions