[英]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.