Chúng tôi cần ứng dụng của chúng tôi để nhận thông báo từ hệ điều hành khi kết nối hoặc ngắt kết nối khỏi thiết bị âm thanh bluetooth (cụ thể là thiết bị âm thanh trong xe của họ).Làm cách nào để được thông báo về kết nối bluetooth khi ứng dụng iOS được chạy trong Xamarin?
Ứng dụng được thông báo khi thiết bị BT ban đầu kết nối, nhưng sau đó có vẻ như ngay lập tức ngắt kết nối và bản ghi lỗi:
BTCentralManager::DisconnectedPeripheral > SoundCore mini(ATT) ERROR Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us."
... và "DisconnectedPeripheral" sự kiện không bao giờ thực sự bị sa thải.
Chúng tôi không chắc chắn cách đơn giản nhận kết nối và ngắt kết nối sự kiện trong khi ứng dụng được chạy nền.
Chúng ta có cần kết nối thiết bị ngoại vi với trình quản lý trung tâm không? Chúng tôi chỉ cần biết liệu thiết bị âm thanh có được kết nối hay không - chúng tôi không cần phải tương tác với thiết bị đó theo bất kỳ cách nào.
Sự kiện không bao giờ gọi lần thứ hai từ nền sau khi ngắt kết nối ngoại vi. Có lẽ vì thông báo lỗi chúng tôi đang nhận.
Mẫu mã bên dưới:
public class BTCentralManager : CBCentralManagerDelegate
{
public CBCentralManager centralManager;
public static CBPeripheral peripheral;
public BTCentralManager()
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::Constructor > ");
centralManager = new CBCentralManager(this, new DispatchQueue("myqueue"),
new CBCentralInitOptions { ShowPowerAlert = true, RestoreIdentifier = "myRestoreIdentifier" });
NSUuid[] arr = { new NSUuid("7e9002be-547f-42bc-8d56-209736f70aa2") }; //Sound core mini
var devices = centralmanager.retrieveperipheralswithidentifiers(arr);
peripheral = devices.firstordefault();
//is the only way to trigger the events, we need to first connect the peripheral to central manager???
centralManager.connectPeripheral(peripheral, new PeripheralConnectionOptions
{
NotifyOnConnection = true,
NotifyOnDisconnection = true,
NotifyOnNotification = true
});
}
//Always is triggered inclusive in background
public override void UpdatedState(CBCentralManager central)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::UpdatedState > " + central.State.ToString());
}
//Only is triggered when the device is first time connected (Inclusive in background)
public override void ConnectedPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::ConnectedPeripheral > " + peripheral.Name);
//After the connect made successfully I receive this error, and never connect again to the device
//Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us."
}
public override void DisconnectedPeripheral(CBCentralManager central, CBPeripheral peripheral, NSError error)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::DisconnectedPeripheral > " + peripheral.Name + " ERROR>" + error.Description);
}
public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::DiscoveredPeripheral > " + peripheral.Name);
// base.DiscoveredPeripheral(central, peripheral, advertisementData, RSSI);
}
}