Android的蓝牙打印停止工作4.1蓝牙、工作、Android

2023-09-04 06:07:54 作者:抱着猫睡觉

我们有打印图像,蓝牙打印机的应用程序。此应用程序一直在努力罚款的Andr​​oid 4.0 ICS,但是当我们升级他们中的一个将Android 4.1果冻豆,打印停止与这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 URI   内容://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>

下面是我们使用打印code。这code已被取自计算器的例子和其他地方。

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

现在,我们都死在水中..任何建议AP preciated。

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

这是工作,但它会弹出选择设备界面。如果你不想,你必须处理这个意图 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.

 
精彩推荐
图片推荐