Jump to content
Sign in to follow this  
Rollo62

Howto call initWithDelegate and CBCentralManagerOptionShowPowerAlertKey

Recommended Posts

Hi there,

 

I wanted to call the Bluetooth initWithDictionary and its option CBCentralManagerOptionShowPowerAlertKey;

There is little information out there howto create the options for this call in the right way.

  CBCentralManager = interface(NSObject)
  ['{587D1855-5B53-43DA-91AF-AF9D93FFDB9F}']
    ...
    function initWithDelegate(delegate: id; queue: dispatch_queue_t; options: NSDictionary): id; cdecl; overload;

In XCode that looks quite straightforward, but what about Fmx ?

let opts = [CBCentralManagerOptionShowPowerAlertKey: false]
let manager = CBCentralManager(delegate: self, 
                               queue: nil, 
                               options: opts)

 

Info's like this doesn't help much

http://codeverge.com/embarcadero.delphi.firemonkey/help-rewrite-objective-c-proced/1046099

 

Using NSUserDefaults to access the info list properties seems not to work, returning nil.

But is NSUserDefaults the right place to look for those keys ?

 

var
    LDefaults: NSUserDefaults; 
    LDict: NSDictionary;       

begin
  LDefaults := TNSUserDefaults.Wrap(TNSUserDefaults.OCClass.standardUserDefaults);
  LDict     := LDefaults.dictionaryForKey( CBCentralManagerOptionShowPowerAlertKey ); // ! This (LDict) is nil

if Assigned(lDict) then
      FCentralManager := TCBCentralManager.Wrap(
          TCBCentralManager.Alloc.initWithDelegate(
              GetObjectID,
              0,
              LDict  
              ))

I used this function to get the key constant

function CBCentralManagerOptionShowPowerAlertKey : NSString;
begin
  Result := CocoaNSStringConst(libCoreBluetooth, 'CBCentralManagerOptionShowPowerAlertKey');
end;

Other ways to construct this NSDictionary look to ugly to be true:

  LDict     := TNSDictionary.Wrap(
                   TNSDictionary.OCClass.dictionaryWithObjectsAndKeys(
                      <<This is Pointer to object a nil-terminated list>>));

 

Maybe somebody can help with some useful hints, to find the right way to access such system options  ?

 

Share this post


Link to post

 

13 hours ago, Rollo62 said:

Other ways to construct this NSDictionary look to ugly to be true:

They're ugly, but they are true. In your case however you should need only use dictionaryWithObject (since there is only one value), thus:

var
  LDict: NSDictionary;
  LBooleanValue: Pointer;
begin
  LBooleanValue := TNSNumber.OCClass.numberWithBool(True);
  LDict := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(LBooleanValue, NSObjectToID(CBCentralManagerOptionShowPowerAlertKey)));
end;

 

  • Thanks 1

Share this post


Link to post

@Dave Nottage

Thanks, I will check that.

Never know when to use what and why excactly when it comes to iOS objects :classic_smile:

 

Edited by Rollo62

Share this post


Link to post

Yes it works with that configuration, the LDict is assigned,

and even better, the Bluetooth alert appears.

 

Thanks again Dave.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×