Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将数据(打印)发送到蓝牙(BLE)打印机_Android_Delphi_Bluetooth Lowenergy_Delphi Xe_Delphi 10.1 Berlin - Fatal编程技术网

Android 将数据(打印)发送到蓝牙(BLE)打印机

Android 将数据(打印)发送到蓝牙(BLE)打印机,android,delphi,bluetooth-lowenergy,delphi-xe,delphi-10.1-berlin,Android,Delphi,Bluetooth Lowenergy,Delphi Xe,Delphi 10.1 Berlin,我正准备将我的Android应用程序连接到一台BLE打印机(Zebra ZD420),然后打印条形码标签。 首先,我使用以下代码检索我可以使用的服务和特性 procedure TForm5.BluetoothLE1ServicesDiscovered(const Sender: TObject; const AServiceList: TBluetoothGattServiceList); var I, C: Integer; begin if AServiceList.Count

我正准备将我的Android应用程序连接到一台BLE打印机(Zebra 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}
从这里开始:

我尝试了以下代码:

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);
DiscoveryDevice事件显示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;

不会引发任何错误,但不会打印任何标签

下面的代码终于起作用了

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;

这可能是权限问题。您应该使用logcat查看器检查有哪些消息。这不是权限问题。我尝试使用Log Cat reader,但没有显示相关信息