Android Instance-delphi Development Bluetooth official case analysis (xe10+ Xiaomi Millet 5)
Related information:
1.http://blog.csdn.net/laorenshen/article/details/41149803
2.http://www.cnblogs.com/findumars/p/5149128.html
I. Clarify the concept
1. Bluetooth device: Refers to the mobile phone with Bluetooth communication, computer, tablet, printer, headphones and so on.
2. Device name: Refers to the device to turn on the Bluetooth function, the name displayed in other devices, 1 with a, B, C and so on.
3. Blue Trismus State: If a phone does not turn on the Bluetooth function, a mobile phone is not available for search. Even if a turns on Bluetooth, other devices are not turned on, a is also not searchable, such as E.
4. Service Name: When Bluetooth is turned on on each device, different services are provided, and the name of the service is displayed on other devices.
5. Where does the service come from? Actually turn on the Bluetooth function, only the device turns on this function, but there is no actual application function. The related service must be established by a program or app to communicate with the device.
Ii. what is provided in Delphi?
1. Bluetooth related function in System.Bluetooth.pas unit.
2.bluetooth.pas provides the right image
Tbluetoothmanager: Bluetooth manager. For Bluetooth device management, including discovering Bluetooth devices, acquiring paired devices, processing remote pairing requests, and more.
Tbluetoothdevicelist: is a list of Bluetooth devices. List of paired devices can be obtained via tbluetoothmanager.getpaireddevices
Tbluetoothadapter: Native Bluetooth device for pairing, de-pairing and other functions, can be
Tbluetoothdevice: Remote Bluetooth device, each remote device can provide a number of services
Tbluetoothservice: Remote Bluetooth device service, including service name and UUID
Tbluetoothservicelist: List of services. A list of remote device services can be obtained via tbluetoothdevice.getservices
Tbluetoothsocket: Bluetooth communication socket.
Third, the case analysis
Xiaomi Mobile 2
1. Turn on Bluetooth
2. Click on "Discover Device" and in the following list you will be shown all the bluetooth devices you have searched for.
3. In the list of Bluetooth devices that appear, select one. such as "Millet mobile phone 5."
4. Tap "Pairing", then Xiaomi phone 5 will receive pairing information.
5. The Xiaomi phone 5 will appear in the Pairing device list, select it.
6. Click "Services", there are n services, but you do not know how to communicate with Xiaomi mobile phone 5. There are fewer services available.
7. Operation Millet Mobile 5 in the third step.
8. In the list of services, select the bottom (halo, do not know why the name is an empty string).
9. Go to the Service Demo page.
10. Click "Send text message" Sorry, I accidentally point two, so Xiaomi phone 5 received two times.
11. Then Xiaomi mobile phone 5 received the message.
Xiaomi Mobile 5
1. Turn on Bluetooth
2. Accept the pairing request of Xiaomi phone 2.
3. Click "Create Text Service"
Ps:
1. The same program includes sending information and receiving information. So the program can be tested on two phones.
2. Now only Xiaomi mobile phone 2 to Xiaomi mobile phone 5 messages, why? Because Xiaomi mobile phone 2 did not build services AH.
3. If Xiaomi phone 5 program quits, you will find Xiaomi phone 2 on the view service, just now Xiaomi mobile phone 5 established service is missing.
4. The picture was very clear, but for the size of the article, it is now a miserable. I hope you forgive me.
Delphi Official Example Source code:
Unit unit1;interfaceuses 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//Read Data thread Tserverconnectionth = Class (TThread) private {private declarations} Fserversocket:tblu Etoothserversocket; 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}//Pairing (pair) procedure Tform1.buttonpairclick (sender:tobject); begin If managerconnected then if Comboboxdevices.itemindex >-1 then Fadapter.pair (fdiscoverdevices[comboboxdevice S.itemindex]) Else showmessage (' No Devices selected (no device selected) '); end;//de-pairing (Unpair) procedure Tform1.buttonunpairclick (sender:tobject); begin if managerconnected then if Comboboxpaired.itemindex >-1 then Fadapter.unpair (fpaired Devices[comboboxpaired.itemindex]) Else showmessage (' No paired devices (no paired device selected) '); End;procedure tform1.com Boboxpairedchange (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 (Fpaireddevic Es[i]. devicename) Else ComboboxPaired.Items.Add (' No pairing device (no paired Devices) '); end;end;//pairing device (paired DevICES) Procedure Tform1.buttonpaireddeviceclick (sender:tobject); begin paireddevices; comboboxpaired.dropdown;end;//Service (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[comboboxpaire D.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 (' No paired devices (no paired device selected) '); end;//release client socket (free client socket) procedure Tform1.frees Ocketclick (sender:tobject); begin Freeandnil (Fsocket); DISPLAYR.LINES.ADD (' Client socket set free '); displayr.gotolineend;end;//empty (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;//Send SMS-> (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;//to determine the status function tform1.managerconnected:boolean;begin if fbluetoothmanager.connectionstate = of Bluetooth Tbluetoothconnectionstate.connected then BEgin Labeldiscoverable.text: = FBluetoothManager.CurrentAdapter.AdapterName; Result: = True; End ELSE begin Result: = False; DISPLAYR.LINES.ADD (' No Bluetooth device Found ' found); Displayr.gototextend; endend;//Send Information method procedure Tform1.senddata;var tosend:tbytes; Ldevice:tbluetoothdevice;begin if (Fsocket = nil) or (ItemIndex <> comboboxpaired.itemindex) THEN BEGIN if Com Boboxpaired.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 (' SMS sent (text Sent) '); Displayr.gototextend; End Else ShowmesSage (' Out of time-15s-'); End Else ShowMessage (' No paired devices (no paired device selected) '); End ELSE begin tosend: = TEncoding.UTF8.GetBytes (Edit1.text); Fsocket.senddata (Tosend); DISPLAYR.LINES.ADD (' SMS sent (text Sent) '); Displayr.gototextend; end;end;//Discovery Equipment (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;//Creating text service (Create Text services) 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 ('-Create service created): ' ' +servicename+ ' '); Displayr.gototextend; Except on E:exception do begin DISPLAYR.LINES.ADD (e.message); Displayr.gototextend; End End end;end;//Delete Text Services (remove text Service) procedure Tform1.buttonclosereadingsocketclick (sender:tobject); begin IF Serverconnectionth <> Nil then begin serverconnectionth.terminate; Serverconnectionth.waitfor; Freeandnil (Serverconnectionth); DISPLAYR.LINES.ADD ('-Service removed-'); Displayr.gototextend; endend;//when the form is displayed, set up the Bluetooth manager and check to see if the procedure Tform1.formshow is turned on (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;//off, Release thread procedure tform1.formclose (Sender:tobject; var action:tcloseaction); Begin if Serverconnectionth <> Nil then begin serverconnectionth.terminate; Serverconnectionth.waitfor; Freeandnil (Serverconnectionth); EndEnd; {tserverconnection}constructor tserverconnectionth.create (acreatesuspended:boolean); Begin inherited;end;// Release resources destructor Tserverconnectionth.destroy;begin Fsocket.free; Fserversocket.free; inherited;end;//read data method of reading thread procedure Tserverconnectionth.execute;var asocket:tbluetoothsocket; Msg:string;begin while not Terminated do try asocket: = Nil; While not Terminated and (Asocket = nil) does Asocket: = fserversocket.accept (100); if (asocket <> nil) THEN BEGIN fsocket: = Asocket; While not Terminated does begin FData: = Asocket.readdata; If Length (FData) > 0 Then Synchronize (Procedure begin FORM1.DISPLAYR.LINES.ADD (T Encoding.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.
Android Instance-delphi Development Bluetooth official case analysis (xe10+ Xiaomi Millet 5)