【问题标题】:Print Image(s) via Paired Bluetooth Printer Canon CP 900, CP 800通过配对的蓝牙打印机打印图像 Canon CP 900、CP 800
【发布时间】:2013-12-24 18:18:01
【问题描述】:

我需要将我的应用与配对的蓝牙设备连接,这将通过蓝牙打印机(佳能)打印图像 CP900 & CP800 - SELPHY).

没有找到任何 Canon Printer 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" /> 

【问题讨论】:

  • 嗨,Moon,您是否已通过佳能 selphy 找到可行的解决方案?对于蓝牙,我需要单独的适配器吗?在规范页面中没有提到佳能网站上的蓝牙..谢谢

标签: java android printing bluetooth


【解决方案1】:

是的,我同意你的看法,这是通过蓝牙发送/打印图像Canon 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);

这可行,但它会弹出“选择设备”用户界面。如果你不希望你必须处理意图 android.bluetooth.devicepicker.action.LAUNCH 并以广播消息 android.bluetooth.devicepicker.action.DEVICE_SELECTED 响应>。但是用户仍然可以获得选择器弹出窗口。

如果您仍有疑问,请告诉我...

感谢我的蓝牙打印/发送应用程序中的@grennis,我使用了相同的来源。

【讨论】:

    【解决方案2】:

    我发现了通过蓝牙将图像发送/打印到佳能 CP 900、CP 800 和任何其他可用的配对蓝牙设备或打印机的最最简单的方法。 p>

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

    【讨论】:

      【解决方案3】:

      这就是我使用它的方式。

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

      }

      【讨论】:

      • 但是蓝牙共享信息呢? Stalker 可以分享一下示例代码吗
      猜你喜欢
      • 2011-12-28
      • 2016-06-23
      • 1970-01-01
      • 2014-08-25
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      相关资源
      最近更新 更多