android 将数据(打印)发送至蓝牙(BLE)打印机

vd8tlhqk  于 2022-12-31  发布在  Android
关注(0)|答案(1)|浏览(311)

我面临着连接我的Android应用程序到BLE打印机(斑马ZD420),然后打印条形码标签。首先,我用下面的代码来检索服务和特征,我可以使用。

procedure TForm5.BluetoothLE1ServicesDiscovered(const Sender: TObject;
  const AServiceList: TBluetoothGattServiceList);
var
  I, C: Integer;
begin
  if AServiceList.Count > 0 then
  begin
    for I := 0 to AServiceList.Count - 1 do
    begin
      memo1.lines.Add((I + 1).ToString+' - '+AServiceList[I].UUIDName+' - '+AServiceList[I].UUID.ToString);
      for C := 0 to AServiceList[I].Characteristics.Count - 1 do
        memo1.lines.Add('    - '+AServiceList[I].Characteristics[C].UUIDName+' - '+AServiceList[I].Characteristics[C].UUID.ToString);
    end;
  end
  else
    memo1.lines.Add('- Not allow access or no services');
    Listbox1.Enabled := True;
end;

然后我从打印机得到的结果如下

- Discovering services -->
1 - GAP - {00001800-0000-1000-8000-00805F9B34FB}
    - Device Name - {00002A00-0000-1000-8000-00805F9B34FB}
    - Appearance - {00002A01-0000-1000-8000-00805F9B34FB}
    - Peripheral Privacy Flag - {00002A02-0000-1000-8000-00805F9B34FB}
    - Reconnection Address - {00002A03-0000-1000-8000-00805F9B34FB}
    - Peripheral Preferred Connection Parameters - {00002A04-0000-1000-8000-00805F9B34FB}
2 - GATT - {00001801-0000-1000-8000-00805F9B34FB}
    - Service Changed - {00002A05-0000-1000-8000-00805F9B34FB}
3 - DEVICE INFORMATION - {0000180A-0000-1000-8000-00805F9B34FB}
    - Model Number String - {00002A24-0000-1000-8000-00805F9B34FB}
    - Serial Number String - {00002A25-0000-1000-8000-00805F9B34FB}
    - Firmware Revision String - {00002A26-0000-1000-8000-00805F9B34FB}
    - Hardware Revision String - {00002A27-0000-1000-8000-00805F9B34FB}
    - Software Revision String - {00002A28-0000-1000-8000-00805F9B34FB}
    - Manufacturer Name String - {00002A29-0000-1000-8000-00805F9B34FB}
    - PnP ID - {00002A50-0000-1000-8000-00805F9B34FB}
4 -  - {38EB4A80-C570-11E3-9507-0002A5D5C51B}
    -  - {38EB4A81-C570-11E3-9507-0002A5D5C51B}
    -  - {38EB4A82-C570-11E3-9507-0002A5D5C51B}
    -  - {38EB4A83-C570-11E3-9507-0002A5D5C51B}
    -  - {38EB4A84-C570-11E3-9507-0002A5D5C51B}

从这里开始:Zebra sample on GitHub
我试了这个代码:

const ZPRINTER_DIS_SERVICE_UUID = '{0000180A-0000-1000-8000-00805F9B34FB}'; // Or '180A'. Device Information Services UUID
const ZPRINTER_SERVICE_UUID='{38EB4A80-C570-11E3-9507-0002A5D5C51B}';
const READ_FROM_ZPRINTER_CHARACTERISTIC_UUID = '{38EB4A81-C570-11E3-9507-0002A5D5C51B}';
const WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID  = '{38EB4A82-C570-11E3-9507-0002A5D5C51B}';
const ZPL_TEST_LABEL = '~hi^XA^FO20,20^BY3^B3N,N,150,Y,N^FDHello WeChat!^FS^XZ\r\n';

首先我得找到那个装置

BluetoothLE1.DiscoverDevices(1000);

DiscoverLEDevice事件显示tListBox中的设备列表

procedure TForm6.BluetoothLE1DiscoverLEDevice(const Sender: TObject;
  const ADevice: TBluetoothLEDevice; Rssi: Integer;
  const ScanResponse: TScanResponse);
var
  Name: string;
  I: Integer;
  DCount: Integer;
  NumberOfDevices: Integer;
begin
  DCount := BluetoothLE1.DiscoveredDevices.Count;
  NumberOfDevices := Listbox1.Count;
  for I  := 0 to DCount - 1 do
  begin
    Name := BluetoothLE1.DiscoveredDevices[I].DeviceName;
    if Name = '' then
      Name := 'Unknown device';
    Name := ' - '+ Name + ' - ' + BluetoothLE1.DiscoveredDevices[I].Identifier;
    if NumberOfDevices = I then
      Listbox1.Items.Add((NumberOfDevices + 1).ToString+Name)
    else
      Listbox1.Items[I] := (I + 1).ToString+Name;
  end;
end;

然后尝试使用此过程发送数据

procedure TForm6.Button4Click(Sender: TObject);
var
  Service:TBluetoothGattService;
  Charateristic:TBluetoothGattCharacteristic;
  guidServ:tguid;
  guidChar:tguid;
begin

  memo1.lines.Clear;
  memo1.lines.add('Printing');

  guidServ:=tguid.Create(ZPRINTER_SERVICE_UUID);
  guidChar:=tguid.Create(WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID);

  memo1.lines.add(guidServ.ToString);
  memo1.lines.add(guidChar.ToString);

  Service:=BluetoothLE1.DiscoveredDevices[ListBox1.ItemIndex].GetService(guidServ);
  Charateristic:=Service.GetCharacteristic(guidChar);
  Charateristic.SetValueAsString(UTF8Encode(ZPL_TEST_LABEL));
  //  Charateristic.SetValueAsString(ZPL_TEST_LABEL,false);

  memo1.lines.add('Data sent');

end;

未引发错误,但不会打印标签!

fhity93d

fhity93d1#

最后下面的代码工作!

procedure TForm6.Button4Click(Sender: TObject);
var
  Service:TBluetoothGattService;
  Charateristic:TBluetoothGattCharacteristic;
  Device:TBluetoothLEDevice;
  guidServ:tguid;
  guidChar:tguid;
begin

  memo1.lines.Clear;
  memo1.lines.add('Printing');

  guidServ:=tguid.Create(ZPRINTER_SERVICE_UUID);
  guidChar:=tguid.Create(WRITE_TO_ZPRINTER_CHARACTERISTIC_UUID);

  memo1.lines.add(guidServ.ToString);
  memo1.lines.add(guidChar.ToString);
  Device:=BluetoothLE1.DiscoveredDevices.Items[ListBox1.ItemIndex];
  Service:=Device.GetService(guidServ);
  Charateristic:=Service.GetCharacteristic(guidChar);
  Charateristic.SetValueAsString(UTF8Encode(ZPL_TEST_LABEL));

  BluetoothLE1.WriteCharacteristic(Device,Charateristic);

  memo1.lines.add('Data sent');

end;

相关问题