xamarin ios 应用程序与 CBCentralManager 一起崩溃 [英] xamarin ios app crashing with CBCentralManager

查看:29
本文介绍了xamarin ios 应用程序与 CBCentralManager 一起崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的应用程序,除了以下代码之外,基本上没有添加任何代码.如果我在模拟器中尝试它会进入更新状态并显示不受支持.如果我尝试在第 6 代 ipad(打开蓝牙)上运行,应用程序会在调试退出 UIButton231_TouchUpInside 后立即崩溃(并且永远不会陷入困境).

我错过了什么吗?

 CBCentralManager _central;部分无效 UIButton231_TouchUpInside(UIButton 发送者){尝试{蓝牙LEManager();}捕获(例外 e){Console.Write(e);}}protected void BluetoothLEManager(){尝试{_central = new CBCentralManager(DispatchQueue.CurrentQueue);_central.DiscoveredPeripheral +=(对象发送者,CBDiscoveredPeripheralEventArgs e)=>{Console.WriteLine("DiscoveredPeripheral:" + e.Peripheral.Name);Console.WriteLine("RSSI:" + e.Peripheral.RSSI);};_central.UpdatedState += (object sender, EventArgs e) =>{Console.WriteLine("更新状态:" + _central.State);};_central.ConnectedPeripheral +=(对象发送者,CBPeripheralEventArgs e)=>{Console.WriteLine("ConnectedPeripheral:" + e.Peripheral.Name);};_central.DisconnectedPeripheral +=(对象发送者,CBPeripheralErrorEventArgs e)=>{Console.WriteLine("DisconnectedPeripheral:" + e.Peripheral.Name);};}捕获(例外 e){Console.Write(e);}}

在下面的图像中(代码被压缩以使图像更短),调试到达第 62 行就好了,但尝试跳过或让它继续将关闭应用程序,并且未到达 catch 中的断点.

解决方案

我在本地站点尝试了共享代码,但它也崩溃并带有一些错误日志:

检查此行错误后:

<块引用>

在执行本机代码时收到 SIGABRT.这通常表示 Mono 运行时或应用程序使用的本机库之一中存在致命错误.

有时可能是由 iOS 中的权限引起的.你可以看看 James 的这篇文章:

此权限是基本且必要的.因为这会在iOS设备中弹出一个权限对话框窗口.

顺便说一下,还有 关于在 Xamarin iOS 中使用蓝牙的官方 API 你可以看看.

公共类 MySimpleCBCentralManagerDelegate : CBCentralManagerDelegate{覆盖 public void UpdatedState (CBCentralManager mgr){if (mgr.State == CBCentralManagerState.PoweredOn) {//传入所有外围设备的空扫描.可以使用 CBUIID 定位外设CBUUID[] cbuuids = null;mgr.ScanForPeripherals (cbuuids);//发起DiscoveredPeripheral的异步调用//30秒后超时var 计时器 = 新计时器 (30 * 1000);timer.Elapsed += (sender, e) =>mgr.StopScan();} 别的 {//无效状态——蓝牙掉电、不可用等System.Console.WriteLine(蓝牙不可用");}}公共覆盖无效 DiscoveredPeripheral(CBCentralManager 中心,CBPeripheral 外围设备,NSDictionary 广告数据,NSNumber RSSI){Console.WriteLine(发现{0},数据{1},RSSI {2}",外设.名称,广告数据,RSSI);}}公共部分类 HelloBluetoothCSharpViewController : UIViewController{MySimpleCBCentralManagerDelegate myDel;公共覆盖 void ViewDidLoad(){base.ViewDidLoad();//重要的是保留引用,否则会被GCmyDel = new MySimpleCBCentralManagerDelegate();var myMgr = new CBCentralManager (myDel, DispatchQueue.CurrentQueue);}

I have a Fresh application with basically no code added to it except the following Code. If I try it in a simulator it goes into update state and says unsupported. If I try to run in on a 6 generation ipad (with bluetooth turned on) the application crashes as soon as debugging exits UIButton231_TouchUpInside (and never goes into the catch).

Am I missing anything?

        CBCentralManager _central;
    partial void UIButton231_TouchUpInside(UIButton sender)
    {
        try
        {
            BluetoothLEManager();
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
    }

    protected void BluetoothLEManager()
    {
        try
        {
            _central = new CBCentralManager(DispatchQueue.CurrentQueue);
            _central.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) =>
            {
                Console.WriteLine("DiscoveredPeripheral: " + e.Peripheral.Name);
                Console.WriteLine("RSSI: " + e.Peripheral.RSSI);
            };

            _central.UpdatedState += (object sender, EventArgs e) =>
            {
                Console.WriteLine("UpdatedState: " + _central.State);
            };


            _central.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) =>
            {
                Console.WriteLine("ConnectedPeripheral: " + e.Peripheral.Name);

            };

            _central.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) =>
            {
                Console.WriteLine("DisconnectedPeripheral: " + e.Peripheral.Name);
            };
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
    }

In the Image below (code is compacted to make the image shorter), debugging reaches line 62 just fine, but attempting to step over or to just let it continue will close the application, and breakpoints in the catch are not reached.

解决方案

I have tried shared code in local site , and it also crashes and with some error logs :

After checking this line error :

Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application.

Sometimes that may be caused by permission in iOS . You can have a look at this aticle by James : New iOS 10 Privacy Permission Settings .

Starting in iOS 10, nearly all APIs that require requesting authorization and other APIs, such as opening the camera or photo gallery, require a new key value pair to describe their usage in the Info.plist.

However , in info.plist , you can add the permission of Bluetooth easily as follow and will forget another most important permission :

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

From native info.plist , you also will find it .

This permission is fundamental and necessary . Because this will pop up a permission Dialog window in iOS device .

By the way , there is an offical API about using Bluetooth in Xamarin iOS you can have a look .

public class MySimpleCBCentralManagerDelegate : CBCentralManagerDelegate
{
        override public void UpdatedState (CBCentralManager mgr)
        {
            if (mgr.State == CBCentralManagerState.PoweredOn) {
                //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);
    }

}
public partial class HelloBluetoothCSharpViewController : UIViewController
{
MySimpleCBCentralManagerDelegate myDel;
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

  //Important to retain reference, else will be GC'ed
        myDel = new MySimpleCBCentralManagerDelegate ();
        var myMgr = new CBCentralManager (myDel, DispatchQueue.CurrentQueue);

    }

这篇关于xamarin ios 应用程序与 CBCentralManager 一起崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆