c# - 提示用户在 Xamarin Forms 中启用蓝牙

标签 c# xamarin xamarin.ios xamarin.android

是否有任何解决方法可用于提示用户在 Xamarin Forms/C# 中启用蓝牙?

就像带有"is"或“否”的显示警报,提示用户在未启用蓝牙的情况下打开蓝牙。

如果用户选择"is",则蓝牙已启用。

请帮助我在 Android 和 iOS 中实现这一目标!提前致谢。

最佳答案

iOS

在 iOS 上,如果不使用私有(private) API(Apple 不允许在 App Store 中使用),则无法以编程方式更改蓝牙,您最多只能使用此代码将用户重定向到蓝牙设置(请注意,它仅适用于真实设备):

// Is bluetooth enabled?
var bluetoothManager = new CoreBluetooth.CBCentralManager();
if (bluetoothManager.State == CBCentralManagerState.PoweredOff) {
    // Does not go directly to bluetooth on every OS version though, but opens the Settings on most
    UIApplication.SharedApplication.OpenUrl(new NSUrl("App-Prefs:root=Bluetooth")); 
}

请注意,此方法会阻止您的应用程序获得 App Store 批准,因为“App-Prefs:root”也被视为私有(private) API,具有以下解释:(感谢@chucky 提及那个)

Your app uses the "prefs:root=" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.

因此对于 iOS 来说,没有应用被拒绝的风险是不可能的。

安卓

Android.Bluetooth.BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
// is bluetooth enabled?
bluetoothAdapter.IsEnabled;

bluetoothAdapter.Disable();
// or
bluetoothAdapter.Enable();

此方法需要 BLUETOOTHBLUETOOTH_ADMIN 权限。

关于c# - 提示用户在 Xamarin Forms 中启用蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752085/

相关文章:

ios - 以前的 UITableView 中的项目出现在新的 UITableView 中

c# - 无法解析 CloudStorageAccount

c# - 如何设置 SQL Server Compact 数据库的相对路径?

c# - string.IsNullOrEmpty(myString) 或 string.IsNullOrWhiteSpace(myString) 是否违反 SRP 规则?

c# - 我编写的第一个应用程序崩溃了,我不知道为什么

css - Xamarin 和 CSS

c# - 处理绑定(bind) MVVM 属性中未捕获的异常

ios - Xamarin iOS : Remove old bundle identifiers

android - TensorFlow 对象检测在 Xamarin Android 上失败并出现 reshape 问题

xamarin.ios - Xamarin/Monotouch - NuGet -RequireConsent 错误