diff --git a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs index 3d72df8..57becd8 100644 --- a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs +++ b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs @@ -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) diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity index 8eb805a..948bd64 100755 Binary files a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity and b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/iOS/CoreBluetoothForUnity.framework/CoreBluetoothForUnity differ diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib index e37f7eb..68168c8 100755 Binary files a/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib and b/Packages/com.teach310.core-bluetooth-for-unity/Plugins/macOS/libCoreBluetoothForUnity.dylib differ diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs new file mode 100644 index 0000000..d4c013e --- /dev/null +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs @@ -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; + } + } +} diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs.meta b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs.meta new file mode 100644 index 0000000..b30d0d7 --- /dev/null +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralInitOptions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4c8e3e6332ec44f878dac785a26dabe0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs index d8ed04e..cedb0ba 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/CBCentralManager.cs @@ -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); } diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/NativeMethods.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/NativeMethods.cs index 6c9210a..3e264d1 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/NativeMethods.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/NativeMethods.cs @@ -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); diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/SafeNativeCentralManagerHandle.cs b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/SafeNativeCentralManagerHandle.cs index 4eef3e5..6fab801 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Runtime/SafeNativeCentralManagerHandle.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Runtime/SafeNativeCentralManagerHandle.cs @@ -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( diff --git a/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBCentralManagerTests.cs b/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBCentralManagerTests.cs index 7754001..3e0fe8f 100644 --- a/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBCentralManagerTests.cs +++ b/Packages/com.teach310.core-bluetooth-for-unity/Tests/Runtime/CBCentralManagerTests.cs @@ -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() { diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift index cedb521..45b2af4 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CB4UCentralManager.swift @@ -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 { diff --git a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CoreBluetoothForUnity.swift b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CoreBluetoothForUnity.swift index 4c6c542..6c5fdcd 100644 --- a/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CoreBluetoothForUnity.swift +++ b/Plugins/CoreBluetoothForUnity/Sources/CoreBluetoothForUnity/CoreBluetoothForUnity.swift @@ -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.fromOpaque(optionsPtr).takeUnretainedValue() + options = nsMutableDictionary as? [String: Any] + } + + return Unmanaged.passRetained(CB4UCentralManager(options)).toOpaque() } @_cdecl("cb4u_central_manager_release")