繁体   English   中英

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

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

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

我成功连接到配对设备并扫描了第一个数据。 但是对于接下来的一系列扫描,我不知道在哪里调用FSocket.ReceiveData 在Java中,有一个等待传入流的侦听函数调用,如何在Delphi android pls中进行呢?

这是我的代码: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;

谁能帮忙...谢谢

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

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

暂无
暂无

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

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