我需要将我的应用与配对的蓝牙设备连接, 打印图像 通过 蓝牙打印机(佳能) CP900& CP800 - SELPHY) 。
我未找到任何佳能打印机Android SDK 任何帮助或链接都会明显。
我发现此link有帮助,但我得到 蓝牙活页夹是空的
我的程序包含两个java类,第一个是 BluetoothActivity.java ,第二个是 BluetoothShare.java
public class BluetoothActivity extends Activity {
public static final String LOG_TAG = "MainActivity";
BluetoothDevice device = null;
Uri contentUri;
BluetoothAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.btnPrint);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String filePath = Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES).toString() + "/kitkat.jpg";
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) return;
if (adapter.isEnabled()) {
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
//build bluetooth request
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, Uri.fromFile(new File(filePath)).toString());
values.put(BluetoothShare.DESTINATION, device.getAddress());
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
@SuppressWarnings("unused")
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
}
}
//turn off the discovery
adapter.cancelDiscovery();
}
});
}
}
将此code用于 BluetoothShare.java
清单权限: -
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
答案 0 :(得分:1)
是的,我同意你的看法,这是通过蓝牙发送/打印图像到佳能CP 900,CP 800 和 任何其他可用的配对蓝牙设备或打印机 。
想出这将不再适用于4.1。直接写入内容提供商的权限现在受“签名”保护,这意味着您必须使用用于签署蓝牙应用程序的相同密钥对您的应用进行签名。
所以这就是我们最终如何做到这一点。首先使用共享意图将其直接发送到应用程序:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("image/jpeg");
File file = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES) + "/kitkat.jpg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
这样可行,但它会弹出“选择设备”UI。如果您不希望必须处理意图 android.bluetooth.devicepicker.action.LAUNCH 并使用广播消息进行响应 android.bluetooth.devicepicker.action.DEVICE_SELECTED 。但是用户仍然可以获得选择器弹出窗口。
如果你心中仍有疑虑,请告诉我......
在我的蓝牙打印/发送应用中信用到@grennis我使用了相同的来源。
答案 1 :(得分:0)
这就是我使用它的方式。
public class PrinterAdapter {
private String _image2Print; // this is your image uri
private Context _context;
@SuppressWarnings("unused")
private final static String TAG = "Bluetooth-Printer";
public PrinterAdapter(Context context) {
_context = context;
}
public PrinterAdapter(Context context, String image2Print) {
_context = context;
_image2Print = image2Print;
}
public void Print() throws IOException {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) return;
if (adapter.isEnabled()) {
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
//build bluetooth request
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, Uri.fromFile(new File(_image2Print)).toString());
values.put(BluetoothShare.DESTINATION, device.getAddress());
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
@SuppressWarnings("unused")
Uri contentUri = _context.getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
}
}
//turn off the discovery
adapter.cancelDiscovery();
}
}
答案 2 :(得分:0)
我找到了通过蓝牙向Canon CP 900,CP 800 以及任何其他可用的配对蓝牙设备或打印机发送/打印图像的最简单方法。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("image/jpeg");
File file = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES) + "/kitkat.jpg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);