1

We have an application that prints images to a bluetooth printer. This application has been working fine on Android 4.0 ICS but when we upgraded one of them to Android 4.1 jelly bean, printing stopped working with this in logcat:

W/System.err(19319): java.lang.SecurityException: Permission Denial: writing com.android.bluetooth.opp.BluetoothOppProvider uri content://com.android.bluetooth.opp/btopp from pid=19319, uid=10106 requires android.permission.ACCESS_BLUETOOTH_SHARE, or grantUriPermission()

The problem is that we are declaring that permission, so this error makes no sense to us. Here is the line from our manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.turner.itstrategy.LumenboxClient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_BLUETOOTH_SHARE"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.VIBRATE" />

     (stuff removed)
</manifest>

Here is the code we are using to print. This code has been taken from examples on stackoverflow and elsewhere.

ContentValues values = new ContentValues();

String path = Environment.getExternalStorageDirectory().toString();
File imageFile = new File(path, "CurrentLumenboxPrint.jpg");

//build the message to send on BT
values.put(BluetoothShare.URI, Uri.fromFile(imageFile).toString());
values.put(BluetoothShare.MIMETYPE, "image/jpeg");
values.put(BluetoothShare.DESTINATION, device.getAddress());
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);

// Here is where the exception happens      
final Uri contentUri = getApplicationContext().getContentResolver().insert(BluetoothShare.CONTENT_URI, values);

Right now we are dead in the water.. any advice appreciated.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • I have the same problem too using the Bluetooth on Jelly Bean. – Giuseppe Sep 15 '12 at 09:40
  • This code is not working below 2.3.8 can you please help me ? When I use this code, and try to send file it is showing nothing. I check using breakpoint and it's just passing from all line nothing happens then. Where as in 2.3.8 it's working. @grennis – Chirag Patel Mar 18 '13 at 07:21
  • The code posted doesnt work everywhere. Use the method posted in the answer below. – Greg Ennis Mar 19 '13 at 11:25
  • You are wrong @Drawable this code is working fine in HTC AMAZE Having 2.3.4 but not working in samsung Galaxy Tab P1000 Having Os 2.2. Any solution ? – Naveed Apr 01 '13 at 18:25

1 Answers1

6

Figured out this will no longer work on 4.1. The permission to write directly to the content provider is now protected with "signed" meaning you would have to sign your app with the same key used to sign the bluetooth app.

So here is how we ended up doing it. First use the share intent to send it directly to the app:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.setComponent(new ComponentName("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
startActivity(sharingIntent);

That works, but it pops up the "Select device" UI. If you don't want that you have to handle the intent android.bluetooth.devicepicker.action.LAUNCH and respond with the broadcast message android.bluetooth.devicepicker.action.DEVICE_SELECTED. But the user could still gets the chooser popup.

UPDATE: I wrote a blog post with a full description of how to do this.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Nice answer +1 for this. But can you please tell me how to share other media using this methode ? @grennis – Chirag Patel Mar 04 '13 at 07:11
  • Works, but you're right about the unpredictable nature of the Select Device UI. Very frustrating that they added this out of the blue(tooth) and for no obvious reason. – Yevgeny Simkin Mar 21 '13 at 04:08
  • could you provide a code sample for handling the LAUNCH intent and responding with a broadcast DEVICE_SELECTED ? – tuxGurl Apr 17 '13 at 19:32
  • Sorry about that. I fixed the link. – Greg Ennis May 11 '14 at 15:41
  • @GregEnnis Broadcasting the DEVICE_SELECTED no longer works in Android 6 (not sure about 5) due to it being a protected broadcast. Have you found any work-around for this? – David Mar 22 '16 at 18:24
  • Sorry,I haven't had to use this since a POC on 4.1 so I don't know how to work around that. – Greg Ennis Mar 24 '16 at 01:32
  • @Greg Ennis can you please share your whole code? Not sure which classes are mandatory to implement and which one are not. Also, same applies to AndroidManifest like what Bluetooth classes should be there? Thanks. – Red M Jan 24 '18 at 19:57