我們有一個將圖像打印到藍牙打印機的應用程序。此應用程序在Android 4.0 ICS上運行良好,但是當我們將其中一個升級到Android 4.1 jelly bean時,此打印在logcat中停止工作:Android藍牙打印停止工作4.1
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 ()
問題是我們正在聲明該權限,所以這個錯誤使得n o感覺到我們。以下是我們清單中的行
<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和其他地方的例子中提取的。
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);
現在我們死在水中..任何建議讚賞。
我也有同樣的問題,使用果凍豆藍牙。 – Giuseppe
此代碼在2.3.8以下無法運行,請幫助我嗎?當我使用這個代碼,並嘗試發送文件時,它什麼也沒有顯示。我檢查使用斷點,它只是從所有行傳遞沒有任何反應。在2.3.8中它正在工作。 @grennis –
發佈的代碼並不適用於任何地方。使用下面答案中發佈的方法。 –