Android bluetooth print stopped working on 4.1

We have an application that displays images on a bluetooth printer. This application works 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: Resolution: write 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 this permission, so this error does not make any sense to us. Here is a 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 use to print. This code was taken from examples in 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); 

Now we are dead in the water .. any advice appreciated.

+1
source share
1 answer

It turned out that this will no longer work on 4.1. The permission to directly contact the content provider is now protected by a β€œsigned” value that you will need to sign the application with the same key that was used to sign the Bluetooth application.

So, this is how we ended it. First, use the intention to share the sending directly to the application:

 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); 

This works, but the "Select Device" user interface appears in it. If you do not want you to have to process the android.bluetooth.devicepicker.action.LAUNCH intent and respond with the android.bluetooth.devicepicker.action.DEVICE_SELECTED broadcast message. But the user can still get a selection popup.

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

+5
source

All Articles