问题
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.
回答1:
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);
}
来源:https://stackoverflow.com/questions/60045636/xamarin-ios-app-crashing-with-cbcentralmanager