在Xamarin.iOS中检查蓝牙状态(打开/关闭)

问题描述 投票:1回答:2

我正在寻找一种能够获取设备蓝牙状态的功能,因此我可以告诉应用程序用户蓝牙的打开或关闭。

当前代码一直在说蓝牙即使打开也已关闭。任何帮助和指导表示赞赏!

public bool CheckBluetoothStatus()
        {
            bool status;

            if (state == CBCentralManagerState.PoweredOn)
            {
                status = true;
                bluetoothEnabledLbl.Text = "Bluetooth enabled";
                bluetoothEnabledAdviceLbl.Text = "Consider turning Bluetooth off if not in use or check to see if all connected devices are recognisable";

                return status;
            }
            else
            {
                status = false;
                bluetoothEnabledLbl.Text = "Bluetooth not enabled";

                return status;
            }
        }
c# iphone bluetooth xamarin.ios
2个回答
0
投票

尝试此操作,如果没有任何更改,可能是由于设备版本:

private CBCentralManagerState state; 
public bool CheckBluetoothStatus()
    {
        bool status;

        if (state == CBCentralManagerState.PoweredOn)
        {
            status = true;
            bluetoothEnabledLbl.Text = "Bluetooth enabled";
            bluetoothEnabledAdviceLbl.Text = "Consider turning Bluetooth off if not in use or check to see if all connected devices are recognisable";

        }
        else
        {
            status = false;
            bluetoothEnabledLbl.Text = "Bluetooth not enabled";

        }
            return status;
    }

0
投票

开发两个[[Xamarin iOS蓝牙应用程序时需要注意两个要点

  • First
,您需要在物理设备中运行它,无法在模拟器设备中测试蓝牙功能。如果那样,您将始终获得带有共享代码的控制台日志:
2020-02-28 13:32:38.310442 + 0800 AppIOSBluetooth [36757:5394891]蓝牙未启用

  • 第二
,您需要在info.plist文件中添加蓝牙应用程序的权限。首次运行应用程序时,将显示权限弹出窗口。 enter image description here

但是,在info.plist中,您可以按照以下步骤轻松添加Bluetooth的权限,而会忘记另一个最重要的权限:

<key>NSBluetoothPeripheralUsageDescription</key> <string>Add BlueTooth Peripheral Permission</string>

对于蓝牙来说,这还不够。您还需要添加另一个权限:

<key>NSBluetoothAlwaysUsageDescription</key> <string>use Bluetooth</string>

此外,有关在Xamarin iOS中使用蓝牙的an offical API doc,您可以看看。

public override void ViewDidLoad () { base.ViewDidLoad (); // Perform any additional setup after loading the view, typically from a nib. myDel = new MySimpleCBCentralManagerDelegate(); var myMgr = new CBCentralManager(myDel, DispatchQueue.CurrentQueue); } public class MySimpleCBCentralManagerDelegate : CBCentralManagerDelegate { override public void UpdatedState(CBCentralManager mgr) { if (mgr.State == CBCentralManagerState.PoweredOn) { Console.WriteLine("Consider turning Bluetooth off if not in use or check to see if all connected devices are recognisable"); //Passing in null scans for all peripherals. Peripherals can be targeted by using CBUIIDs CBUUID[] cbuuids = null; mgr.ScanForPeripherals(cbuuids); //Initiates async calls of DiscoveredPeripheral //Timeout after 30 seconds var timer = new Timer(30 * 1000); timer.Elapsed += (sender, e) => mgr.StopScan(); } else { //Invalid state -- Bluetooth powered down, unavailable, etc. System.Console.WriteLine("Bluetooth is not available"); } } public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI) { Console.WriteLine("Discovered {0}, data {1}, RSSI {2}", peripheral.Name, advertisementData, RSSI); } }

© www.soinside.com 2019 - 2024. All rights reserved.