I have a requirement. I need to build an app, which uses Bluetooth to send media files like songs, images etc to another device. I have no knowledge about how to do this. Can anyone assist from scratch to make me get a rough idea about how getting this done. Sample code would be very helpful.
Thanks.
I think you should read this document once .
In this example they are sending PDF file form SD Card Path but i think you can also send
media file like audio and video as same.
see this : Bluetooth file transfer Android
private void envio() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File archivo=new File(_path);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(archivo) );
///////////////////pakage manager
PackageManager pm = getPackageManager();
List<ResolveInfo> appsList = pm.queryIntentActivities( intent, 1);
if(appsList.size() > 0) {
//Toast.makeText(this,"su telefono no cuenta con aplicacion de intercambio de datos",Toast.LENGTH_LONG).show();
}
//selleccionar la aplicacion de bluetooth
String packageName = null;
String className = null;
boolean found = false;
// BluetoothAdapter.checkBluetoothAddress("");
for(ResolveInfo info: appsList){
packageName = info.activityInfo.packageName;
if( packageName.equals("com.android.bluetooth")){
className = info.activityInfo.name;
found = true;
break;// found
}
}
if(! found){
Toast.makeText(this,"...",
Toast.LENGTH_SHORT).show();
// exit
}
intent.setClassName(packageName, className);
startActivity(intent);
}
Related
I am sending an excel file into a USB device in an activty via send Intent, and i can manage this process as it's supposed to be. After sending data, when i remove the USB device my current Activity (regardless of the which Activity is active) is closing without any warning or error with a code line. I just get the error below.
Unknown bits set in runtime_flags: 0x8000
The export excel file code:
private void exportFile(File file) {
ArrayList<Uri> pickedFileUris = new ArrayList<>();
if (android.os.Build.VERSION.SDK_INT >= 27) {
Uri imageUri = FileProvider.getUriForFile(getApplicationContext(), provider, file);
pickedFileUris.add(imageUri);
} else {
pickedFileUris.add(Uri.fromFile(file));
}
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("application/excel");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Export Items");
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, pickedFileUris);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{});
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
FileSaverActivity.firstInit = false;
Intent chooser = Intent.createChooser(sendIntent, null);
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, pickedFileUris.get(0), Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(chooser);
}
I am using that export function for export image and pdf files and never gets an error like that. I really don't know what is the problem. When i search about the runtime_flags: 0x8000 there are so many different things but found nothing to help me.
I have an Android application that uses the option to share in Android. The application worked fine to post a tweet, but recently stopped publishing the tweet giving an error. The error reason I have it located. My application downloads an image, which is attached to the tweet. That image is stored in a temporary folder and deleted when the onActivityResult method is called. Twitter is now calling this method before posting the tweet, therefore when you try to post the picture, it has already been removed.
Any suggestions on this? Is this something that will change in a Twitter update, or should I modify my application?
Use this code.
private void sendShareTwit() {
try {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
String filename = "<file path>/twitter_image.jpg";
File imageFile = new File(Environment.getExternalStorageDirectory(), filename);
tweetIntent.putExtra(Intent.EXTRA_TEXT, twitter_share_text);
tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
tweetIntent.setType("image/jpeg");
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo ri : lract) {
if (ri.activityInfo.name.contains("twitter")) {
tweetIntent.setClassName(ri.activityInfo.packageName,
ri.activityInfo.name);
resolved = true;
break;
}
}
startActivity(resolved ?
tweetIntent :
Intent.createChooser(tweetIntent, "Choose one"));
} catch (final ActivityNotFoundException e) {
System.out.rintln( "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT));
}
}
I want to make an application that could send itself (apk file) by bluetooth. but i have trouble with finding the apk file path. i tried this code:
final PackageManager pm = this.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
String st = null;
for (PackageInfo packageInfo : packages) {
if(packageInfo.packageName.contains("testbutton"))
st=packageInfo.packageName;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
String uri = "/data/app/";
uri+=st;
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));
startActivity(intent);
but st returns null value.
please help me with this. thanks in advance
finally i'd found the right answer that works in this purpose, thanks to #Kanak for her help :)
PackageManager pm = getPackageManager();
String uri = null;
for (ApplicationInfo app : pm.getInstalledApplications(0)) {
if(!((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1))
if(!((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1)){
uri=app.sourceDir;
if(uri.contains("com.example.test"))
break;
}
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));
startActivity(intent);
There is no need to iteration. Getting the application itself APK file uri is as easy as this:
String appUri = getApplicationInfo().publicSourceDir;
Also note that doc says this about publicSourceDir:
Full path to the publicly available parts of sourceDir,
including resources and manifest. This may be different from
sourceDir if an application is forward locked.
And also note that to send an APK file, you need to set the type to application/vnd.android.package-archive instead of image/*
So the complete snippet would be:
String appUri = getApplicationInfo().publicSourceDir;
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("application/vnd.android.package-archive");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(appUri)));
startActivity(Intent.createChooser(sharingIntent, "Share via"));
My application evokes the android PackageManager when a file is chosen and the user is presented with a choice of applications to handle how the file should be dealt with. I want to limit this choice to Bluetooth. Currently Bluetooth comes up as the first option which is fine and this all works. I was wondering if its possible to only present the user with this single option.
case REQUEST_FILE_SELECT:
if (requestCode == REQUEST_FILE_SELECT) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "File Uri: " + uri.toString());
// Get the path
String path = null;
try {
path = FileUtils.getPath(this, uri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d(TAG, "File Path: " + path);
// Get the file instance
File mFile = new File(path);
// Evoke the file chooser
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("*/*");
// Evoke the package manager
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(shareIntent,
PackageManager.GET_ACTIVITIES);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
if (packageName.equals("com.android.bluetooth")) {
Intent targetedShareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(mFile));
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
startActivity(Intent.createChooser(shareIntent,
"Share File"));
}
}
}
}
Solution: find out which apps does the device support for your intent, find the one that is bluetooh, invoke it directly.
This article answers your question:
http://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/
From the article:
We can see that the BT application is among those handlers. We could of course let the user pick that application from the list and be done with it. But if we feel we should be a tad more user-friendly, we need to go further and start the application ourselves, instead of simply displaying it in a midst of other unnecessary options…But how?
One way to do that would be to use Android’s PackageManager this way:
//list of apps that can handle our intent
PackageManager pm = getPackageManager();
List appsList = pm.queryIntentActivities( intent, 0);
if(appsList.size() > 0 {
// proceed
}
The above PackageManager method returns the list we saw earlier of all activities susceptible to handle our file transfer intent, in the form of a list of ResolveInfo objects that encapsulate information we need:
//select bluetooth
String packageName = null;
String className = null;
boolean found = false;
for(ResolveInfo info: appsList){
packageName = info.activityInfo.packageName;
if( packageName.equals("com.android.bluetooth")){
className = info.activityInfo.name;
found = true;
break;// found
}
}
if(! found){
Toast.makeText(this, R.string.blu_notfound_inlist,
Toast.LENGTH_SHORT).show();
// exit
}
We now have the necessary information to start BT ourselves:
//set our intent to launch Bluetooth
intent.setClassName(packageName, className);
startActivity(intent);
What we did was to use the package and its corresponding class retrieved earlier. Since we are a curious bunch, we may wonder what the class name for the “com.android.bluetooth” package is. This is what we would get if we were to print it out: com.broadcom.bt.app.opp.OppLauncherActivity. OPP stands for Object Push Profile, and is the Android component allowing to wirelessly share files.
Also in the article, how to enable the bluetooth from your application.
String mimetype = ".docx\tapplication/vnd.openxmlformats-officedocument.wordprocessingml.document";
File file = new File(FilePath, filename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
I use above code to read a .docx file.
How can I check whether application can support the file format or not?
If not, below message:
Toast.makeText(this, "Not be supported.", Toast.LENGTH_LONG).show();
Make a function like this and give it your intent. It will check if there is any app able to process this intent.
private boolean isIntentLaunchable(Intent intent)
{
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
return true;
}
return false;
}