Bluetooth File transfer on Android(even restricted types)

孤者浪人 提交于 2019-12-10 19:52:13

问题


I was making an application in which I wanted to add a feature to send apk files via bluetooth. Its not allowed through the traditional method as apk is restricted file type so i used BluetoothShare.java. Apparently it doesn't work on Jellybean. I get a nasty security Exception. Same as this one. Android bluetooth print stopped working on 4.1

Is there any way I can go about doing this, if possible?


回答1:


rename the .apk to .zip and send it, then rename it back to .apk on phone.




回答2:


I was able to get a stock Nexus 7 running 4.4.2 to send APKs by changing the MIME type in the Intent to application/zip. But this still didn't change the block on receiving APKs. But since many/most ROMs remove that block, it is still useful to be able to send APKs from stock ROMs.

PackageManager pm = getPackageManager();
ApplicationInfo appInfo;
try {
    appInfo = pm.getApplicationInfo("org.fdroid.fdroid",
            PackageManager.GET_META_DATA);
    Intent sendBt = new Intent(Intent.ACTION_SEND);
    // NOT THIS! sendBt.setType("application/vnd.android.package-archive");
    sendBt.setType("application/zip");
    sendBt.putExtra(Intent.EXTRA_STREAM,
            Uri.parse("file://" + appInfo.publicSourceDir));
    sendBt.setClassName("com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
    startActivity(sendBt);
} catch (NameNotFoundException e1) {
    e1.printStackTrace();
}

This is a simple example because it only targets the one Bluetooth Activity that I see on my two devices (com.android.bluetooth.opp.BluetoothOppLauncherActivity). Unfortunately, the Activity not always called that (for example, com.broadcom.bt.app.opp.OppLauncherActivity), and even the package name can be different (for example, com.mediatek.bluetooth).

Here's how to handle that:

  • Limiting Android PackageManager to a single choice
  • https://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/


来源:https://stackoverflow.com/questions/16277593/bluetooth-file-transfer-on-androideven-restricted-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!