如何从蓝牙HC-05 arduino接收数据到Delphi XE android?

时间:2016-01-29 18:21:25

标签: delphi bluetooth arduino

我已经制作了Android应用程序,这是经典蓝牙示例的修改,可以通过蓝牙与Delphi XE发送和接收文本,并已经与另一个Android测试。然后我用arduino和HC-05进行测试两者都可以配对但是arduino中的字符串没有出现在应用程序DisplayR: TMemo中。

这是我的服务器代码:

{Server}

constructor TServerConnectionTH.Create(ACreateSuspended: Boolean);
begin
  inherited;
end;

destructor TServerConnectionTH.Destroy;
begin
  FSocket.Free;
  FServerSocket.Free;
  inherited;
end;

procedure TServerConnectionTH.execute;
var
  ASocket: TBluetoothSocket;
  Msg: string;
begin
  while not Terminated do
    try
      ASocket := nil;
      while not Terminated and (ASocket = nil) do
        ASocket := FServerSocket.Accept(100);
      if(ASocket <> nil) then
      begin
        FSocket := ASocket;
        while not Terminated do
        begin
          FData := ASocket.ReceiveData;
          if length(FData) > 0 then
            Synchronize(procedure
              begin
                Form1.DisplayR.Lines.Add(TEncoding.UTF8.GetString(FData));
                Form1.DisplayR.GoToTextEnd;
              end);
          sleep(100);
        end;
      end;
    except
      on E : Exception do
      begin
        Msg := E.Message;
        Synchronize(procedure
          begin
            Form1.DisplayR.Lines.Add('Server Socket closed: ' + Msg);
            Form1.DisplayR.GoToTextEnd;
          end);
      end;
    end;
end;

{End Server}

//create sevice server
procedure TForm1.btnopenreadingsocketClick;
begin
  if (ServerConnectionTH = nil) and ManagerConnected then
  begin
    try
      FAdapter := FBluetoothManager.CurrentAdapter;
      ServerConnectionTH := TServerConnectionTH.Create(True);
      ServerConnectionTH.FServerSocket := FAdapter.CreateServerSocket(ServiceName, StringToGUID(ServiceGUI), False);
      ServerConnectionTH.Start;
      DisplayR.Lines.Add(' - Service created: "'+ServiceName+'"');
      DisplayR.GoToTextEnd;
    except
      on E : Exception do
      begin
        DisplayR.Lines.Add(E.Message);
        DisplayR.GoToTextEnd;
      end;
    end;
  end;
end;

arduino代码:

#include <SoftwareSerial.h> // impor library softwareserial
SoftwareSerial BlueSer(8, 9); // RX, TX
int led=13;

void setup() {
  BlueSer.begin(9600);
  BlueSer.println("Bluetooth ready, press 1 or 0 to on/off LED");
  pinMode(led,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (BlueSer.available()){

      BlueSer.println("LED on");

      BlueSer.println("LED off");

}
delay(100);
}

我如何从蓝牙HC-05接收字符串数据并显示在DisplayR: TMemo

0 个答案:

没有答案