2

I need to connect my app with paired bluetooth devices, that will print images via Bluetooth printer (Canon CP900 & CP800 - SELPHY).

And I did not find any Canon Printer Android SDK any help or link will appreciable.

I found this link helpful, but i am getting Bluetooth binder is Null

My program contains two java classes, first is BluetoothActivity.java and second is 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();
        }
    });
  }
}

Using this code for BluetoothShare.java

Manifest Permissions:-

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
Community
  • 1
  • 1
Sun
  • 6,768
  • 25
  • 76
  • 131
  • Hi Moon, have you reached a working solution with canon selphy? For the bluetooth do I need a separate adapter? On canon site bluetooth is not mentioned in the specs page..thanks – Apperside Apr 28 '14 at 15:38

3 Answers3

1

Yeah I agree with you, this is the most easiest way to send / print image via Bluetooth to Canon CP 900, CP 800 and to any other available paired Bluetooth Devices or Printers.

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

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.

If still have some doubts in your mind, then let me know...

Credit goes to @grennis in my Bluetooth printing/sending App i used same source.

Community
  • 1
  • 1
Android
  • 1,529
  • 1
  • 12
  • 27
0

This is how I used it.

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

}

hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • but what about BluetoothShare information? Stalker can you share a sample code with me – Sun Dec 10 '13 at 08:08
0

I found the most easiest way to send / print image via Bluetooth to Canon CP 900, CP 800 and to any other available paired Bluetooth Devices or Printers.

        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);
Sun
  • 6,768
  • 25
  • 76
  • 131