通过ESP32 BLE扫描,匹配打印机名称,连接服务,获取特征,通过特征发送CPCL指令实现打印,具体代码如下:
#include "BLEDevice.h"
// 汉印HM300LserviceUUID和charUUID
static BLEUUID serviceUUID("ff00");
static BLEUUID charUUID("ff02");
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
std::map<std::string, BLEAdvertisedDevice*> myDevices;
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println((char*)pData);
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server
pClient->connect(myDevice);
// if you pass BLEAdvertisedDevice instead of address,
//it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.println("特征获取为空");
pClient->disconnect();
return false;
}
Serial.println("特征获取成功");
if (pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if (pRemoteCharacteristic->canNotify())
pRemoteCharacteristic->registerForNotify(notifyCallback);
connected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises
* the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// 可以通过匹配打印机名称或者serviceUUID来匹配打印机
// if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
if (advertisedDevice.haveName() && (advertisedDevice.getName() == "HM-A300-879A")) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// 停止扫描
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup() {
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(10, false);
}
void printHY() {
// 打印
if (doConnect == true) {
// 连接打印机服务
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
}
if (connected) {
// 打印CPCL指令
String str = "CENTER\r\n";
str = str + "! 2 200 120 120 1\r\n";
str = str + "TEXT 4 0 0 0 text1\r\n";
str = str + "TEXT 4 0 0 40 text2\r\n";
str = str + "FORM\r\n";
str = str + "PRINT\r\n";
Serial.print(str.c_str());
pRemoteCharacteristic->writeValue(str.c_str(), true);
} else if (doScan) {
BLEDevice::getScan()->start(0);
printHY();
}
}
// This is the Arduino main loop function.
void loop() {
printHY();
delay(100000);
}
由于Arduino编码(UTF-8)与打印机编码(GBK)不一致,打印中文会导致乱码,如果中文是固定的,可以通过引入gbk编码的lib来解决。如果中文不固定变化特别大,我没有最优解。
896

被折叠的 条评论
为什么被折叠?



