unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, nrcommbox, nrclasses, nrbt, ExtCtrls, nratcmd, nrgsm,
  nrlogfile, nrcomm;

type
  TForm1 = class(TForm)
    nrDeviceBox1: TnrDeviceBox;
    CheckBox1: TCheckBox;
    Button1: TButton;
    GroupBox1: TGroupBox;
    chAuthenticated: TCheckBox;
    chRemembered: TCheckBox;
    chUnknown: TCheckBox;
    chConnected: TCheckBox;
    chInquiry: TCheckBox;
    Button2: TButton;
    Button3: TButton;
    chForceAuth: TCheckBox;
    chAddWiz: TCheckBox;
    chSkipServices: TCheckBox;
    Memo1: TMemo;
    nrBT1: TnrBT;
    Label1: TLabel;
    ListBox1: TListBox;
    Label2: TLabel;
    procedure CheckBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure nrBT1Disconnect(Sender: TObject);
    procedure nrBT1Connect(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure nrBT1FatalError(Sender: TObject; ErrorCode, Detail: Cardinal;
      ErrorMsg: String; var RaiseException: Boolean);
  private   { Private declarations }
  public    { Public declarations }
    procedure ApplyCheckBoxes;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  try
    nrBT1.Active := CheckBox1.Checked;
  except
    CheckBox1.Checked := nrBT1.Active;
    raise;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nrBT1.ConfigDialog;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ApplyCheckBoxes;
  nrBT1.Update;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ApplyCheckBoxes;
  nrBT1.SelectDeviceDialog('Additional information',chForceAuth.Checked,chAddWiz.Checked,chSkipServices.Checked);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with nrBT1 do begin
    chAuthenticated.Checked := bteAuthenticated in DeviceEnum;
    chRemembered.Checked    := bteRemembered in DeviceEnum;
    chUnknown.Checked       := bteUnknown in DeviceEnum;
    chConnected.Checked     := bteConnected in DeviceEnum;
    chInquiry.Checked       := bteIssueInquiry in DeviceEnum;
  end;
end;

procedure TForm1.ApplyCheckBoxes;
begin
  with nrBT1 do begin
    if chAuthenticated.Checked
      then DeviceEnum := DeviceEnum + [bteAuthenticated]
      else DeviceEnum := DeviceEnum - [bteAuthenticated];

    if chRemembered.Checked
      then DeviceEnum := DeviceEnum + [bteRemembered]
      else DeviceEnum := DeviceEnum - [bteRemembered];

    if chUnknown.Checked
      then DeviceEnum := DeviceEnum + [bteUnknown]
      else DeviceEnum := DeviceEnum - [bteUnknown];

    if chConnected.Checked
      then DeviceEnum := DeviceEnum + [bteConnected]
      else DeviceEnum := DeviceEnum - [bteConnected];

    if chInquiry.Checked
      then DeviceEnum := DeviceEnum + [bteIssueInquiry]
      else DeviceEnum := DeviceEnum - [bteIssueInquiry];

  end;
end;

procedure TForm1.nrBT1Disconnect(Sender: TObject);
begin
  CheckBox1.Checked := False;
  Memo1.Enabled := False;
  Memo1.Color := clBtnFace;
  Listbox1.Items.Add('BT device is DISconnected !!!');
end;

procedure TForm1.nrBT1Connect(Sender: TObject);
begin
  Memo1.Enabled := True;
  Memo1.Color := clWindow;
  Listbox1.Items.Add('BT device is connected !!!');
  nrBT1.SendString('AT'#13);
  Memo1.SetFocus;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  nrBT1.Active := False;
end;

procedure TForm1.nrBT1FatalError(Sender: TObject; ErrorCode,
  Detail: Cardinal; ErrorMsg: String; var RaiseException: Boolean);
begin
  Listbox1.Items.Add('BT Error '+ ErrorMsg);
end;

end.
