From 71edf8c2990390934aea7b67a8a9853bf28008d5 Mon Sep 17 00:00:00 2001 From: teach310 Date: Mon, 30 Oct 2023 23:07:04 +0900 Subject: [PATCH] explicit interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit デフォルト引数があるインタフェースの場合、明示しないと関数名のミスに気づきづらい --- .../SampleLightControl_Central.cs | 16 +++---- .../SampleLightControl_Peripheral.cs | 8 ++-- .../Samples/12_Debug/SampleDebug_Central.cs | 44 +++++++++---------- .../12_Debug/SampleDebug_Peripheral.cs | 16 +++---- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Central.cs b/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Central.cs index 8c8231a..985af28 100644 --- a/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Central.cs +++ b/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Central.cs @@ -39,7 +39,7 @@ void Update() } } - public void DidUpdateState(CBCentralManager central) + void ICBCentralManagerDelegate.DidUpdateState(CBCentralManager central) { if (central.State == CBManagerState.PoweredOn) { @@ -48,7 +48,7 @@ public void DidUpdateState(CBCentralManager central) } } - public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi) + void ICBCentralManagerDelegate.DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi) { _header.SetStateText("Connecting..."); _peripheral = peripheral; @@ -57,27 +57,27 @@ public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral periphe central.Connect(peripheral); } - public void DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral) + void ICBCentralManagerDelegate.DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral) { _header.SetStateText("Connected"); peripheral.DiscoverServices(new string[] { SampleLightControl_Data.ServiceUUID }); } - public void DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) + void ICBCentralManagerDelegate.DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) { _header.SetStateText("Scanning..."); _lightControlCharacteristic = null; central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID }); } - public void DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) + void ICBCentralManagerDelegate.DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) { _header.SetStateText("Scanning..."); _lightControlCharacteristic = null; central.ScanForPeripherals(new string[] { SampleLightControl_Data.ServiceUUID }); } - public void DidDiscoverServices(CBPeripheral peripheral, CBError error) + void ICBPeripheralDelegate.DidDiscoverServices(CBPeripheral peripheral, CBError error) { if (error != null) { @@ -91,7 +91,7 @@ public void DidDiscoverServices(CBPeripheral peripheral, CBError error) } } - public void DidDiscoverCharacteristics(CBPeripheral peripheral, CBService service, CBError error) + void ICBPeripheralDelegate.DidDiscoverCharacteristics(CBPeripheral peripheral, CBService service, CBError error) { if (error != null) { @@ -110,7 +110,7 @@ public void DidDiscoverCharacteristics(CBPeripheral peripheral, CBService servic } } - public void DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) + void ICBPeripheralDelegate.DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { if (error != null) { diff --git a/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Peripheral.cs b/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Peripheral.cs index b91822a..c152fc9 100644 --- a/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Peripheral.cs +++ b/Assets/CoreBluetooth/Samples/01_LightControl/SampleLightControl_Peripheral.cs @@ -35,7 +35,7 @@ void StartAdvertising() _peripheralManager.StartAdvertising(options); } - public void DidUpdateState(CBPeripheralManager peripheral) + void ICBPeripheralManagerDelegate.DidUpdateState(CBPeripheralManager peripheral) { if (peripheral.State == CBManagerState.PoweredOn) { @@ -56,7 +56,7 @@ public void DidUpdateState(CBPeripheralManager peripheral) } } - public void DidAddService(CBPeripheralManager peripheral, CBService service, CBError error) + void ICBPeripheralManagerDelegate.DidAddService(CBPeripheralManager peripheral, CBService service, CBError error) { if (error != null) { @@ -67,7 +67,7 @@ public void DidAddService(CBPeripheralManager peripheral, CBService service, CBE StartAdvertising(); } - public void DidStartAdvertising(CBPeripheralManager peripheral, CBError error) + void ICBPeripheralManagerDelegate.DidStartAdvertising(CBPeripheralManager peripheral, CBError error) { if (error != null) { @@ -78,7 +78,7 @@ public void DidStartAdvertising(CBPeripheralManager peripheral, CBError error) _header.SetStateText("Advertising..."); } - public void DidReceiveWriteRequests(CBPeripheralManager peripheral, CBATTRequest[] requests) + void ICBPeripheralManagerDelegate.DidReceiveWriteRequests(CBPeripheralManager peripheral, CBATTRequest[] requests) { var firstRequest = requests[0]; foreach (var request in requests) diff --git a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs index b725929..4d9deb9 100644 --- a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs +++ b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs @@ -22,16 +22,7 @@ void Start() _centralManager = new CBCentralManager(this, initOptions); } - public void DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi) - { - Debug.Log($"[DidDiscoverPeripheral] peripheral: {peripheral} rssi: {rssi}"); - _peripheral = peripheral; - peripheral.Delegate = this; - central.StopScan(); - central.Connect(peripheral); - } - - public void DidUpdateState(CBCentralManager central) + void ICBCentralManagerDelegate.DidUpdateState(CBCentralManager central) { Debug.Log($"[DidUpdateState] {central.State}"); if (central.State == CBManagerState.PoweredOn) @@ -41,24 +32,33 @@ public void DidUpdateState(CBCentralManager central) } } - public void DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral) + void ICBCentralManagerDelegate.DidDiscoverPeripheral(CBCentralManager central, CBPeripheral peripheral, int rssi) + { + Debug.Log($"[DidDiscoverPeripheral] peripheral: {peripheral} rssi: {rssi}"); + _peripheral = peripheral; + peripheral.Delegate = this; + central.StopScan(); + central.Connect(peripheral); + } + + void ICBCentralManagerDelegate.DidConnectPeripheral(CBCentralManager central, CBPeripheral peripheral) { Debug.Log($"[DidConnectPeripheral] peripheral: {peripheral}"); Debug.Log($"[DidConnectPeripheral] mtu: {peripheral.GetMaximumWriteValueLength(CBCharacteristicWriteType.WithResponse)}"); peripheral.DiscoverServices(new string[] { _serviceUUID }); } - public void DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) + void ICBCentralManagerDelegate.DidDisconnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) { Debug.Log($"[DidDisconnectPeripheral] peripheral: {peripheral} error: {error}"); } - public void DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) + void ICBCentralManagerDelegate.DidFailToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, CBError error) { Debug.Log($"[DidFailToConnectPeripheral] peripheral: {peripheral} error: {error}"); } - public void DidDiscoverServices(CBPeripheral peripheral, CBError error) + void ICBPeripheralDelegate.DidDiscoverServices(CBPeripheral peripheral, CBError error) { Debug.Log($"[DidDiscoverServices] peripheral: {peripheral}"); if (error != null) @@ -74,7 +74,7 @@ public void DidDiscoverServices(CBPeripheral peripheral, CBError error) } } - public void DidDiscoverCharacteristics(CBPeripheral peripheral, CBService service, CBError error) + void ICBPeripheralDelegate.DidDiscoverCharacteristics(CBPeripheral peripheral, CBService service, CBError error) { Debug.Log($"[DidDiscoverCharacteristics] peripheral: {peripheral} service: {service}"); if (error != null) @@ -104,7 +104,7 @@ public void DidDiscoverCharacteristics(CBPeripheral peripheral, CBService servic } } - public void DidUpdateValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) + void ICBPeripheralDelegate.DidUpdateValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { Debug.Log($"[DidUpdateValueForCharacteristic] characteristic: {characteristic}"); if (error != null) @@ -117,7 +117,7 @@ public void DidUpdateValueForCharacteristic(CBPeripheral peripheral, CBCharacter Debug.Log($"Data: {str}"); } - public void DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) + void ICBPeripheralDelegate.DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { Debug.Log($"[DidWriteValueForCharacteristic] characteristic: {characteristic}"); if (error != null) @@ -127,12 +127,12 @@ public void DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteri } } - public void IsReadyToSendWriteWithoutResponse(CBPeripheral peripheral) + void ICBPeripheralDelegate.IsReadyToSendWriteWithoutResponse(CBPeripheral peripheral) { Debug.Log($"[IsReadyToSendWriteWithoutResponse] {peripheral}"); } - public void DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) + void ICBPeripheralDelegate.DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { Debug.Log($"[DidUpdateNotificationStateForCharacteristic] characteristic: {characteristic}"); if (error != null) @@ -142,7 +142,7 @@ public void DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral, } } - public void DidReadRSSI(CBPeripheral peripheral, int rssi, CBError error) + void ICBPeripheralDelegate.DidReadRSSI(CBPeripheral peripheral, int rssi, CBError error) { Debug.Log($"[DidReadRSSI] rssi: {rssi}"); if (error != null) @@ -152,12 +152,12 @@ public void DidReadRSSI(CBPeripheral peripheral, int rssi, CBError error) } } - public void DidUpdateName(CBPeripheral peripheral) + void ICBPeripheralDelegate.DidUpdateName(CBPeripheral peripheral) { Debug.Log($"[DidUpdateName] {peripheral}"); } - public void DidModifyServices(CBPeripheral peripheral, CBService[] services) + void ICBPeripheralDelegate.DidModifyServices(CBPeripheral peripheral, CBService[] services) { var serviceIds = services.Select(s => s.UUID.ToString()).ToArray(); Debug.Log($"[DidModifyServices] services count: {services.Length} serviceIds: {string.Join(", ", serviceIds)}"); diff --git a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Peripheral.cs b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Peripheral.cs index 2b64289..c0b0306 100644 --- a/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Peripheral.cs +++ b/Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Peripheral.cs @@ -25,7 +25,7 @@ void Start() _disposables.Add(_peripheralManager); } - public void DidUpdateState(CBPeripheralManager peripheral) + void ICBPeripheralManagerDelegate.DidUpdateState(CBPeripheralManager peripheral) { Debug.Log($"[DidUpdateState] {peripheral.State}"); if (peripheral.State == CBManagerState.PoweredOn) @@ -44,7 +44,7 @@ public void DidUpdateState(CBPeripheralManager peripheral) } } - public void DidAddService(CBPeripheralManager peripheral, CBService service, CBError error) + void ICBPeripheralManagerDelegate.DidAddService(CBPeripheralManager peripheral, CBService service, CBError error) { Debug.Log($"[DidAddService] peripheral: {peripheral} service: {service} error: {error}"); if (error == null) @@ -58,29 +58,29 @@ public void DidAddService(CBPeripheralManager peripheral, CBService service, CBE } } - public void DidStartAdvertising(CBPeripheralManager peripheral, CBError error) + void ICBPeripheralManagerDelegate.DidStartAdvertising(CBPeripheralManager peripheral, CBError error) { Debug.Log($"[DidStartAdvertising] peripheral: {peripheral} error: {error}"); } - public void DidSubscribeToCharacteristic(CBPeripheralManager peripheral, CBCentral central, CBCharacteristic characteristic) + void ICBPeripheralManagerDelegate.DidSubscribeToCharacteristic(CBPeripheralManager peripheral, CBCentral central, CBCharacteristic characteristic) { Debug.Log($"[DidSubscribeToCharacteristic] peripheral: {peripheral} central: {central} characteristic: {characteristic}"); _central = central; } - public void DidUnsubscribeFromCharacteristic(CBPeripheralManager peripheral, CBCentral central, CBCharacteristic characteristic) + void ICBPeripheralManagerDelegate.DidUnsubscribeFromCharacteristic(CBPeripheralManager peripheral, CBCentral central, CBCharacteristic characteristic) { Debug.Log($"[DidUnsubscribeFromCharacteristic] peripheral: {peripheral} central: {central} characteristic: {characteristic}"); _central = null; } - public void IsReadyToUpdateSubscribers(CBPeripheralManager peripheral) + void ICBPeripheralManagerDelegate.IsReadyToUpdateSubscribers(CBPeripheralManager peripheral) { Debug.Log($"[IsReadyToUpdateSubscribers] {peripheral}"); } - public void DidReceiveReadRequest(CBPeripheralManager peripheral, CBATTRequest request) + void ICBPeripheralManagerDelegate.DidReceiveReadRequest(CBPeripheralManager peripheral, CBATTRequest request) { if (request.Characteristic.UUID != _characteristicUUID) { @@ -100,7 +100,7 @@ public void DidReceiveReadRequest(CBPeripheralManager peripheral, CBATTRequest r Debug.Log($"[DidReceiveReadRequest] {System.Text.Encoding.UTF8.GetString(request.Value)}"); } - public void DidReceiveWriteRequests(CBPeripheralManager peripheral, CBATTRequest[] requests) + void ICBPeripheralManagerDelegate.DidReceiveWriteRequests(CBPeripheralManager peripheral, CBATTRequest[] requests) { var firstRequest = requests[0]; foreach (var request in requests)