Alper
Alper

Reputation: 1105

How to send text message from mobile app to ESP32 device via BLuetooth LE using Delphi 10.3

I am trying to create a mobile app with Delphi that will send text messages to a ESP32 device via Bluetooth LE.

I am not experienced about BLE so trying to follow this official document and trying to edit this Delphi sample app (placed under ..\Embarcadero\Studio\20.0\Samples\Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\BLEScanner) based on my needs to learn the structure in Delphi side.

On the ESP32 side, I've found a DIY project on there. But a thunkable app used for this project and I couldn't understand how to do same thing (below you can find the block view of app) with Delphi.

enter image description here

Is there anyone who has the sample app/code that can I use for this aim? Thanks right now.

Upvotes: 1

Views: 1445

Answers (1)

Alper
Alper

Reputation: 1105

I've resolved the problem with below structure;

First you should (you can do it later as well) define the UUIDs of both Service and Characteristics of the device

const
  SERVICE : TGUID = '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}';
  CHARACT : TGUID = '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}';

and define the device and its characteristic that you would like to use

Device         : TBluetoothLEDevice;
FCharacteristic: TBluetoothGattCharacteristic;

then you need to get access coarse location to get scan working (I did it on FormCreate)

{$IFDEF ANDROID}
  FLocationPermission := JStringToString(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION);
{$ENDIF}

then, you need to execute PermissionsService

PermissionsService.RequestPermissions([FLocationPermission], RequestPermissionsResult, DisplayRationale);

NOTE: You can find these codes and more on Delphi sample app (placed under ..\Embarcadero\Studio\20.0\Samples\Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\BLEScanner)

after accessing all permissions (and discovering the device) you need to write to the characteristic that you want to use;

  FCharacteristic:= Device.GetService(SERVICE).GetCharacteristic(CHARACT);
  FCharacteristic.SetValueAsString(RawByteString('command'));
  Device.WriteCharacteristic(CHARACT);

IMOPRTANT : Because of Embarcadero disables Ansichar and AnsiString in mobile compilers. (additional info in here and here) either you need to use TBytes or get Ansi support back with this magnificent library (so you can send any command to any 8-Bit IoT device (like most of them)). Rest of them is done by MCU.

Upvotes: 1

Related Questions