iOS Question [Solved] Printing with BLE

aeric

Expert
Licensed User
Longtime User
I downloaded an app from App Store call Tools4BTPrinter and able to print. Pairing is not required from the iOS settings but through scanning the BT device.
I am able to connect to my 58mm bluetooth thermal printer using iBLE library in the example but I don't know how I can use Manager.WriteData as I am not sure which Service and Characteristic to pass.

1669221455993.png

 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
manager.WriteData("18F0","2AF1", Data)
' or manager.WriteData("18F1","2AF1", Data)

see
 
Upvote 1
Solution

aeric

Expert
Licensed User
Longtime User
Here is the logs if anyone may find useful.

B4X:
Copying updated assets files (9)
Application_Start
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Application_Active
Application_Inactive
Application_Active
Application_Inactive
Application_Active
Found: LMP201, 66853B02-602C-AAFC-2B9F-6F4459580AD7, RSSI = 127, (read only map) {
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = LMP201;
    kCBAdvDataManufacturerData = {length = 8, bytes = 0x4b43021a33621767};
    kCBAdvDataRxPrimaryPHY = 0;
    kCBAdvDataRxSecondaryPHY = 0;
    kCBAdvDataServiceUUIDs =     (
        "E7810A71-73AE-499D-8C15-FAA9AEF0C3F2",
        18F0
    );
    kCBAdvDataTimestamp = "690913305.374766";
}
connecting
Discovering services
Services discovery completed.
Connected
Application_Inactive
Application_Background
Application_Foreground
Application_Active
Disconnected
Disconnected
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0

aeric

Expert
Licensed User
Longtime User
Just found this thread:
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Really thanks to you @Star-Dust
I still don't understand the Assigned Numbers but never mind.
Now I am able to print to my bluetooth printer. Tested with GB18030 charset. Will do more tests and see if I can print image later. Cheers! :)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
ble works differently from classic Bluetooth. when you scan you receive a set of information about the devices with which you can communicate. for each device you receive a series of services that are listening and for each service a set of characteristic that this service makes available.

when you connect to the printer you receive a list of available services concerning communication with the printer which are usually 18F0 and 18F1. and it also receives a list of characteristics regarding two-way or one-way communication or whatever.

most likely you have ignored these strings that have arrived and therefore at the time of transmission you do not know what to insert. the ones I gave you are the standard ones that printers usually send. but theoretically you should copy exactly the services and features it receives from the device upon connection
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
' after connection
Private Sub manager_Connected (Services As List)
    ' list of sevice
    For Each s As String In Services
        ' request characteristic for service
        log("Service: " & s)
        manager.ReadData(s)
    Next  
End Sub

Private Sub manager_DataAvailable (Service As String, Characteristics As Map)
    For Each id As String In Characteristics.Keys
        Log(Service & ": " & id & " - " & Characteristics.Get(id))
    Next
End Sub
 
Upvote 1

tseyfarth

Member
Licensed User
Longtime User
Here is a video I test to print to my bluetooth thermal printers through BLE.

Hello Aeric,

Can you share the project in your video? I'm trying to learn BT-LE and am having trouble, sending, just like you. Your help would be greatly appreciated!

Tim
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Hello Aeric,

Can you share the project in your video? I'm trying to learn BT-LE and am having trouble, sending, just like you. Your help would be greatly appreciated!

Tim
Sorry, I don't feel I want to share the project as I took time to develop it. If you have question, please start a new thread.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I attach a simple working example.
I just modify Erel's BLE Example and add some code to print using ESCPOS commands.

Make sure scan and connected successfully to the bluetooth LE printer.
Click on ReadData button and then WriteData button.
It should print "Print Success!".

B4X:
Private Sub btnWriteData_Click
    ' Print with ESCPOS
    PrintString("" & Chr(0x1B) & Chr(0x40))                    ' Clear
    PrintString("" & Chr(0x1B) & Chr(0x61) & Chr(0x01))        ' Centre
    PrintString("Print Success!")
    PrintString("" & Chr(0x0A) & "" & Chr(0x0A))            ' Line Feed X2
    PrintString("" & Chr(0x0A) & "" & Chr(0x0A))            ' Line Feed X2
End Sub

Sub PrintString (data As String)
    #If B4A
    manager.WriteData(UUID("18F0"), UUID("2AF1"), data.GetBytes("UTF8"))
    #Else If B4i
    manager.WriteData("18F0", "2AF1", data.GetBytes("UTF8"))
    #End If
End Sub
 

Attachments

  • BLEPrint.zip
    181.3 KB · Views: 228
Upvote 0
Top