简体   繁体   English

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

[英]Checking Bluetooth status (on/off) in Xamarin.iOS

I am looking for a function that gets the status of the devices Bluetooth so I can tell the app users if it is on or off.我正在寻找一种获取设备蓝牙状态的功能,以便我可以告诉应用程序用户它是打开还是关闭。

Current code keeps saying the Bluetooth is off even though it is on.当前代码一直说蓝牙已关闭,即使它已打开。 Any help and guidance is appreciated!任何帮助和指导表示赞赏!

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;
            }
        }

Try this and if nothing changes it can be because of device version :试试这个,如果没有任何变化,可能是因为设备版本:

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;
    }

There are two points need to pay attention when developing Xamarin iOS bluetooth application.在开发Xamarin iOS 蓝牙应用时需要注意两点

  • First ,you need to run it in a physical device , can not testing bluetooth function in a simulator device .首先,您需要在物理设备上运行它,不能在模拟器设备中测试蓝牙功能。 If that , you will always get follow console log with shared code :如果那样,您将始终获得带有共享代码的控制台日志:

2020-02-28 13:32:38.310442+0800 AppIOSBluetooth[36757:5394891] Bluetooth not enabled 2020-02-28 13:32:38.310442+0800 AppIOSBluetooth[36757:5394891] 蓝牙未启用

  • Second , you need to add permission for bluetooth application in info.plist file .When first running app , it will show permission popup window .其次,您需要在info.plist文件中为蓝牙应用程序添加权限。当第一次运行应用程序时,它会显示权限弹出窗口。

在此处输入图片说明

However , in info.plist , you can add the permission of Bluetooth easily as follow and will forget another most important permission :但是,在info.plist ,您可以轻松添加蓝牙权限,如下所示,并且会忘记另一个最重要的权限:

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

That's not enough for Bluetooth .这对蓝牙来说还不够。 You also need to add another permission :您还需要添加另一个权限:

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

In addition , there is an offical API doc about using Bluetooth in Xamarin iOS you can have a look .此外,还有一个关于在 Xamarin iOS 中使用蓝牙的官方 API 文档,您可以看看。

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("Bluetooth is available");
            //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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM