0%

蓝牙传输文件代码

主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void sendFile(Activity activity) {
PackageManager localPackageManager = activity.getPackageManager();
Intent intent = new Intent();
HashMap<String, ActivityInfo> shareItems = new HashMap<>();
try {
intent.setAction(Intent.ACTION_SEND);
String filepath = App.getContext().getPackageManager()
.getApplicationInfo(BuildConfig.APPLICATION_ID, 0).sourceDir;
File file = new File(filepath);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
// intent.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(new File(localApplicationInfo.sourceDir)));
intent.setType("*/*");
List<ResolveInfo> resolveInfos = localPackageManager.queryIntentActivities(intent, 0);
for(ResolveInfo resolveInfo : resolveInfos){
ActivityInfo activityInfo = resolveInfo.activityInfo;
String progressName = activityInfo.applicationInfo.processName;
if(progressName.contains("bluetooth"))
shareItems.put(progressName, activityInfo);
}
} catch (Exception e) {
Log.e(e.getMessage(), e);
}
ActivityInfo activityInfo = shareItems.get("com.android.bluetooth");
if (activityInfo == null)
activityInfo = shareItems.get("com.mediatek.bluetooth");
if (activityInfo == null) {
Iterator<ActivityInfo> iterator = shareItems.values().iterator();
if (iterator.hasNext())
activityInfo = iterator.next();
}
if (activityInfo != null) {
intent.setComponent(new ComponentName(activityInfo.packageName, activityInfo.name));
activity.startActivityForResult(intent, BLUETOOTH_SHARE_REQUEST_CODE);
}
}