admin管理员组

文章数量:1289581

Currently, I am writing an application in Swift to establish Bluetooth Low Energy (BLE) connections with devices and facilitate pairing and configuration. The pairing process I am employing involves the ‘Passkey Entry: Responder Displays, Initiator Inputs’ method.

To handle all of this I am using the AccessorySetupKit because it gives me the ability to retrieve the devices that I already paired, which is crucial for my project

However, I am encountering an issue during the pairing process: After inserting the PIN, my iPhone becomes stuck in the ‘Connecting’ state, despite the fact that both devices have successfully paired (as indicated by the other device’s screen), only to timeout after some time.

I tried this with two different iPhones ( 12 Pro and 13 Pro Max with different iOS 18 versions ), and multiple 'to-pair' devices ( all had different firmwares )

Here how I initialized the ASPickerDisplayItem:

internal struct MyAccessory
{
    private static let serviceUuid: String = "180A"
    
    public static let name: String = "MyDevice"
    
    public static let image: UIImage = UIImage( named: "MyImage" )!
    
    public static let services: CBUUID = CBUUID( string: serviceUuid )
    
    public static let pickerDisplayItem: ASPickerDisplayItem =
    {
        let descriptor: ASDiscoveryDescriptor = ASDiscoveryDescriptor()

        descriptor.bluetoothServiceUUID = services
        
        let pickerDisplayItem = ASPickerDisplayItem(
            name: name,
            productImage: image,
            descriptor: descriptor
        )
        
        // For Passkey Entry '.confirmAuthorization' is needed
        pickerDisplayItem.setupOptions = [.confirmAuthorization, .finishInApp]
        
        return pickerDisplayItem
    }()
}

And a quick sample on how the App works ( which is similar to this example given by Apple:

public class AccessoriesManager
{
    private var accessorySession: ASAccessorySession = ASAccessorySession()

    init()
    {
        accessorySession.activate( on: DispatchQueue.main, eventHandler: handleSessionEvent )
    }

    public func startScan()
    {
        accessorySession.showPicker( for: [
            MyAccessory.pickerDisplayItem
            ]
        )
        { error in
            if let error
            {
                print( "Failed to show picker due to: \( error.localizedDescription )" )
            }
        }
    }

    private func handleSessionEvent( event: ASAccessoryEvent )
    {
        switch event.eventType
        {
        case .accessoryAdded:
            /*
             Continue the configuration
             */
            
            break
        default:
            break
        }
    }
}

I anticipate the pairing process to conclude as it does when I perform it without utilizing the AccessorySetupKit (invoking the traditional operating system pop-up menu to insert the PIN using classic Bluetooth handling).

本文标签: swiftiOS AccessorySetupKit gets stuck while PairingStack Overflow