Android 蓝牙打印在 4.1 上停止工作 [英] Android bluetooth print stopped working on 4.1

查看:17
本文介绍了Android 蓝牙打印在 4.1 上停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序可以将图像打印到蓝牙打印机.此应用程序在 Android 4.0 ICS 上运行良好,但是当我们将其中一个应用程序升级到 Android 4.1 jelly bean 时,在 logcat 中停止打印:

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:权限拒绝:编写 com.android.bluetooth.opp.BluetoothOppProvider uricontent://com.android.bluetooth.opp/btopp 来自 pid=19319, uid=10106需要 android.permission.ACCESS_BLUETOOTH_SHARE,或grantUriPermission()

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>

这是我们用来打印的代码.此代码取自 stackoverflow 和其他地方的示例.

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.

推荐答案

发现这将不再适用于 4.1.直接写入内容提供者的权限现在受已签名"保护,这意味着您必须使用用于签署蓝牙应用程序的相同密钥来签署您的应用程序.

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

这可行,但它会弹出选择设备"用户界面.如果您不想这样做,则必须处理 Intent android.bluetooth.devicepicker.action.LAUNCH 并使用广播消息 android.bluetooth.devicepicker.action.DEVICE_SELECTED 进行响应>.但是用户仍然可以获得选择器弹出窗口.

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.

这篇关于Android 蓝牙打印在 4.1 上停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆