Barcode Detection Enterprise API

I am attempting to use the Barcode Detection enterprise API. I have the necessary entitlements and license file. I'm following the sample code online, and whenever I attempt to run the barcode detection using arKitSession.run I get the following error message:

ar_barcode_detection_provider_t <0x300d82130>: Failed to run provider with transient error code: 1

It obviously isn't running the barcode detection, even though it's running in an immersive space in mixed mode. Any idea what might be going on?

struct BarcodeView: View {
    @Environment(AppModel.self) var appModel
    @State private var root = Entity()
    @State private var arKitSession = ARKitSession()
    
    
    var body: some View {
        RealityView { content, attachments in
            content.add(root)
            
            if let barcodeAttachment = attachments.entity(for: "testView") {
                barcodeAttachment.position = [0, 1, -1]
                root.addChild(barcodeAttachment)
            }
        } placeholder: {
            ProgressView()
        } attachments: {
            Attachment(id: "testView") {
                Text("Looking for barcodes")
                    .bold()
                    .font(.largeTitle)
            }
        }
        .task {
            guard BarcodeDetectionProvider.isSupported else {
                print("Barcode detection is not supported")
                return
            }
            
            await arKitSession.requestAuthorization(for: [.cameraAccess, .worldSensing])
            
            let barcodeDetection = BarcodeDetectionProvider(symbologies: [.code128, .code39, .code93, .ean8, .ean13, .qr, .upce])
            do {
                try await arKitSession.run([barcodeDetection])
                
                for await update in barcodeDetection.anchorUpdates {
                    switch update.event {
                        
                    case .added:
                        print("New barcode detected: \(update.anchor.payloadString ?? "")")
                    case .updated:
                        print("Barcode updated: \(update.anchor.payloadString ?? "")")
                    case .removed:
                        print("Barcode removed: \(update.anchor.payloadString ?? "")")
                    }
                }
                
            } catch {
                print("Error running ARKit session: \(error.localizedDescription)")
            }
            
            
        }
    }
}

Hello @jeremyfromtoledo

Barcode detection does not require camera access. Try changing:

await arKitSession.requestAuthorization(for: [.cameraAccess, .worldSensing])

to

await arKitSession.requestAuthorization(for: [.worldSensing])

Aside from that, your code looks good. If that doesn't fix the issue please try updating Xcode and visionOS (on your Vision Pro) and test again.

Please let me know how it goes.

@Vision Pro Engineer Thanks for responding. I did remove that code after posting this, and that did not make a difference. I also made sure my Vision Pro was updated to 2.0.1.

I'm using the standard template for an immersive space app and this is the code to launch my barcode immersive space. There's nothing missing in this part of the implementation is there?

import SwiftUI

@main
struct Enterprise3App: App {
    @State private var appModel = AppModel()
    @State private var avPlayerViewModel = AVPlayerViewModel()

    var body: some Scene {
        WindowGroup {
            if avPlayerViewModel.isPlaying {
                AVPlayerView(viewModel: avPlayerViewModel)
            } else {
                ContentView()
                    .environment(appModel)
            }
        }
        
        ImmersiveSpace(id: appModel.immersiveSpaceID2) {
            BarcodeView()
                .environment(appModel)
                .onAppear {
                    appModel.immersiveSpaceState = .open
                }
                .onDisappear {
                    appModel.immersiveSpaceState = .closed
                }
                
        }
        .immersionStyle(selection: .constant(.mixed), in: .mixed)
    }
}

Did you find the solution to this error? I'm trying to use the API too but am getting the same error.

@jeremyfromtoledo and @team_hcs

This behavior is unexpected and our teams need to investigate further. Can you please file a bug using feedback assistant (http://feedbackassistant.apple.com) and include a sysdiagnose? Instructions for capturing a sysdiagnose can be found here.

Please reply to this post with the feedback number so I can route it to the appropriate team.

@jeremyfromtoledo and @team_hcs

I spoke to the team responsible for the barcode API. The error you're seeing is not fatal and does not stop barcode detection from running. A few suggestions:

  • Triple check your entitlements and make sure your license file is part of your build target. This post contains tips to help you confirm enterprise api entitlements.
  • If you haven't already, try different bar codes to rule out input as the cause.

My entitlements look great, and there is no issue at compile time with them matching up to what's in the portal. I've confirmed that the Enterprise.license file is included in the target membership for the project.

I've even gone so far as to create an additional project from the Xcode template, used .mixed immersionStyle, and copy the code from the example directly into ImmersiveView.swift. I still get that error message and the app absolutely does not pick up and type of bar code. I've added all the available symbologies, and tried scanning both QR codes and barcodes on the back of an iPhone box and also a standard UPC code, to no avail.

It really seems that the code published (https://developer.apple.com/documentation/visionos/locating-and-decoding-barcodes-in-3d-space) to attempt barcode scanning is missing something in order to get it to work properly.

@team_hcs have you gotten barcode scanning to work yet?

HI @jeremyfromtoledo

Thanks for checking your entitlements and replying so quickly. I have successfully ran the code you referenced on several occasions, but I want to give you a better answer than "it works on my machine". Can you file a feedback request that includes a sysdiagnose (instructions in a previous post) and reply with the feedback number? I want to unblock you and get to the bottom of this.

As an aside, if you are not in an immersive space you won't get frames, but your code seems to indicate you are in an immersive space. Can you confirm you are?

Unfortunately the link that you sent I get an "Unauthorized Access" page when clicking on it, even when signed in with the account holder role.

I can confirm that I am using an immersive space. I've even replaced the barcode detection .task with the task from this sample code (https://developer.apple.com/documentation/visionos/setting-up-access-to-arkit-data) and I have no problem detecting planes in the await update in planeData.anchorUpdates code block. Is there any chance something is wrong with my Enterprise.license file that would cause this?

Also, you mentioned that you've ran my code and it seems to be working just fine. Can you confirm you see the same message that I do in the console when running barcode detection? The ar_barcode_detection_provider_t <0x303ee4a20>: Failed to run provider with transient error code: 1 message?

the same problem

@jeremyfromtoledo

Thanks for being patient. I missed your reply. Can you try this link https://developer.apple.com/bug-reporting/profiles-and-logs/?platform=visionos. Scroll down to where is says Sysdiagnose and click the instructions link.

@jeremyfromtoledo

I forgot to add, yes an invalid license file would cause this behavior.

@jeremyfromtoledo I'm running into the same issue with spatial barcode scanning. Have you come across any solutions for this?

I am also trying to use the Barcode Detection API. The code for me is running. World sensing is Authorized and I can see that the barcode detection task is tarted

However, Barcodes are not being recognized. I am wondering if this could be caused by my AVP beging on visionOS 2.2 Beta 3 ?

I would like to add more details. This is how my App looks like

import ARKit

@main
struct MyApp: App {
    
    @State var session = ARKitSession()
    @State var immersionState: ImmersionStyle = .mixed
    var body: some Scene {
        WindowGroup {
            MyContentView()
        }
        ImmersiveSpace(id: "appSpace") {
            ImmersiveView()
        }.immersionStyle(selection: $immersionState, in: .mixed)
    }
    
    
    struct MyContentView: View {
        @Environment(\.openImmersiveSpace) private var openImmersiveSpace
        
        var body: some View {
            Button("Start ARKit experience") {
                Task {
                    await openImmersiveSpace(id: "appSpace")
                }
            }
        }
    }
}

And my ImmersiveView with the barcode detection:

//  ImmersiveView.swift
//  VisionProHelloWorld
//
//  Created by Development User on 26.07.24.
//

import SwiftUI
import RealityKit
import ARKit
import Combine

struct ImmersiveView: View {
    @State private var arkitSession = ARKitSession()
    @State private var root = Entity()
    @State private var fadeCompleteSubscriptions: Set<AnyCancellable> = []
    
    var body: some View {
        RealityView { content in
            content.add(root)
        }
        .task {
         // Check if barcode detection is supported; otherwise handle this case.
         guard BarcodeDetectionProvider.isSupported else { return }
         
         
         // Specify the symbologies you want to detect.
            let barcodeDetection = BarcodeDetectionProvider(symbologies: [.code128, .qr, .upce, .ean13, .ean8])
         
         
         do {
         try await arkitSession.requestAuthorization(for: [.worldSensing])
         
         try await arkitSession.run([barcodeDetection])
         
        print("Barcode scanning started")

         
         for await update in barcodeDetection.anchorUpdates where update.event == .added {
         
         let anchor = update.anchor
         
         // Play an animation to indicate the system detected a barcode.
         playAnimation(for: anchor)
         
         // Use the anchor's decoded contents and symbology to take action.
         print(
         """
         
         Payload: \(anchor.payloadString ?? "")
         Symbology: \(anchor.symbology)
         """)
         }
         } catch {
         // Handle the error.
         print(error)
         }
         }
         }
         
         // Define this function in ImmersiveView.
         func playAnimation(for anchor: BarcodeAnchor) {
         guard let scene = root.scene else { return }
         
         // Create a plane sized to match the barcode.
         let extent = anchor.extent
         let entity = ModelEntity(mesh: .generatePlane(width: extent.x, depth: extent.z), materials: [UnlitMaterial(color: .green)])
         entity.components.set(OpacityComponent(opacity: 0))
         
         // Position the plane over the barcode.
         entity.transform = Transform(matrix: anchor.originFromAnchorTransform)
         root.addChild(entity)
         
         // Fade the plane in and out.
         do {
         let duration = 0.5
         let fadeIn = try AnimationResource.generate(with: FromToByAnimation<Float>(
         from: 0,
         to: 1.0,
         duration: duration,
         isAdditive: true,
         bindTarget: .opacity)
         )
         let fadeOut = try AnimationResource.generate(with: FromToByAnimation<Float>(
         from: 1.0,
         to: 0,
         duration: duration,
         isAdditive: true,
         bindTarget: .opacity))
         
         let fadeAnimation = try AnimationResource.sequence(with: [fadeIn, fadeOut])
         
         _ = scene.subscribe(to: AnimationEvents.PlaybackCompleted.self, on: entity, { _ in
         // Remove the plane after the animation completes.
         entity.removeFromParent()
         }).store(in: &fadeCompleteSubscriptions)
         
         entity.playAnimation(fadeAnimation)
         } catch {
         print("Error")
         }
         }
    }

And here is the output from the log:

Unfortunately now barcode is detected neither qr, nor another.

Barcode Detection Enterprise API
 
 
Q