Search code examples
androidiosdelphifiremonkey

How to check the status of BlueTooth adapter (ON/OFF) with Delphi 10.3 (Firemonkey) App for both Android and iOS


I am writing an IOT based (BLE) Firemonkey app and I need to check that Bluetooth adapter is Enabled or not for both iOS and Android.

I've found a source here but it is only for android (like many others), but I need a cross platform solution.


Solution

  • I've found a proper solution on here.

    uses System.Bluetooth;
    
    function IsBluetoothLEAdapterEnabled: Boolean;
    var
      manager: TBluetoothLEManager;
      adapt: TBluetoothLEAdapter;
    begin
      Result := False;
      try
        manager := TBluetoothLEManager.CreateInstance;
      except
        exit;
      end;
    
      try
        adapt := manager.CurrentAdapter;
      except
        exit;
      end;
    
      try
        if adapt.State = TBluetoothAdapterState.On then
        begin
          // BluetoothLE Adapter was found
          Result := True;
          exit;
        end;
      except
        exit;
      end;
    end;
    

    end execute like below

      if IsBluetoothLEAdapterEnabled then
        ShowMessage('BluetoothLE ON')
      else
        ShowMessage('BluetoothLE OFF');