小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android實(shí)例-Delphi開發(fā)藍(lán)牙官方實(shí)例解析

 npkaida 2019-03-07


時(shí)間:2018-02-10 11:24:29                          

Android實(shí)例-Delphi開發(fā)藍(lán)牙官方實(shí)例解析(XE10+小米2+小米5)

技術(shù)分享圖片

技術(shù)分享圖片

相關(guān)資料:
1.http://blog.csdn.net/laorenshen/article/details/41149803
2.http://www.cnblogs.com/findumars/p/5149128.html

一、理清概念
1.藍(lán)牙設(shè)備:是指代有藍(lán)牙通信的手機(jī)、電腦、平板、打印機(jī)、耳機(jī)等。
2.設(shè)備名稱:是指設(shè)備打開藍(lán)牙功能后,在其他設(shè)備中顯示的名字,如圖1用的A、B、C等。
3.藍(lán)牙關(guān)態(tài):如果A手機(jī)沒有打開藍(lán)牙功能,A手機(jī)是不能使用搜索的。就算A打開藍(lán)牙,其他的設(shè)備沒打開,A也是搜索不到的,如E。
4.服務(wù)名稱:每一個(gè)設(shè)備上打開藍(lán)牙時(shí),都會提供不同的服務(wù),服務(wù)的名稱是在其他設(shè)備上顯示的。
5.服務(wù)是從哪里來的?其實(shí)打開藍(lán)牙功能,只是設(shè)備開啟此功能,但沒有實(shí)際的應(yīng)用功能。必須由某個(gè)程序或應(yīng)用建立相關(guān)的服務(wù),其他設(shè)備才能與此設(shè)備通信。

二、Delphi中提供了什么?
1.藍(lán)牙相關(guān)功能在System.Bluetooth.pas單元。
2.Bluetooth.pas提供的對像
TBluetoothManager:藍(lán)牙管理器。用于藍(lán)牙設(shè)備管理,包括發(fā)現(xiàn)藍(lán)牙設(shè)備,獲取配對設(shè)備,處理遠(yuǎn)程配對請求等功能。
TBluetoothDeviceList:是藍(lán)牙設(shè)備列表??梢酝ㄟ^TBluetoothManager.GetPairedDevices獲得配對設(shè)備列表
TBluetoothAdapter:本機(jī)藍(lán)牙設(shè)備,實(shí)現(xiàn)配對、取消配對等功能,可通過
TBluetoothDevice:遠(yuǎn)端藍(lán)牙設(shè)備,每個(gè)遠(yuǎn)端設(shè)備可以提供若干個(gè)服務(wù)
TBluetoothService:遠(yuǎn)端藍(lán)牙設(shè)備服務(wù),包括服務(wù)名和UUID
TBluetoothServiceList:服務(wù)列表??赏ㄟ^TBluetoothDevice.GetServices獲得遠(yuǎn)端設(shè)備服務(wù)列表
TBluetoothSocket:藍(lán)牙通訊套接字。

三、實(shí)例解析 

小米手機(jī)2
1.打開藍(lán)牙
2.點(diǎn)擊“發(fā)現(xiàn)設(shè)備”,過一會就會在下面的列表中顯示出所有搜索到的藍(lán)牙設(shè)備。
3.在出現(xiàn)的藍(lán)牙設(shè)備列表中,選一個(gè)。比如“小米手機(jī)5”。
4.點(diǎn)擊“配對”,這時(shí)小米手機(jī)5會收到配對信息。
5.這時(shí)配對設(shè)備列表中就會出現(xiàn)小米手機(jī)5,選中它。
6.點(diǎn)擊“服務(wù)”,出現(xiàn)N個(gè)服務(wù),但這時(shí)你不知道怎么與小米手機(jī)5通信。對的還少個(gè)服務(wù)。
7.操作小米手機(jī)5中的第三步。
8.在服務(wù)列表中選中最下方的(暈,不知道為什么名字是個(gè)空串)。
9.進(jìn)入服務(wù)演示頁面。
10.點(diǎn)“發(fā)送短信給”不好意思,我不小心點(diǎn)了二下,所以小米手機(jī)5收到二次。
11.這時(shí)小米手機(jī)5就收到了信息。 

小米手機(jī)5
1.打開藍(lán)牙
2.接受小米手機(jī)2的配對請求。
3.點(diǎn)擊“創(chuàng)建文本服務(wù)” 

PS:
1.同一個(gè)程序里包括了發(fā)送信息,與接收信息。所以程序可以在二個(gè)手機(jī)上測試。
2.現(xiàn)在只能小米手機(jī)2給小米手機(jī)5發(fā)信息,為什么呢?因?yàn)樾∶资謾C(jī)2沒有建服務(wù)呀。
3.如果小米手機(jī)5的程序退出,你會發(fā)現(xiàn)小米手機(jī)2上查看服務(wù),剛才小米手機(jī)5建立的服務(wù)不見了。
4.圖片本來很清楚,但為了文章的大小,就成了現(xiàn)在的慘不忍睹了。望大家見諒。

Delphi官方實(shí)例源碼:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Bluetooth,
  FMX.Layouts, FMX.ListBox, FMX.StdCtrls, FMX.Memo, FMX.Controls.Presentation,
  FMX.Edit, FMX.TabControl, FMX.ScrollBox;

type
  //讀取數(shù)據(jù)線程
  TServerConnectionTH = class(TThread)
  private
    { Private declarations }
    FServerSocket: TBluetoothServerSocket;
    FSocket: TBluetoothSocket;
    FData: TBytes;
  protected
    procedure Execute; override;
  public
    { Public declarations }
    constructor Create(ACreateSuspended: Boolean);
    destructor Destroy; override;
  end;

  TForm1 = class(TForm)
    ButtonDiscover: TButton;
    ButtonPair: TButton;
    ButtonUnPair: TButton;
    ButtonPairedDevices: TButton;
    DisplayR: TMemo;
    Edit1: TEdit;
    Button2: TButton;
    FreeSocket: TButton;
    Labeldiscoverable: TLabel;
    ComboBoxDevices: TComboBox;
    ComboBoxPaired: TComboBox;
    Panel1: TPanel;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabItem2: TTabItem;
    LabelNameSarver: TLabel;
    ButtonServices: TButton;
    ComboBoxServices: TComboBox;
    PanelClient: TPanel;
    LabelClient: TLabel;
    ButtonConnectToRFCOMM: TButton;
    PanelServer: TPanel;
    ButtonCloseReadingSocket: TButton;
    ButtonOpenReadingSocket: TButton;
    LabelServer: TLabel;
    Label1: TLabel;
    procedure ButtonDiscoverClick(Sender: TObject);
    procedure ButtonPairClick(Sender: TObject);
    procedure ButtonUnPairClick(Sender: TObject);
    procedure ButtonPairedDeviceClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonOpenReadingSocketClick(Sender: TObject);
    procedure ButtonConnectToRFCOMMClick(Sender: TObject);
    procedure ButtonCloseReadingSocketClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FreeSocketClick(Sender: TObject);
    function ManagerConnected:Boolean;
    function GetServiceName(GUID: string): string;
    procedure ComboBoxPairedChange(Sender: TObject);
    procedure ButtonServicesClick(Sender: TObject);
  private
    { Private declarations }
    FBluetoothManager: TBluetoothManager;
    FDiscoverDevices: TBluetoothDeviceList;
    FPairedDevices: TBluetoothDeviceList;
    FAdapter: TBluetoothAdapter;
    FData: TBytes;
    FSocket: TBluetoothSocket;
    ItemIndex: Integer;
    ServerConnectionTH: TServerConnectionTH;
    procedure DevicesDiscoveryEnd(const Sender: TObject; const ADevices: TBluetoothDeviceList);
    procedure PairedDevices;
    procedure SendData;
  public
    { Public declarations }
  end;

Const
  ServiceName = 'Basic Text Server';
  ServiceGUI = '{B62C4E8D-62CC-404B-BBBF-BF3E3BBB1378}';
var
  Form1: TForm1;

implementation

{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}
{$R *.LgXhdpiPh.fmx ANDROID}
{$R *.SmXhdpiPh.fmx ANDROID}
{$R *.Macintosh.fmx MACOS}
{$R *.iPhone4in.fmx IOS}
{$R *.Windows.fmx MSWINDOWS}
//配對(Pair)
procedure TForm1.ButtonPairClick(Sender: TObject);
begin
  if ManagerConnected then
    if ComboboxDevices.ItemIndex > -1 then
      FAdapter.Pair(FDiscoverDevices[ComboboxDevices.ItemIndex])
    else
      ShowMessage('沒有選擇的設(shè)備(No device selected)');
end;

//取消配對(UnPair)
procedure TForm1.ButtonUnPairClick(Sender: TObject);
begin
  if ManagerConnected then
    if ComboboxPaired.ItemIndex > -1 then
      FAdapter.UnPair(FPairedDevices[ComboboxPaired.ItemIndex])
    else
      ShowMessage('未選擇配對設(shè)備(No Paired device selected)');
end;

procedure TForm1.ComboBoxPairedChange(Sender: TObject);
begin
  LabelNameSarver.Text := ComboBoxPaired.Items[ComboBoxPaired.ItemIndex];
end;

procedure TForm1.PairedDevices;
var
  I: Integer;
begin
  ComboboxPaired.Clear;
  if ManagerConnected then
  begin
  FPairedDevices := FBluetoothManager.GetPairedDevices;
  if FPairedDevices.Count > 0 then
    for I:= 0 to FPairedDevices.Count - 1 do
      ComboboxPaired.Items.Add(FPairedDevices[I].DeviceName)
  else
    ComboboxPaired.Items.Add('沒有配對設(shè)備(No Paired Devices)');
  end;
end;

//配對設(shè)備(Paired Devices)
procedure TForm1.ButtonPairedDeviceClick(Sender: TObject);
begin
  PairedDevices;
  ComboboxPaired.DropDown;
end;

//服務(wù)(Services)
procedure TForm1.ButtonServicesClick(Sender: TObject);
var
  LServices: TBluetoothServiceList;
  LDevice: TBluetoothDevice;
  I: Integer;
begin
  ComboBoxServices.Clear;
  if ManagerConnected then
    if ComboboxPaired.ItemIndex > -1 then
    begin
      LDevice := FPairedDevices[ComboboxPaired.ItemIndex] as TBluetoothDevice;
      LServices := LDevice.GetServices;
      for I := 0 to LServices.Count - 1 do
        ComboBoxServices.Items.Add(LServices[I].Name + ' --> ' + GUIDToString(LServices[I].UUID));
      ComboBoxServices.ItemIndex := 0;
      ComboBoxServices.DropDown;
    end
    else
      ShowMessage('未選擇配對設(shè)備(No paired device selected)');
end;

//釋放客戶套接字(Free Client Socket)
procedure TForm1.FreeSocketClick(Sender: TObject);
begin
  FreeAndNil(FSocket);
  DisplayR.Lines.Add('客戶套接字釋放(Client socket set free)');
  DisplayR.GoToLineEnd;
end;

//清空(Clear)
procedure TForm1.Button2Click(Sender: TObject);
begin
  DisplayR.ReadOnly := False;
  DisplayR.SelectAll;
  DisplayR.DeleteSelection;
  DisplayR.ReadOnly := True;
end;

function TForm1.GetServiceName(GUID: string): string;
var
  LServices: TBluetoothServiceList;
  LDevice: TBluetoothDevice;
  I: Integer;
begin
  LDevice := FPairedDevices[ComboboxPaired.ItemIndex] as TBluetoothDevice;
  LServices := LDevice.GetServices;
  for I := 0 to LServices.Count - 1 do
  begin
    if StringToGUID(GUID) = LServices[I].UUID then
    begin
      Result := LServices[I].Name;
      break;
    end;
  end;
end;

//發(fā)送短信給 - >(Send text to ->)
procedure TForm1.ButtonConnectToRFCOMMClick(Sender: TObject);
begin
  if ManagerConnected then
    try
      SendData;
    except
      on E : Exception do
      begin
        DisplayR.Lines.Add(E.Message);
        DisplayR.GoToTextEnd;
        FreeAndNil(FSocket);
      end;
    end;
end;

//判斷藍(lán)牙的狀態(tài)
function TForm1.ManagerConnected:Boolean;
begin
  if FBluetoothManager.ConnectionState = TBluetoothConnectionState.Connected then
  begin
    Labeldiscoverable.Text := FBluetoothManager.CurrentAdapter.AdapterName;
    Result := True;
  end
  else
  begin
    Result := False;
    DisplayR.Lines.Add('沒有發(fā)現(xiàn)藍(lán)牙設(shè)備(No Bluetooth device Found)');
    DisplayR.GoToTextEnd;
  end
end;

//發(fā)送信息方法
procedure TForm1.SendData;
var
  ToSend: TBytes;
  LDevice: TBluetoothDevice;
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;
        ToSend := TEncoding.UTF8.GetBytes(Edit1.Text);
        FSocket.SendData(ToSend);
        DisplayR.Lines.Add('短信已發(fā)送(Text Sent)');
        DisplayR.GoToTextEnd;
      end
      else
        ShowMessage('Out of time -15s-');
    end
    else
      ShowMessage('未選擇配對設(shè)備(No paired device selected)');
  end
  else
  begin
    ToSend := TEncoding.UTF8.GetBytes(Edit1.Text);
    FSocket.SendData(ToSend);
    DisplayR.Lines.Add('短信已發(fā)送(Text Sent)');
    DisplayR.GoToTextEnd;
  end;
end;

//發(fā)現(xiàn)設(shè)備(Discover devices)
procedure TForm1.ButtonDiscoverClick(Sender: TObject);
begin
  ComboboxDevices.Clear;
  if ManagerConnected then
  begin
    FAdapter := FBluetoothManager.CurrentAdapter;
    FBluetoothManager.StartDiscovery(10000);
    FBluetoothManager.OnDiscoveryEnd := DevicesDiscoveryEnd;
  end;
end;

procedure TForm1.DevicesDiscoveryEnd(const Sender: TObject; const ADevices: TBluetoothDeviceList);
begin
  TThread.Synchronize(nil, procedure
  var
    I: Integer;
  begin
    FDiscoverDevices := ADevices;
    for I := 0 to ADevices.Count - 1 do
      ComboboxDevices.Items.Add(ADevices[I].DeviceName + '  -> ' + ADevices[I].Address);
    ComboboxDevices.ItemIndex := 0;
  end);
end;

//創(chuàng)建文本服務(wù)(Create text service)
procedure TForm1.ButtonOpenReadingSocketClick(Sender: TObject);
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(' - 創(chuàng)建服務(wù)(Service created): "'+ServiceName+'"');
      DisplayR.GoToTextEnd;
    except
      on E : Exception do
      begin
        DisplayR.Lines.Add(E.Message);
        DisplayR.GoToTextEnd;
      end;
    end;
  end;
end;

//刪除文字服務(wù)(Remove text service)
procedure TForm1.ButtonCloseReadingSocketClick(Sender: TObject);
begin
  if ServerConnectionTH <> nil then
  begin
    ServerConnectionTH.Terminate;
    ServerConnectionTH.WaitFor;
    FreeAndNil(ServerConnectionTH);
    DisplayR.Lines.Add(' - 服務(wù)刪除(Service removed) -');
    DisplayR.GoToTextEnd;
  end
end;

//顯示窗體時(shí),建立藍(lán)牙管理器,并檢查是否開啟
procedure TForm1.FormShow(Sender: TObject);
begin
  try
    LabelServer.Text := ServiceName;
    LabelClient.Text := 'Client of '+ServiceName;
    FBluetoothManager := TBluetoothManager.Current;
    FAdapter := FBluetoothManager.CurrentAdapter;
    if ManagerConnected then
    begin
      PairedDevices;
      ComboboxPaired.ItemIndex := 0;
    end;
  except
    on E : Exception do
    begin
      ShowMessage(E.Message);
    end;
  end;
end;

//關(guān)閉時(shí),釋放線程
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if ServerConnectionTH <> nil then
  begin
    ServerConnectionTH.Terminate;
    ServerConnectionTH.WaitFor;
    FreeAndNil(ServerConnectionTH);
  end
end;

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

//釋放資源
destructor TServerConnectionTH.Destroy;
begin
  FSocket.Free;
  FServerSocket.Free;
  inherited;
end;

//讀取線程的讀取數(shù)據(jù)方法
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.ReadData;
          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('服務(wù)器關(guān)閉套接字(Server Socket closed): ' + Msg);
            Form1.DisplayR.GoToTextEnd;
          end);
      end;
    end;
end;

end.

Android實(shí)例-Delphi開發(fā)藍(lán)牙官

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多