(06-01-2020, Saat: 17:34)Murat Mecit KAHRAMANLI Adlı Kullanıcıdan Alıntı: Merhaba değerli DelphiCan Ailesi,
BLE(Bluetooth Low Energy) ile çeşitli platformlarda çalışacak karşıya veri yazma ve karşıdan veri okuma uygulaması yapmaktayım.
Çeşitli karakterleri okuma ve yazma işlemleri ile Testte kullandığım ESP32 içerisinde bulunan çeşitli modları açma ve kapama yapıyorum.
Şuan Mac OS X ve Android platformlarında deneyebildiğim uygulamamda OS X tarafında çalışırken tüm okuma-yazma, karşıdan gelen
karaktere karşı cevap olarak karakterler göndermeyi başarılı bir şekilde geciktirmeden yapıyor.
PROBLEM: Android üzerinde ise uygulamanın okumasında bazen tekli okumalarda uygulamanın "Sleep()" kodu gibi bir etki ile bekleyip donduğunu,
ve 5-6 sn sonra tekrar devam ettiğini gördüm. Bu süreçte akan bir timer ile de donma(durma) sürecini takipledim. Bu durum karşıdan bir karakter
geldiğinde, ona karşı bir cevap yazıyorum. Fakat okuma işlemindeki donma neden oluyor, komponentle ilgili veya sistemle ilgili ayarlama mı yapmalıyım.
Aynı kod OS X'te problem yaşatmıyor...
Yaşamış olduğum problemle ilgili tecrübesi ve ya önerisi olabilecek arkadaşlardan çözüm rica etmekteyim...
Bu Okuma bloğum OnCharacteristicRead event'i ile bağlı çağrılıyor.
procedure TFrVestelBLE.BluetoothLE1CharacteristicRead(const Sender: TObject; const ACharacteristic: TBluetoothGattCharacteristic; AGattStatus: TBluetoothGattStatus);
begin
try
LbCurrentValue.Items.Add(IntToStr(ACharacteristic.GetValueAsInt8));
if ACharacteristic.GetValueAsInt8 = 86 then
begin
btnWriteClick(nil);
end;
except
end;
end;
Ekteki resimde Arduino ile kodlar takiplendiğinde kırmızılara cevap vermiyor uygulama donuyor. OS X te bu durum olmuyor ? :/
Merhaba
ESP32 WROM içine standart uart kodunu attığımda play store dan indirdiğim serial bluetooth apk ile Custom ID seçenekleri seçip veri alıp gönderebiliyorum.
Fakat kendi uygulamamda aşağıdaki linkin aynısı olmasına rağmen eşleşme yapamıyorum. Android uygulama esp32 ile eşleşmiyor.
string veri alıp gönderemiyorum. Her iki tarafta UUID ler aynı ESP tarafında
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
Delphi kodu ise şöyle:
Scanning: Boolean;
ScanningStart: Cardinal;
FLocationPermission: string;
procedure RequestPermissionsResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
procedure DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
procedure StartBLEDiscovery;
public
procedure cihaz_arama();
{ Public declarations }
end;
const
ScanningTime = 5000; // 30s in msecs
var
ESP32Form : TESP32Form;
Cihaz : TBluetoothLEDevice;
CihazBulundu : Boolean;
FKarakter : TBluetoothGattCharacteristic;
implementation
uses
{$IFDEF ANDROID}
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
{$ENDIF}
FMX.DialogService;
{$R *.fmx}
const //TBluetoothUUID
//SERVICE : TGUID = '{4FAFC201-1FB5-459E-8FCC-C5C9C331914B}';
// KARAKTER: TGUID = '{BEB5483E-36E1-4688-B7F5-EA07361B26A8}';
SERVICE : TGUID = '{6E400001-B5A3-F393-E0A9-E50E24DCCA9E}' ;
KARAKTER: TGUID = '{6E400001-B5A3-F393-E0A9-E50E24DCCA9E}';
//BleSerialService: TBluetoothUUID = '{FEED0001-C497-4476-A7ED-727DE7648AB1}';
procedure TESP32Form.FormCreate(Sender: TObject);
begin
{$IFDEF ANDROID}
FLocationPermission := JStringToString(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION);
{$ENDIF}
CihazBulundu := False;
end;
procedure TESP32Form.FormShow(Sender: TObject);
begin
BluetoothLE1.Enabled:=TRUE;
cihaz_arama;
end;
procedure TESP32Form.cihaz_arama();
begin
Scanning := False;
PermissionsService.RequestPermissions([FLocationPermission], RequestPermissionsResult, DisplayRationale);
end;
procedure TESP32Form.DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
begin
TDialogService.ShowMessage(' Cihazı tanımlayabilmek için konum bilgisine erişim izniniz gerekiyor!',
procedure(const AResult: TModalResult)
begin
APostRationaleProc;
end)
end;
procedure TESP32Form.RequestPermissionsResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
begin
// 1 permissions involved: ACCESS_COARSE_LOCATION
if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
StartBLEDiscovery
else
TDialogService.ShowMessage('Gerekli izin verilmediğinden Cihaz tarama başlatılamadı!');
end;
procedure TESP32Form.StartBLEDiscovery;
begin
if not Scanning then
begin
ScanningStart := TThread.GetTickCount;
BluetoothLE1.DiscoverDevices(ScanningTime);
Scanning := True;
end;
end;
procedure TESP32Form.BluetoothLE1CharacteristicRead(const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
var
s:String;
f
ouble;
begin
//ShowMessage('bi sey');
s := ACharacteristic.GetValueAsString(8, true);
// f := ACharacteristic.GetValueAsDouble(0);
Memo1.Lines.Add('read:'+S);
//Memo1.Lines.Add('read:'+floattostr(f));
// memo2.Lines.Add(ACharacteristic.GetValueAsString(2,true)) ;
// memo2.Lines.Add(IntToStr(ACharacteristic.GetValueAsInt8));
ShowMessage(s);
end;
procedure TESP32Form.BluetoothLE1CharacteristicReadRequest(
const Sender: TObject; const ACharacteristic: TBluetoothGattCharacteristic;
var AGattStatus: TBluetoothGattStatus);
begin
ShowMessage('read requet');
end;
procedure TESP32Form.BluetoothLE1CharacteristicSubscribed(const Sender: TObject;
const AClientId: string; const ACharacteristic: TBluetoothGattCharacteristic);
begin
ShowMessage('subscript');
end;
procedure TESP32Form.BluetoothLE1CharacteristicWrite(const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
begin
ShowMessage('write ici');
end;
procedure TESP32Form.BluetoothLE1Disconnect(Sender: TObject);
begin
ShowMessage('on disconnect');
end;
procedure TESP32Form.BluetoothLE1DiscoverLEDevice(const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer; const ScanResponse: TScanResponse);
var
DiscoveredDevicesCount: Integer;
DiscoveredDeviceIndex: Integer;
begin
DiscoveredDevicesCount := BluetoothLE1.DiscoveredDevices.Count;
Memo1.Lines.Add(inttostr(DiscoveredDevicesCount));
for DiscoveredDeviceIndex := 0 to DiscoveredDevicesCount - 1 do
begin
Cihaz := BluetoothLE1.DiscoveredDevices[DiscoveredDeviceIndex];
Cihaz.DiscoverServices();
if Cihaz.GetService(SERVICE)<>NIL then
begin
Cihaz.GetService(SERVICE).Characteristics;
CihazBulundu := True;
Button1.Enabled := True;
Button2.Enabled := True;
Memo1.Lines.Add(Cihaz.Identifier);
Memo1.Lines.Add(Cihaz.Address);
//ben ekledim
// Cihaz.Connect;
Break;
end;
end;
end;
procedure TESP32Form.BluetoothLE1EndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
begin
Scanning := False;
end;
procedure TESP32Form.BluetoothLE1ServiceAdded(const Sender: TObject;
const AService: TBluetoothGattService;
const AGattStatus: TBluetoothGattStatus);
begin
ShowMessage('service added');
end;
procedure TESP32Form.BluetoothLE1ServicesDiscovered(const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
var
i :integer;
RxCharact: TBluetoothGattCharacteristic;
begin
{ if AServiceList.Count > 0 then begin
// サービスを探す
for i := 0 to AServiceList.Count -1 do begin
// サービスを保持
SERVICE := AServiceList[i];
break;
end;
if SERVICE <> nil then begin
// キャラクリスティックを取得
for i := 0 to SERVICE.Characteristics.Count -1 do begin
// SerialRx の UUID
if SERVICE.Characteristics[i].UUID = KARAKTER then begin
RxCharact := SERVICE.Characteristics[i];
break;
end;
end;
if RxCharact <> nil then begin
// Notify の受信要求
SERVICE.SetCharacteristicNotification(RxCharact, true);
end;
end;
end;
}
end;
procedure TESP32Form.Button1Click(Sender: TObject);
begin
if CihazBulundu then
begin
try
//Showmessage('ac gidiyo');
FKarakter := Cihaz.GetService(SERVICE).GetCharacteristic(KARAKTER);
FKarakter.SetValueAsString(RawByteString('ac'));
Cihaz.WriteCharacteristic(FKarakter);
except on E: Exception do
ShowMessage(e.Message);
end;
end else
Showmessage('Cihazla eşleşme gerçekleşmedi');
end;
procedure TESP32Form.Button2Click(Sender: TObject);
begin
if CihazBulundu then
begin
FKarakter := Cihaz.GetService(SERVICE).GetCharacteristic(KARAKTER);
FKarakter.SetValueAsString(RawByteString('kapa'));
Cihaz.WriteCharacteristic(FKarakter);
end else
Showmessage('Cihazla eşleşme gerçekleşmedi');
end;
procedure TESP32Form.Button3Click(Sender: TObject);
begin
cihaz_arama;
end;
end.
Örnek aldığım uygulama ise:
https://www.delphican.com/showthread.php...0#pid36260
Bir türlü ilerleyemedim 4 gündür. yardımı dokunacak arkadaşlara şimdiden teşekkürler.