简体   繁体   English

在Delphi Android中如何在连接后(使用监听模式)使用蓝牙接收数据

[英]How to receive data using bluetooth after connection (in listening mode) in Delphi android

I am developing an android app in Delphi like a classic bluetooth app wherein I need to get the data scanned by a bluetooth scanner and display in a TEdit component in Firemonkey. 我正在像经典的蓝牙应用程序一样在Delphi中开发一个android应用程序,其中我需要获取由蓝牙扫描仪扫描的数据并显示在Firemonkey的TEdit组件中。

I was successful in connecting to the paired device and getting the 1st data scanned. 我成功连接到配对设备并扫描了第一个数据。 But for the next series of scanning , I dont know where to call the FSocket.ReceiveData . 但是对于接下来的一系列扫描,我不知道在哪里调用FSocket.ReceiveData In java, there is a listening function call that waits for incoming streams, how do I do it in Delphi android pls? 在Java中,有一个等待传入流的侦听函数调用,如何在Delphi android pls中进行呢?

Here is my code: code snippet from sample classic bluetooth in rad: 这是我的代码:rad中的经典蓝牙示例代码片段:

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;

Can anyone pls help... Thank you 谁能帮忙...谢谢

Welcome to SO! 欢迎来到SO! Here' s example of using ReadData method: https://github.com/Appmethod/Appmethod115/blob/5f8ca9a9a1994d917bfbb062f9e5fd44564da6a5/Object%20Pascal/Mobile%20Samples/Device%20Sensors%20and%20Services/Bluetooth/BlueChat/uChatManager.pas . 这是使用ReadData方法的示例: https : //github.com/Appmethod/Appmethod115/blob/5f8ca9a9a1994d917bfbb062f9e5fd44564da6a5/Object%20Pascal/Mobile%20Samples/Device%20Sensors%20and%20Services/Bluetooth/BlueChat/uChatManager

A common idea is you should create a separate thread, which run a cycle until program is terminated or your own conditions. 一个普遍的想法是,您应该创建一个单独的线程,该线程运行一个周期,直到程序终止或您自己的条件终止为止。 In this cycle thread calls ReadData and has FOnTextReceived event. 在此循环中,线程调用ReadData并具有FOnTextReceived事件。 Please note you need to use Synchronize method if you want to access visual components from other threads. 请注意,如果要从其他线程访问可视组件,则需要使用Synchronize方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM