Testing BluetoothLE on Delphi DX 10
Today i do some test about TBluetooth LE component on Delphi Seattle on different devices.
Device | BT LE as Client | BT LE as Server (pheriferial mode) |
---|---|---|
Mac Book Pro OSX 10.11.6 | ||
iPhone 6s iOS 9.3 | ||
Samsung S4 Android lollipop | ||
PC Windows 10 Pro – Sitecom BT | ||
iPhone 4s |
what i found is that all function well on Android (Samsung S4/lollipop), Mac Book Pro (OSX 10.3), iPhone (iPhone 6s/iOS 9.3)
On my Windows system the LE does not function at on my pc (Intel i7/ Windows 10 / SiteCom Bluetooth). But with classic Bluetooth all function well, i don’t kwon if the problem is my blu tooth interface ?!?!. Soon i’ll try with another bluetooth interface.
What i found is that using BT LE between my Mac and my iPhone the connection is very stable, and when i go away from my pc and back automatically the restart the comunication without checking bluetooth connection.
On Android all function as client but when i try to invoke this method “btLE.GetGattServer;” i got an error like “Your device can’t work as pheriferial mode”.
I create a server app for my MacBook and it function very well only using this method:
procedure TForm7.FormCreate(Sender: TObject);
var
ggs: TBluetoothGattServer;
service: TBluetoothGattService;
pp : TBluetoothPropertyFlags;
chara: TBluetoothGattCharacteristic;
//
// TBluetoothProperty = (Broadcast, ExtendedProps, Notify, Indicate, Read, Write, WriteNoResponse, SignedWrite);
// TBluetoothPropertyFlags = set of TBluetoothProperty;
begin
btLE.Enabled := True;
ggs := btLE.GetGattServer;
if ggs<>nil then
Begin
service := ggs.CreateService(StringToGUID('{27EFDF1D-2D6F-4AB2-9783-E288DB80D5F1}'),TBluetoothServiceType.Primary);
chara := ggs.CreateCharacteristic(service,StringToGUID('{90C16494-5AD1-48AD-B567-952359AAB3F1}'),[
// TBluetoothProperty.Broadcast,
// TBluetoothProperty.ExtendedProps,
TBluetoothProperty.Notify,
TBluetoothProperty.Indicate,
TBluetoothProperty.Read,
TBluetoothProperty.Write],'RemodeLEServer');
// chara.UUIDName := 'RemodeLEServer';
ggs.AddService(service);
btLE.Enabled := True;
end
else
showmessage('impossibile creare il service ');
end;
for display the recived data from server :
procedure TForm7.btLECharacteristicWriteRequest(const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
var AGattStatus: TBluetoothGattStatus; const AValue: TArray);
var
e: TEncoding;
begin
e := TEncoding.Create;
// e.GetString(AValue);
// memo1.lines.Add(e.GetString(AValue));
memo1.lines.Add(ACharacteristic.GetValueAsString);
AGattStatus := TBluetoothGattStatus.Success;
end;
On the client side :
i put a button to discover bt devices
procedure TForm6.Button1Click(Sender: TObject);
var
I: Integer;
j: Integer;
begin
btLE.Enabled := True;
Adev := nil;
AChar := nil;
btLE.DiscoverDevices(5000);
end;
and copiled the method about the end of discovering devices
procedure TForm6.btLEEndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
var
I: Integer;
j: Integer;
m: Integer;
begin
// log
Memo1.Lines.Add(ADeviceList.Count.ToString + ' devices discovered:');
for I := 0 to ADeviceList.Count - 1 do
begin
Memo1.Lines.Add(i.ToString + ADeviceList[I].DeviceName + ADeviceList[I].Address);
ADeviceList[I].DiscoverServices;
for j:=0 to ADeviceList[I].Services.Count-1 do
Begin
Memo1.Lines.Add(i.ToString + '/' + j.ToString + ' ' + ADeviceList[I].Services[J].UUID.ToString);
For m:=0 to ADeviceList[I].Services[J].Characteristics.Count-1 do
Begin
Memo1.Lines.Add(i.ToString + '/' + j.ToString + ' ' + ADeviceList[I].Services[J].Characteristics[m].UUID.ToString);
if (ADeviceList[I].Services[J].UUID.ToString = '{27EFDF1D-2D6F-4AB2-9783-E288DB80D5F1}')and(ADeviceList[I].Services[J].Characteristics[m].UUID.ToString = '{90C16494-5AD1-48AD-B567-952359AAB3F1}') then
Begin
Adev := ADeviceList[I];
AChar := ADeviceList[I].Services[J].Characteristics[m];
If ADeviceList[I].Connect then
Begin
Memo1.Lines.Add('connesso!!!');
ADeviceList[I].Services[J].Characteristics[m].SetValueAsString('ciao');
if ADeviceList[I].WriteCharacteristic(ADeviceList[I].Services[J].Characteristics[m]) then
Begin
memo1.Lines.Add('ho scritto');
End
Else
Begin
memo1.Lines.Add('non ho scritto');
End;
//ADeviceList[I].Disconnect;
End
Else
Begin
Memo1.Lines.Add('non connesso');
End;
End;
End;
End;
end;
showmessage('done');
end;