Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class SampleDebug_Central : MonoBehaviour, ICBCentralManagerDelegate, ICB

void Start()
{
_centralManager = new CBCentralManager(this);
var initOptions = new CBCentralInitOptions() { ShowPowerAlert = true };
_centralManager = new CBCentralManager(this, initOptions);
}

public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi)
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
public class CBCentralInitOptions
{
public static readonly string ShowPowerAlertKey = "kCBInitOptionShowPowerAlert";
public bool? ShowPowerAlert { get; set; } = null;

public CBCentralInitOptions()
{
}

internal NSMutableDictionary ToNativeDictionary()
{
var dict = new NSMutableDictionary();
if (ShowPowerAlert.HasValue)
{
using var value = new NSNumber(ShowPowerAlert.Value);
dict.SetValue(ShowPowerAlertKey, value.Handle);
}
return dict;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ public ICBCentralManagerDelegate Delegate

NativeCentralManagerProxy _nativeCentralManagerProxy;

public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null)
public CBCentralManager(ICBCentralManagerDelegate centralDelegate = null, CBCentralInitOptions options = null)
{
_handle = SafeNativeCentralManagerHandle.Create();
if (options == null)
{
_handle = SafeNativeCentralManagerHandle.Create();
}
else
{
using var optionsDict = options.ToNativeDictionary();
_handle = SafeNativeCentralManagerHandle.Create(optionsDict.Handle);
}
Delegate = centralDelegate;
_nativeCentralManagerProxy = new NativeCentralManagerProxy(_handle, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int identifierSize
internal static extern int cb4u_central_maximum_update_value_length(SafeNativeCentralHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNativeCentralManagerHandle cb4u_central_manager_new();
internal static extern SafeNativeCentralManagerHandle cb4u_central_manager_new(IntPtr options);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_central_manager_release(IntPtr handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ internal class SafeNativeCentralManagerHandle : SafeHandleZeroOrMinusOneIsInvali

SafeNativeCentralManagerHandle() : base(true) { }

internal static SafeNativeCentralManagerHandle Create()
static SafeNativeCentralManagerHandle Create(IntPtr options)
{
var instance = NativeMethods.cb4u_central_manager_new();
var instance = NativeMethods.cb4u_central_manager_new(options);
instance.RegisterHandlers();
return instance;
}

internal static SafeNativeCentralManagerHandle Create(Foundation.SafeNSMutableDictionaryHandle options)
{
return Create(options.DangerousGetHandle());
}

internal static SafeNativeCentralManagerHandle Create()
{
return Create(IntPtr.Zero);
}

void RegisterHandlers()
{
NativeMethods.cb4u_central_manager_register_handlers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public void Create()
Assert.That(centralManager, Is.Not.Null);
}

[Test]
public void CreateWithOptions()
{
var options = new CBCentralInitOptions() { ShowPowerAlert = true };
using var centralManager = new CBCentralManager(null, options);
Assert.That(centralManager, Is.Not.Null);
}

[Test]
public void Release()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class CB4UCentralManager : NSObject {
public var didDiscoverPeripheralHandler: CB4UCentralManagerDidDiscoverPeripheralHandler?
public var didUpdateStateHandler: CB4UCentralManagerDidUpdateStateHandler?

public override init() {
public init(_ options: [String: Any]? = nil) {
super.init()

centralManager = CBCentralManager(delegate: self, queue: nil)
centralManager = CBCentralManager(delegate: self, queue: nil, options: options)
}

func selfPointer() -> UnsafeMutableRawPointer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ public func cb4u_central_maximum_update_value_length(_ centralPtr: UnsafeRawPoin
}

@_cdecl("cb4u_central_manager_new")
public func cb4u_central_manager_new() -> UnsafeMutableRawPointer {
return Unmanaged.passRetained(CB4UCentralManager()).toOpaque()
public func cb4u_central_manager_new(_ optionsPtr: UnsafeRawPointer?) -> UnsafeMutableRawPointer {
var options: [String: Any]? = nil
if let optionsPtr = optionsPtr {
let nsMutableDictionary = Unmanaged<NSMutableDictionary>.fromOpaque(optionsPtr).takeUnretainedValue()
options = nsMutableDictionary as? [String: Any]
}

return Unmanaged.passRetained(CB4UCentralManager(options)).toOpaque()
}

@_cdecl("cb4u_central_manager_release")
Expand Down