Bluetooth File transfer on Android(even restricted

2019-07-03 16:12发布

问题:

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/