Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any chances of supporting Bluetooth classic/LE on Android/iOS platforms? #51

Closed
deli-nik opened this issue Jun 3, 2020 · 5 comments
Closed

Comments

@deli-nik
Copy link

deli-nik commented Jun 3, 2020

This would be the perfect package that can integrate with everything if Bluetooth classic/LE is supported. Is there a plan for that?

@igorocampos
Copy link
Collaborator

igorocampos commented Jun 3, 2020

You can use this library to get the corresponding byte[] you want to print and then connect yourself via Bluetooth and send the data. Let me show how I did so in one of my Android projects:

//For this you will need:
//using Android.Bluetooth;

private static string bluetoothPrinterAddress = "00:01:02:03:0A:0B"; //replace with your BT Address
private static Java.Util.UUID PRINTER_UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");

private static BluetoothDevice GetDevice(BluetoothAdapter bluetoothAdapter)
{
    foreach (BluetoothDevice device in bluetoothAdapter.BondedDevices)
        if (device.Address == bluetoothPrinterAddress)
            return device;
    
    return null;
}

private static BluetoothSocket GetSocket(BluetoothDevice device)
{
    BluetoothSocket socket = device.CreateRfcommSocketToServiceRecord(PRINTER_UUID);
    socket.Connect();
    return socket;
}

private static void SendData(byte[] bytes, BluetoothSocket socket)
{
    var o = socket.OutputStream;
    o.Write(bytes, 0, bytes.Length);
    o.Close();
}

public static void Print(byte[] byteArray)
{
    //Bluetooth is turned off
    if (BluetoothAdapter.DefaultAdapter is null)
        return;

    BluetoothDevice device = GetDevice(BluetoothAdapter.DefaultAdapter);

    //Printer not found
    if (device is null)
        return;

    BluetoothSocket socket = GetSocket(device);
    SendData(byteArray, socket);
}

@lukevp
Copy link
Owner

lukevp commented Jun 3, 2020

@igorocampos this is awesome, I would love to support this since the library supports basically all other printing types already! Do you think you could implement this as a BluetoothPrinter type like we have for the other printer types? How are you using Bluetooth from within the Android namespace, is it built against xamarin or something? I wonder if we could do this in a cross-platform way?

@igorocampos
Copy link
Collaborator

@lukevp this code example was from a xamarin project alright. I don't own the devices anymore, but they were an Android POS with a BT printer embedded. But once you get the Bluetooth socket from whatever hardware you have, it should be straight forward to send the bytes over.

@deli-nik
Copy link
Author

deli-nik commented Jun 4, 2020

Yes, this is what I was planning to implement. Just asking if there are plans to support it right in the library. Thanks for the code snippet, will definitely use this library for my purpose. Great library 😁

@igorocampos
Copy link
Collaborator

igorocampos commented Jan 23, 2024

@igorocampos this is awesome, I would love to support this since the library supports basically all other printing types already! Do you think you could implement this as a BluetoothPrinter type like we have for the other printer types? How are you using Bluetooth from within the Android namespace, is it built against xamarin or something? I wonder if we could do this in a cross-platform way?

Coming back at this one again, @lukevp would you consider having 32feet as a dependency for BT comms?

If yes, I think we can definitely do a cross-platform Bluetooth printer, what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants