Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Created June 12, 2024 10:46
Show Gist options
  • Save DelphiWorlds/e9c73f32516624a61e1433a3601578fb to your computer and use it in GitHub Desktop.
Save DelphiWorlds/e9c73f32516624a61e1433a3601578fb to your computer and use it in GitHub Desktop.
Check Bluetooth state on iOS
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls,
Macapi.ObjectiveC, Macapi.Bluetooth,
iOSapi.Foundation, iOSapi.CocoaTypes;
type
CBConnectionEvent = NSInteger;
CBCentralManagerDelegate = interface(IObjectiveC)
['{3A116076-D6AB-4437-BD82-0326D057DBC5}']
[MethodName('centralManager:connectionEventDidOccur:forPeripheral:')]
procedure centralManagerConnectionEventDidOccur(central: CBCentralManager; connectionEventDidOccur: CBConnectionEvent; forPeripheral: CBPeripheral); cdecl;
[MethodName('centralManager:didConnectPeripheral:')]
procedure centralManagerDidConnectPeripheral(central: CBCentralManager; didConnectPeripheral: CBPeripheral); cdecl;
[MethodName('centralManager:didDisconnectPeripheral:error:')]
procedure centralManagerDidDisconnectPeripheral(central: CBCentralManager; didDisconnectPeripheral: CBPeripheral; error: NSError); cdecl;
[MethodName('centralManager:didDiscoverPeripheral:advertisementData:RSSI:')]
procedure centralManagerDidDiscoverPeripheral(central: CBCentralManager; didDiscoverPeripheral: CBPeripheral; advertisementData: NSDictionary; RSSI: NSNumber); cdecl;
[MethodName('centralManager:didFailToConnectPeripheral:error:')]
procedure centralManagerDidFailToConnectPeripheral(central: CBCentralManager; didFailToConnectPeripheral: CBPeripheral; error: NSError); cdecl;
[MethodName('centralManager:didUpdateANCSAuthorizationForPeripheral:')]
procedure centralManagerDidUpdateANCSAuthorizationForPeripheral(central: CBCentralManager; didUpdateANCSAuthorizationForPeripheral: CBPeripheral); cdecl;
procedure centralManagerDidUpdateState(central: CBCentralManager); cdecl;
[MethodName('centralManager:willRestoreState:')]
procedure centralManagerWillRestoreState(central: CBCentralManager; willRestoreState: NSDictionary); cdecl;
end;
TCBCentralManagerDelegate = class(TOCLocal, CBCentralManagerDelegate)
public
{ CBCentralManagerDelegate }
[MethodName('centralManager:connectionEventDidOccur:forPeripheral:')]
procedure centralManagerConnectionEventDidOccur(central: CBCentralManager; connectionEventDidOccur: CBConnectionEvent;
forPeripheral: CBPeripheral); cdecl;
[MethodName('centralManager:didConnectPeripheral:')]
procedure centralManagerDidConnectPeripheral(central: CBCentralManager; didConnectPeripheral: CBPeripheral); cdecl;
[MethodName('centralManager:didDisconnectPeripheral:error:')]
procedure centralManagerDidDisconnectPeripheral(central: CBCentralManager; didDisconnectPeripheral: CBPeripheral; error: NSError); cdecl;
[MethodName('centralManager:didDiscoverPeripheral:advertisementData:RSSI:')]
procedure centralManagerDidDiscoverPeripheral(central: CBCentralManager; didDiscoverPeripheral: CBPeripheral; advertisementData: NSDictionary;
RSSI: NSNumber); cdecl;
[MethodName('centralManager:didFailToConnectPeripheral:error:')]
procedure centralManagerDidFailToConnectPeripheral(central: CBCentralManager; didFailToConnectPeripheral: CBPeripheral; error: NSError); cdecl;
[MethodName('centralManager:didUpdateANCSAuthorizationForPeripheral:')]
procedure centralManagerDidUpdateANCSAuthorizationForPeripheral(central: CBCentralManager;
didUpdateANCSAuthorizationForPeripheral: CBPeripheral); cdecl;
procedure centralManagerDidUpdateState(central: CBCentralManager); cdecl;
[MethodName('centralManager:willRestoreState:')]
procedure centralManagerWillRestoreState(central: CBCentralManager; willRestoreState: NSDictionary); cdecl;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FCentralManager: CBCentralManager;
FCentralManagerDelegate: TCBCentralManagerDelegate;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
{ TCBCentralManagerDelegate }
procedure TCBCentralManagerDelegate.centralManagerConnectionEventDidOccur(central: CBCentralManager; connectionEventDidOccur: CBConnectionEvent;
forPeripheral: CBPeripheral);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidConnectPeripheral(central: CBCentralManager; didConnectPeripheral: CBPeripheral);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidDisconnectPeripheral(central: CBCentralManager; didDisconnectPeripheral: CBPeripheral;
error: NSError);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidDiscoverPeripheral(central: CBCentralManager; didDiscoverPeripheral: CBPeripheral;
advertisementData: NSDictionary; RSSI: NSNumber);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidFailToConnectPeripheral(central: CBCentralManager; didFailToConnectPeripheral: CBPeripheral;
error: NSError);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidUpdateANCSAuthorizationForPeripheral(central: CBCentralManager;
didUpdateANCSAuthorizationForPeripheral: CBPeripheral);
begin
end;
procedure TCBCentralManagerDelegate.centralManagerDidUpdateState(central: CBCentralManager);
begin
case central.state of
CBManagerStateUnknown: ShowMessage('unknown');
CBManagerStateResetting: ShowMessage('resetting');
CBManagerStateUnsupported: ShowMessage('unsupported');
CBManagerStateUnauthorized: ShowMessage('unauthorized');
CBManagerStatePoweredOff: ShowMessage('off');
CBManagerStatePoweredOn: ShowMessage('on');
end;
end;
procedure TCBCentralManagerDelegate.centralManagerWillRestoreState(central: CBCentralManager; willRestoreState: NSDictionary);
begin
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
FCentralManagerDelegate := TCBCentralManagerDelegate.Create;
FCentralManager := TCBCentralManager.Wrap(TCBCentralManager.Alloc.initWithDelegate(FCentralManagerDelegate.GetObjectID, 0));
end;
end.
@DelphiWorlds
Copy link
Author

CBCentralManagerDelegate has been redeclared here because the one in the Delphi source is either wrong and/or out of date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment