Skip to content

Commit cca997a

Browse files
jpobstjonathanpeppers
authored andcommitted
[Mono.Android] Fix incorrect Bluetooth enumification (#6214)
Fixes: https://developercommunity2.visualstudio.com/t/xamarin-android-bluetooth-the-advertisin/841102 Fixes: #2595 Update enumification of various `Android.Bluetooth.LE.AdvertisingSetParameters.Builder` and `Android.Bluetooth.BluetoothDevice` methods to use the `Android.Bluetooth.BluetoothPhy` enum instead of the `Android.Bluetooth.LE.ScanSettingsPhy` enum, introduced in e33eb53. Backwards compatibility is achieved by creating overloads for the existing methods in `Additions`, and marking them as `[Obsolete]`. The API "breakage" is because the `[Register]` attribute has moved from the incorrect methods to the correct methods. As the signature change is an enum, the marshalled type is still an `int`, so moving the `[Register]` should not cause issues. For example: // Current [Register] public void DoSomething (BadEnum value) { ... } // Fixed [Register] public void DoSomething (GoodEnum value) { ... } [Obsolete ("Use DoSomething(GoodEnum)")] public void DoSomething (BadEnum value) => DoSomething ((GoodEnum)value); Note: this is only generally save to do when `DoSomething()` is a *non-`virtual`* method (i.e. Java `final` method). Fortunately, this is the case with the methods involved here.
1 parent ed5ad12 commit cca997a

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if ANDROID_26
2+
3+
namespace Android.Bluetooth.LE
4+
{
5+
public sealed partial class AdvertisingSetParameters
6+
{
7+
public sealed partial class Builder
8+
{
9+
// These methods were obsoleted as a warning in API-31
10+
[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
11+
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
12+
public unsafe Android.Bluetooth.LE.AdvertisingSetParameters.Builder? SetPrimaryPhy ([global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy primaryPhy)
13+
=> SetPrimaryPhy ((Android.Bluetooth.BluetoothPhy) primaryPhy);
14+
15+
[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
16+
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
17+
public unsafe Android.Bluetooth.LE.AdvertisingSetParameters.Builder? SetSecondaryPhy ([global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy secondaryPhy)
18+
=> SetSecondaryPhy ((Android.Bluetooth.BluetoothPhy) secondaryPhy);
19+
}
20+
}
21+
}
22+
23+
#endif
24+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#if ANDROID_26
2+
3+
namespace Android.Bluetooth
4+
{
5+
public sealed partial class BluetoothDevice
6+
{
7+
// These methods were obsoleted as a warning in API-31
8+
[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
9+
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
10+
public unsafe Android.Bluetooth.BluetoothGatt? ConnectGatt (Android.Content.Context? context, bool autoConnect, Android.Bluetooth.BluetoothGattCallback? @callback, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.BluetoothTransports transport, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy phy)
11+
=> ConnectGatt (context, autoConnect, @callback, transport, (Android.Bluetooth.BluetoothPhy) phy);
12+
13+
[global::System.Obsolete ("This method has the wrong enumeration. Use the version that takes an 'Android.Bluetooth.BluetoothPhy' instead.")]
14+
[global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android26.0")]
15+
public unsafe Android.Bluetooth.BluetoothGatt? ConnectGatt (Android.Content.Context? context, bool autoConnect, Android.Bluetooth.BluetoothGattCallback? @callback, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.BluetoothTransports transport, [global::Android.Runtime.GeneratedEnum] Android.Bluetooth.LE.ScanSettingsPhy phy, Android.OS.Handler? handler)
16+
=> ConnectGatt (context, autoConnect, @callback, transport, (Android.Bluetooth.BluetoothPhy) phy, handler);
17+
}
18+
}
19+
20+
#endif
21+

src/Mono.Android/Mono.Android.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@
172172
<Compile Include="Android.App\UsesLibraryAttribute.cs" />
173173
<Compile Include="Android.App\UsesPermissionAttribute.cs" />
174174
<Compile Include="Android.App.Admin\DevicePolicyManager.cs" />
175+
<Compile Include="Android.Bluetooth\AdvertisingSetParametersBuilder.cs" />
176+
<Compile Include="Android.Bluetooth\BluetoothDevice.cs" />
175177
<Compile Include="Android.Bluetooth\BluetoothGattServer.cs" />
176178
<Compile Include="Android.Bluetooth\BluetoothGattServerCallback.cs" />
177179
<Compile Include="Android.Content\ContentProvider.cs" />

src/Mono.Android/methodmap.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,8 +2189,8 @@
21892189
26, android.bluetooth, BluetoothGatt, setPreferredPhy, phyOptions, Android.Bluetooth.BluetoothPhyOption
21902190
26, android.bluetooth.le, ScanSettings.Builder, setPhy, phy, Android.Bluetooth.BluetoothPhy
21912191
26, android.bluetooth.le, ScanSettings, getPhy, return, Android.Bluetooth.LE.ScanSettingsPhy
2192-
26, android.bluetooth.le, AdvertisingSetParameters.Builder, setPrimaryPhy, primaryPhy, Android.Bluetooth.LE.ScanSettingsPhy
2193-
26, android.bluetooth.le, AdvertisingSetParameters.Builder, setSecondaryPhy, secondaryPhy, Android.Bluetooth.LE.ScanSettingsPhy
2192+
26, android.bluetooth.le, AdvertisingSetParameters.Builder, setPrimaryPhy, primaryPhy, Android.Bluetooth.BluetoothPhy
2193+
26, android.bluetooth.le, AdvertisingSetParameters.Builder, setSecondaryPhy, secondaryPhy, Android.Bluetooth.BluetoothPhy
21942194
26, android.bluetooth.le, AdvertisingSetParameters.Builder, setTxPowerLevel, txPowerLevel, Android.Bluetooth.LE.AdvertiseTxPower
21952195
26, android.bluetooth.le, ScanResult, ctor, primaryPhy, Android.Bluetooth.LE.ScanSettingsPhy
21962196
26, android.bluetooth.le, ScanResult, ctor, secondaryPhy, Android.Bluetooth.LE.ScanSettingsPhy
@@ -2205,7 +2205,7 @@
22052205
26, android.bluetooth, BluetoothGattServerCallback, onPhyUpdate, txPhy, Android.Bluetooth.LE.ScanSettingsPhy
22062206
26, android.bluetooth, BluetoothGattServerCallback, onPhyUpdate, rxPhy, Android.Bluetooth.LE.ScanSettingsPhy
22072207
26, android.bluetooth, BluetoothGattServerCallback, onPhyUpdate, status, Android.Bluetooth.GattStatus
2208-
26, android.bluetooth, BluetoothDevice, connectGatt, phy, Android.Bluetooth.LE.ScanSettingsPhy
2208+
26, android.bluetooth, BluetoothDevice, connectGatt, phy, Android.Bluetooth.BluetoothPhy
22092209
26, android.bluetooth, BluetoothDevice, connectGatt, transport, Android.Bluetooth.BluetoothTransports
22102210
26, android.bluetooth, BluetoothGattCallback, onPhyRead, txPhy, Android.Bluetooth.LE.ScanSettingsPhy
22112211
26, android.bluetooth, BluetoothGattCallback, onPhyRead, rxPhy, Android.Bluetooth.LE.ScanSettingsPhy
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
Compat issues with assembly Mono.Android:
2+
CannotRemoveAttribute : Attribute 'Android.Runtime.RegisterAttribute' exists on 'Android.Bluetooth.BluetoothDevice.ConnectGatt(Android.Content.Context, System.Boolean, Android.Bluetooth.BluetoothGattCallback, Android.Bluetooth.BluetoothTransports, Android.Bluetooth.LE.ScanSettingsPhy)' in the contract but not the implementation.
3+
CannotRemoveAttribute : Attribute 'Android.Runtime.RegisterAttribute' exists on 'Android.Bluetooth.BluetoothDevice.ConnectGatt(Android.Content.Context, System.Boolean, Android.Bluetooth.BluetoothGattCallback, Android.Bluetooth.BluetoothTransports, Android.Bluetooth.LE.ScanSettingsPhy, Android.OS.Handler)' in the contract but not the implementation.
4+
CannotRemoveAttribute : Attribute 'Android.Runtime.RegisterAttribute' exists on 'Android.Bluetooth.LE.AdvertisingSetParameters.Builder.SetPrimaryPhy(Android.Bluetooth.LE.ScanSettingsPhy)' in the contract but not the implementation.
5+
CannotRemoveAttribute : Attribute 'Android.Runtime.RegisterAttribute' exists on 'Android.Bluetooth.LE.AdvertisingSetParameters.Builder.SetSecondaryPhy(Android.Bluetooth.LE.ScanSettingsPhy)' in the contract but not the implementation.

0 commit comments

Comments
 (0)