简体   繁体   中英

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.

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 . In java, there is a listening function call that waits for incoming streams, how do I do it in Delphi android pls?

Here is my code: code snippet from sample classic bluetooth in 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! 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 .

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. Please note you need to use Synchronize method if you want to access visual components from other threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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