【问题标题】:How to receive data using bluetooth after connection (in listening mode) in Delphi android如何在Delphi android中连接后(在监听模式下)使用蓝牙接收数据
【发布时间】:2018-10-25 07:39:57
【问题描述】:

我正在 Delphi 中开发一个 android 应用程序,就像经典的蓝牙应用程序一样,我需要获取蓝牙扫描仪扫描的数据并显示在 Firemonkey 的 TEdit 组件中。

我成功连接到配对设备并扫描了第一个数据。但是对于接下来的一系列扫描,我不知道在哪里打电话给FSocket.ReceiveData。在java中,有一个等待传入流的监听函数调用,我该如何在Delphi android pls中做到这一点?

这是我的代码: rad 中示例经典蓝牙的代码 sn-p:

procedure TForm1.SendData;
 var
  ToSend: TBytes;
  LDevice: TBluetoothDevice;
  Buff: TBytes;
 begin
  if (FSocket = nil) or (ItemIndex <> ComboboxPaired.ItemIndex) then
 begin
    if ComboboxPaired.ItemIndex > -1 then
 begin
    LDevice := FPairedDevices[ComboboxPaired.ItemIndex] as TBluetoothDevice;
    DisplayR.Lines.Add(GetServiceName(ServiceGUI));
    DisplayR.GoToTextEnd;
    FSocket := LDevice.CreateClientSocket(StringToGUID(ServiceGUI), False);
   if FSocket <> nil then
     begin
      ItemIndex := ComboboxPaired.ItemIndex;
      FSocket.Connect;
      ServerConnectionTH.FServerSocket.Accept(500);
     .....

//after successful connect, go to another form , focus in a TEdit then scan,
//currently uses this function to get data

 function TForm1.getBLData: string;
    var
    Buff: TBytes;
    s:string;
  begin
     FSocket.ReceiveData;
    Buff := FSocket.ReceiveData;
    frmLoadGrid.Edit1.Text:='';
    frmLoadGrid.Edit1.Text:= TEncoding.UTF8.GetString(Buff); //enters read data 
    result:=s;
   end;

谁能帮忙...谢谢

【问题讨论】:

    标签: android delphi bluetooth


    【解决方案1】:

    欢迎来到 SO!下面是使用 ReadData 方法的示例: https://github.com/Appmethod/Appmethod115/blob/5f8ca9a9a1994d917bfbb062f9e5fd44564da6a5/Object%20Pascal/Mobile%20Samples/Device%20Sensors%20and%20Services/Bluetooth/BlueChat/uChatManager.pas.

    一个常见的想法是你应该创建一个单独的线程,它运行一个循环,直到程序终止或你自己的条件。在此循环中,线程调用ReadData 并具有FOnTextReceived 事件。 请注意,如果您想从其他线程访问可视组件,则需要使用Synchronize 方法。

    【讨论】:

    • 这就是我认为我需要的!谢谢!我马上试试
    猜你喜欢
    • 2015-08-03
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2012-08-08
    相关资源
    最近更新 更多