unit Bluetooth_MD300CI;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, System.Bluetooth,
System.Generics.Collections,
System.Bluetooth.Components,
Vcl.StdCtrls;
type
TLucas =
record
Geraetenummer: Integer;
end;
Tmainfrm =
class(TForm)
Scan_Button: TButton;
Beenden_Btn: TButton;
BluetoothLE1: TBluetoothLE;
ListBox1: TListBox;
Subscribe_Btn: TButton;
Label1: TLabel;
procedure Scan_ButtonClick(Sender: TObject);
procedure Beenden_BtnClick(Sender: TObject);
procedure Subscribe_BtnClick(Sender: TObject);
procedure BluetoothLE1EndDiscoverServices(
const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
procedure BluetoothLE1CharacteristicRead(
const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
procedure BluetoothLE1DiscoverLEDevice(
const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Device_List: TList<System.Bluetooth.TBluetoothLEDevice>;
FBLEDevice: TBluetoothLEDevice;
SPOService: TBluetoothGattService;
Read_Charact: TBluetoothGattCharacteristic;
ServiceList: TBluetoothGattServiceList;
public
const
ServiceUUID = '
';
CharactUUID = '
';
CI218_SERVICE: TBluetoothUUID = '
{00001822-0000-1000-8000-00805F9B34FB}';
CI218_Characteristic
: TBluetoothUUID = '
{00002A60-0000-1000-8000-00805F9B34FB}';
end;
var
mainfrm: Tmainfrm;
implementation
{$R *.dfm}
procedure Tmainfrm.Beenden_BtnClick(Sender: TObject);
begin
application.Terminate;
end;
procedure Tmainfrm.Subscribe_BtnClick(Sender: TObject);
Var
SelectedText:
String;
Device_Identifier:
String;
Current_Device: TBluetoothLEDevice;
begin
Scan_Button.Enabled := true;
if (ListBox1.ItemIndex >= 0)
then
begin
Current_Device := ListBox1.Items.Objects[ListBox1.ItemIndex]
as TBluetoothLEDevice;
if Current_Device.Connect
then
begin
showMessage('
connected');
end
else
begin
end;
SPOService :=
nil;
Read_Charact :=
nil;
ServiceList :=
nil;
BluetoothLE1.DiscoverServices(Current_Device);
//SPOService := BluetoothLE1.GetService(Current_Device, CI218_SERVICE);
//Read_Charact := BluetoothLE1.GetCharacteristic(SPOService,
// CI218_Characteristic);
// ServiceList := BluetoothLE1.GetServices(Current_Device);
// BluetoothLE1.SubscribeToCharacteristic(Current_Device, Read_Charact);
// if SPOService <> nil then
// begin
// ShowMessage('Service found');
// // FHRMeasurementGattCharact := BluetoothLE1.GetCharacteristic(FHRGattService, HRMEASUREMENT_CHARACTERISTIC);
// // FBodySensorLocationGattCharact := BluetoothLE1.GetCharacteristic(FHRGattService, BODY_SENSOR_LOCATION_CHARACTERISTIC);
// end
// else
// begin
// ShowMessage('Service not found');
//
// end;
//end
//else
//ShowMessage('Could not connect device');
end
else
ShowMessage('
Please select a device from the List');
end;
procedure Tmainfrm.Scan_ButtonClick(Sender: TObject);
begin
Scan_Button.Enabled := false;
BluetoothLE1.DiscoverDevices(5000, [CI218_SERVICE]);
end;
procedure Tmainfrm.BluetoothLE1CharacteristicRead(
const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
var
LAmbient: Double;
LTarget: Double;
LAccel: Double;
LHum: Integer;
LHHum: Double;
begin
// if ACharacteristic.UUIDName = 'UUID_IRT_DATA' then
// begin
// LAmbient := extractAmbientTemperature(ACharacteristic);
// LTarget := extractTargetTemperature(ACharacteristic, LAmbient);
// EdAmbient.Text := Format('%f °C',[LAmbient]);
// EdTarget.Text := Format('%f °C',[LTarget]);
// end
// else if ACharacteristic.UUIDName = 'UUID_ACC_DATA' then
// begin
// LAccel := ACharacteristic.GetValueAsInt8(0) /64;
// EdAccelX.Text := Format('%f',[LAccel]);
// LAccel := ACharacteristic.GetValueAsInt8(1) /64;
// end;
//
//
//
end;
procedure Tmainfrm.BluetoothLE1DiscoverLEDevice(
const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
var
Device: System.Bluetooth.TBluetoothLEDevice;
I: Integer;
DCount: Integer;
NumberOfDevices: Integer;
ListBoxText:
String;
begin
DCount := BluetoothLE1.DiscoveredDevices.Count;
NumberOfDevices := ListBox1.Count;
for I := 0
to DCount - 1
do
begin
Device := BluetoothLE1.DiscoveredDevices[I];
// if (Device.DeviceName = 'MD300CI218') then
begin
if Not Device_List.
Contains(Device)
Then
begin
Device_List.Add(Device);
// Name := 'Unknown device';
// Name := Name + BluetoothLE1.DiscoveredDevices[I].Identifier;
// ListBoxText := '- ' + Device.DeviceName + ' - ' + Device.Identifier;
// ListBox1.Items.Add(ListBoxText);
ListBox1.Items.AddObject(Device.DeviceName, Device);
end;
// if (List.IndexOf(Device_Count - 1) <> I) then
// begin
// List.Insert(Device_Count, I);
// // device_array[NumberOfDevices] := I;
// Name := ' - ' + Name + ' - ' + BluetoothLE1.DiscoveredDevices[I]
// .Identifier;
// ListBox1.Items.Add(IntToStr(NumberOfDevices + 1) + Name);
// Device_Count := Device_Count + 1;
// end;
// end
// else
// begin
// List.Insert(Device_Count, I);
// Name := ' - ' + Name + ' - ' + BluetoothLE1.DiscoveredDevices[I]
// .Identifier;
// ListBox1.Items.Add(IntToStr(NumberOfDevices + 1) + Name);
// Device_Count := Device_Count + 1;
//
// end;
end;
end;
end;
//
procedure Tmainfrm.BluetoothLE1EndDiscoverServices(
const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
var
I, J: Integer;
begin
// for I:= 0 to AServiceList.Count - 1 do
// begin
// if AServiceList[I].UUIDName = 'UUID_IRT_Serv' then //Temperature service
// begin
// for J := 0 to AServiceList[I].Characteristics.Count - 1 do
// begin
// if AServiceList[I].Characteristics[J].UUIDName = 'UUID_IRT_CONF' then
// begin
// AServiceList[I].Characteristics[J].SetValueAsUInt8(1);
// BluetoothLE1.WriteCharacteristic(FcurrentDevice, AServiceList[I].Characteristics[J])
// end
// else if AServiceList[I].Characteristics[J].UUIDName = 'UUID_IRT_DATA' then
// BluetoothLE1.SubscribeToCharacteristic(FCurrentDevice, AServiceList[I].Characteristics[J])
// end;
// end
// else if AServiceList[I].UUIDName = 'UUID_ACC_SERV' then //Accelerometer
// begin
// for J := 0 to AServiceList[I].Characteristics.Count - 1 do
// begin
// if AServiceList[I].Characteristics[J].UUIDName = 'UUID_ACC_CONF' then
// begin
// AServiceList[I].Characteristics[J].SetValueAsUInt8(1);
// BluetoothLE1.WriteCharacteristic(FcurrentDevice, AServiceList[I].Characteristics[J])
// end
// else if AServiceList[I].Characteristics[J].UUIDName = 'UUID_ACC_DATA' then
// BluetoothLE1.SubscribeToCharacteristic(FCurrentDevice, AServiceList[I].Characteristics[J])
// end;
// end
//
// end;
end;
procedure Tmainfrm.FormCreate(Sender: TObject);
begin
Device_List := TList<System.Bluetooth.TBluetoothLEDevice>.Create;
end;
procedure Tmainfrm.FormDestroy(Sender: TObject);
begin
Device_List.Free;
BluetoothLE1.CancelDiscovery();
end;
end.