Socket Bluetooth Windows - Can’t connect to device:(10049)

Asked

Viewed 214 times

0

I am having socket error problems with code 10049 when I try to connect a bluetooth SSP device, serial port service, in Delphi Tokyo 10.2 in Windows environment using a Tbluetoothsocket class. Error occurs at time of connection (Tbluetoothsocket.Connect), error occurs at line 152 of Unit Main below, follows:

unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, System.Bluetooth,
  System.Bluetooth.Components, FMX.Layouts, FMX.ListBox, FMX.Edit,
  FMX.ScrollBox, FMX.Memo, FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
  FMX.ListView;

type
  TFormMain = class(TForm)
    ButtonDiscoverDevices: TButton;
    Bluetooth: TBluetooth;
    ListBoxDevices: TListBox;
    ListBoxServices: TListBox;
    ButtonOpenClose: TButton;
    Edit: TEdit;
    Panel: TPanel;
    ButtonSend: TButton;
    Timer: TTimer;
    ListView1: TListView;
    AniIndicator1: TAniIndicator;
    procedure ButtonDiscoverDevicesClick(Sender: TObject);
    procedure BluetoothDiscoveryEnd(const Sender: TObject;
      const ADeviceList: TBluetoothDeviceList);
    procedure ButtonOpenCloseClick(Sender: TObject);
    procedure ButtonSendClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure ListBoxDevicesChange(Sender: TObject);
  private
    { Private declarations }
    DeviceList: TBluetoothDeviceList;
    Device: TBluetoothDevice;
    SerialPort: TBluetoothService;
    Socket: TBluetoothSocket;
    Connected: Boolean;
    procedure ShowWaitIndicator;
    procedure HideWaitIndicator;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.fmx}

procedure TFormMain.ShowWaitIndicator;
begin
  AniIndicator1.Enabled := True;
  AniIndicator1.Visible := True;
end;

procedure TFormMain.HideWaitIndicator;
begin
  AniIndicator1.Visible := False;
  AniIndicator1.Enabled := False;
end;

procedure TFormMain.ButtonDiscoverDevicesClick(Sender: TObject);
begin
  SerialPort.Name := '';
  Device := nil;
  DeviceList := nil;
  ButtonOpenClose.Enabled := False;
  ListBoxDevices.Clear;
  ListBoxServices.Clear;

  Bluetooth.DiscoverDevices(45000); // 45s
  ShowWaitIndicator;
end;

procedure TFormMain.BluetoothDiscoveryEnd(const Sender: TObject; const ADeviceList: TBluetoothDeviceList);
var I: Integer;
begin
  HideWaitIndicator;
  ListBoxDevices.Clear;
  for I := 0 to ADeviceList.Count - 1 do
    ListBoxDevices.Items.Add(ADeviceList.Items[I].DeviceName);
  DeviceList := ADeviceList;
end;

procedure TFormMain.ListBoxDevicesChange(Sender: TObject);
var
  DeviceIndex: Integer;
  LastServices: TBluetoothServiceList;
  I: Integer;
begin
  SerialPort.Name := '';
  ListBoxServices.Clear;
  ButtonOpenClose.Enabled := False;
  DeviceIndex := ListBoxDevices.ItemIndex;
  if DeviceIndex = -1 then
    Exit;

  /// Recuperar o dispositivo selecionado
  Device := nil;
  if DeviceList <> nil then
    if (DeviceIndex >= 0) and (DeviceIndex < DeviceList.Count) then
      Device := DeviceList.Items[DeviceIndex];
  if Device = nil then
    Exit;

  ShowWaitIndicator;
  Application.ProcessMessages;
  try
    /// Parear dispositivo
    if not Device.IsPaired then
      if not Bluetooth.Pair(Device) or not Device.IsPaired then
      begin
        ListBoxDevices.ItemIndex := -1;
        raise Exception.Create('Device ''' + Device.DeviceName + ''' is not paired');
      end;

    /// Pesquisar pelo serviço de serial port
    LastServices := Device.LastServiceList;
    ListBoxServices.Clear;
    for I := 0 to LastServices.Count - 1 do
    begin
      ListBoxServices.Items.Add(LastServices[I].Name);
      if LastServices[I].Name = 'SerialPort' then
        SerialPort := LastServices[I];
    end;
    if SerialPort.Name = '' then
      raise Exception.Create('SerialPort service not found');

    ButtonOpenClose.Enabled := True;
  finally
    HideWaitIndicator;
  end;
end;

procedure TFormMain.ButtonOpenCloseClick(Sender: TObject);
begin
  if Connected then
  begin
    Connected := False;
    try
      Socket.Close;
    except
    end;
    Socket := nil;
  end
  else
  begin
    Socket := Device.CreateClientSocket(SerialPort.UUID, True);
    Socket.Connect;
    Connected := Socket.Connected;
  end;

  if Connected then
  begin
    ButtonOpenClose.Text := 'Close';
    ButtonDiscoverDevices.Enabled := False;
    ListBoxDevices.Enabled := False;
    Edit.Enabled := True;
    ButtonSend.Enabled := True;
    Timer.Enabled := True;
  end
  else
  begin
    ButtonOpenClose.Text := 'Open';
    ButtonDiscoverDevices.Enabled := True;
    ListBoxDevices.Enabled := True;
    Edit.Enabled := False;
    ButtonSend.Enabled := False;
    Timer.Enabled := False;
  end;
end;

procedure TFormMain.ButtonSendClick(Sender: TObject);
begin
  Socket.SendData(TEncoding.ANSI.GetBytes(Edit.Text));
end;

procedure TFormMain.TimerTimer(Sender: TObject);
var
  Data: TBytes;
  ItemList: TListViewItem;
begin
  Data := Socket.ReceiveData(0);
  if Data <> nil then
  begin
    ItemList := ListView1.Items.Add;
    ItemList.Text := TEncoding.ANSI.GetString(Data);
  end;
end;

end.
  • 1

    10049 Means that device does not support service you are Tring to connect to.

  • In debbug mode go to that line (151) Socket := Device.CreateClientSocket(SerialPort.UUID, True); and check whether the SerialPort.UUID has some data

  • Hello Matheus Ribeiro, yes there is the communication GUID with serial port, -> '{00001101-0000-1000-8000-00805F9B34FB}'

  • Mike Petrichenko, Yes the service is supported by the device, because with the same example for Android Works, it Connects normally.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.