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?
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"));
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.
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
Related
Is there an app which calls a definable URL when a NFC sticker is detected? I want to put different stickers on my desk and when I place my phone on one of the stickers a specific URL should be call:
If StickerA is detectd call https://www.myserver.com/?val=a
If StickerB is detectd call https://www.myserver.com/?val=b
If StickerC is detectd call https://www.myserver.com/?val=c
etc.
Is this possible with android without activating the phone and is there a an existing app for that task?
With Web NFC on Android, any website can write a URL NDEF Record to an NFC tag. This URL will be launched in user's browser on Android when user taps NFC tag.
const urlRecord = {
recordType: "url",
data:"https://w3c.github.io/web-nfc/"
};
const ndef = new NDEFReader();
await ndef.write({ records: [urlRecord] });
See https://web.dev/nfc/#read-and-write-a-url-record
Detecting a sticker, and taking an action are two different things. When you detect a sticker, I think you can get the ID of the sticker, then, based on the ID, you would create an Action View Intent passing the URL and that would cause an external browser window to open with the URL you passed.
//pass any url to this function to open a new browser session
protected void handleExternalDeviceActivity(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
you can write unique URL in every "sticker" or nfc tag by many Android App you can try NFC tools, then when you touch your Android device it will open the URL.
without activiating the Android device you will not be able to communicate with NFC tag*.
*you could with rooted device and you need to write the app by your self.
The Android OS itself has the ability to detect NFC and launch a web browser to open a URL on the NFC stickers/Tags (as long has the phone has NFC hardware and it is turned on)
The Sticker/Tag has to have the correctly formatted NDEF data written to it.
You can use https://play.google.com/store/apps/details?id=com.wakdev.wdnfc or other apps to write the correct data to the stickers/tags to store the URL correctly.
You can write your own App to do this as well but it does not require a "rooted device". But it does require then device to have the hardware and for it to be turned on.
I am looking for viable ways to send a file from my android app to another android device that doesnt have my app installed.
Option 1. Via share intent: https://stackoverflow.com/a/16755810/1865583
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(picURI);
sharingIntent.setType("image/*");
sharingIntent.setPackage("com.android.bluetooth");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image"));
Issue regarding option 1:
I would love if i could specify the device i want to send this file to, in the intent parameters? I've searched online but cant find this option.
I've stumbled across this multiple times but it only works on particular devices as far people claim. https://evilzone.org/java/%28android%29-three-ways-to-send-data-over-bluetooth/
ContentValues values = new ContentValues();
String address = device.getAddress();
values.put(BluetoothShare.URI, Uri.fromFile(new File("somefile.mp3")).toString());
values.put(BluetoothShare.DESTINATION, address);
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
Is there a working/tested way to specify the device the bluetooth will try to send the file too? (and pair by default if necessary)?
Option 2. Using Bluetooth sockets to control the file transfer :
I am actually quite interested in developing this options, but all the examples online go about sending files from client to server where both code bases are developed by me.
What i need is to connect via bluetooth to the other android bluetooth enabled device and initiate the file transfer? But i am unsure how to initiate this transfer or which protocols are used if any etc? How would you connect to the default bluetooth app to send it a file?
I've found some information that i thought using as starting point https://stackoverflow.com/a/17237945/1865583
The user talks about using 00001106-0000-1000-8000-00805F9B34FB (File transfer service) and starting a RfcommSocket for communication. Would this be the right way to go to communicate with bluetooth android apps for file transfer?
want to connect to device and start receiving data from the external device
now able to list devices and pair to that .....now wish to connet to it and start receiving data from it
Is there any way because i dont exactly know wat kind of data it is sending
means in which formate etc ...
that is Bluetooth device is continuously sends data to app so the app should continuously receive data
use listenUsingRfcommWithServiceRecord instead of
this code in service
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class} );
btSocket = (BluetoothSocket) m.invoke(device, 1);
Answers to your questions are as follows
Is there any way because i dont exactly know wat kind of data it is
sending means in which formate etc ...
Yes there are 2 ways you can do it
1) use intent
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"));
code sample to send data using inbuild bluetooth
2) Use open source code which use socket connection and bluetooth manager to do it via code. following is the link of open source code
blueterm opensource code
Data sent over bluetooth is in form of byte array which you have to parse and convert it to string or any form that you want to use in your code.
There are two things in bluetooth
1) pairing.
2) global connection.
If you use first method of bluetooth (via intent) device will be paired but connection will not be continuous. while sending data connection is established else connection is not there. But if you are using blueterm open source code you pairing and connection is constant and will not break untill user wants.
So its upto you which kind of bluetooth functionality you want.
Let me know if my answer solves your problem
I'd like to share information between my own app running on 2 different phones via a bluetooth intent.
Lets say i have some data on phone a, then i will tap synch and it will start the same app at phone b (if it's not already open) with a bundle containing that "data".
My app on phone b acts acordingly.
Is that possible?
I am not really sure if this is what you're looking for.
Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileLocation));
startActivity(Intent.createChooser(i, "Send Image"));
This intent shows all available options for file send such as Email and Bluetooth. Choose Bluetooth and the device initiates bluetooth discovery.
Thanks!
From my understanding, it is not possible.
Use BluetoothSocket and BluetoothServerSocket instead
I recently saw this interesting video about NFC, and i know you are talking about bluetooth.
but watch this video http://www.youtube.com/watch?feature=player_detailpage&v=49L7z3rxz4Q#t=768s
Timestamp added, starts at: 12:48.
What they did is starting the app trough nfc but probably they send the data trough bluetooth. Its really user-friendly.
In this way you do not need to push the button sync but just bump eachother phones together!
I hope this helps you maybe further,
Daniel
I think you must make use of BluetoothServerSocket to accept incoming connections. Exchange data with server once connected. To get started check out this doc.
Start-up
You could find the source code in your SDK. Download samples from Android SDK manager. Select 'Samples for SDK' from the required SDK version.
Go to
<location of android-sdk>/samples/<version>/
Open 'Bluetooth Chat' application. It has almost everything you need.
Thanks!
By the way, don't forget to accept the answer!
I am sending file using following code in my android application :
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file) );
startActivity(intent);
But it shows me list like email , bluetooth , and many other. I dont want this list and should be sent directly without user interaction to perticular paired device .
Is it possible in android ?
Thanks in advance.
Nope, current API's will not allow you to bypass user confirmation, but you can do this programmatically using the Bluetooth API's after the pairing stage. Establish a RFCOMM and then use InputStream/OutputStream to receive/send files. A great place to start is looking at the BluetoothChat example by google.