headless_ar_measure 0.1.0
headless_ar_measure: ^0.1.0 copied to clipboard
Headless iOS AR measurement: a typed stream of status and distance samples over an ARKit camera surface. No text of its own, no exceptions, no network calls.
headless_ar_measure #
A headless AR distance measurement surface for iOS: a typed stream of status and distance samples over an ARKit camera view.
Every number, every label, every unit conversion is left to you. This package hands back millimetres and a tolerance; how you display them is not its business.
Headless does not mean it draws nothing. It draws exactly the two things Flutter cannot draw for you, and nothing else:
- The two points and the segment between them. They are 3D coordinates in ARKit's world, so only the native layer knows where they land on screen after the phone turns. Flutter only ever receives a millimetre value.
- Surface-scanning guidance — Apple's
ARCoachingOverlayView, in the system's own words and the device's own language. It turns itself on while the session is not ready and off once ARKit has found a plane.
No text of ours, no numbers, no buttons, no product vocabulary.
Install #
dependencies:
headless_ar_measure: ^0.1.0
iOS only. There is no Android implementation, and that is deliberate — this package wraps ARKit, and everything in it is Apple's semantics.
Your app's Info.plist needs an NSCameraUsageDescription. Without it the
app is killed the moment the session starts.
The camera prompt is ours to raise #
Your app does not need a permission plugin. When authorization is still
notDetermined, this package asks for it with AVCaptureDevice.requestAccess
before starting the session, and starts the session only once the answer is
in. A refusal arrives as ArMeasureStatus.cameraUnauthorized.
That ordering is load-bearing, not tidiness. Calling ARSession.run() while the
permission is undecided makes the camera pipeline depend on a grant that happens
after run — nothing in Apple's contract says ARKit rebuilds that pipeline
when the user taps Allow, and no session callback reports that it did not.
didFailWithError stays silent and the tracking state stays silent. The AR
surface is transparent until SceneKit draws its first frame, so what the
user sees is their own app's background, with nothing anywhere saying why.
Use #
final availability = await ArMeasure.isAvailable();
if (!availability.arSupported) return;
ArMeasureController? controller;
// The stream feeds the screen; the controller drives the session.
ArMeasure.samples.listen((sample) {
print(sample.status);
if (sample.measurement case final m?) {
print('${m.mm.toStringAsFixed(0)}mm ±${m.tolMm.toStringAsFixed(0)}mm');
}
});
ArMeasureView(
onPlatformViewCreated: (id) => controller = ArMeasureController(id),
);
// Later, from a button:
switch (await controller?.placePoint()) {
case ArMeasurePlaceResult.placed: // haptic, and carry on
case ArMeasurePlaceResult.missed: // "move around until it locks"
case ArMeasurePlaceResult.alreadyComplete: // "read the number, or undo"
case ArMeasurePlaceResult.notReady || null: // the status already says why
}
// And from State.dispose():
controller?.dispose();
| Call | What it does |
|---|---|
ArMeasure.isAvailable() |
ARWorldTrackingConfiguration.isSupported, asked at runtime |
ArMeasure.samples |
Status and distance, one broadcast stream |
ArMeasure.parseSample() |
Builds an ArMeasureSample from raw channel data |
ArMeasureView |
A thin UiKitView wrapper around the native camera surface. Takes no touches — put your buttons on top of it |
ArMeasureController.placePoint() |
Places a point under the screen centre; returns an ArMeasurePlaceResult saying why not, when not |
ArMeasureController.undoPoint() |
Drops the last point |
ArMeasureController.reset() |
Drops both points and rebuilds the coordinate system |
ArMeasureController.pause() / .resume() |
Stops and restarts the camera, keeping both points |
ArMeasureController.dispose() |
Required. Stops the session and turns the camera off |
The crosshair has to say whether it is on something #
ArMeasureSample.aimLocked is true while a ray from the centre of the screen
is hitting a surface — in other words, while a tap would actually place a point.
Draw two crosshairs and swap between them on that flag, the way Apple's Measure
app does. The user moves the phone until it locks, and then taps.
This is not polish. Without it, a miss and a broken button look identical: a
real device, aimed at a glossy black tablet screen at close range — reflective,
untextured, near-zero feature points, the worst surface ARKit can be handed —
produced a "Place" button that did nothing at all, with ready on screen the
whole time. The ray was missing, correctly; nothing said so.
The flag is probed with the same raycast placePoint() uses, at 10Hz,
under a 0.3s grace period before it drops back to false so a marginal surface
does not strobe it. It is always false outside ready and firstPointPlaced
— once both points are down there is nothing left to aim at.
And when a tap does miss anyway, placePoint() says which kind of nothing
happened:
ArMeasurePlaceResult |
What to say |
|---|---|
placed |
nothing — fire a haptic |
missed |
move slowly around the object until the crosshair locks |
notReady |
the current ArMeasureStatus already carries the reason |
alreadyComplete |
both points are down; read the number, or undo |
notReady is also what you get when the channel cannot answer at all — a
missing plugin, a disposed view. Never missed: inviting someone to keep
moving the phone will not revive a dead channel.
Every sample carries how the measurement happened #
ArMeasureSample.diagnostics holds one ArPointDiagnostics per placed point,
in the order they were placed — one entry after the first tap, two once both
are down, null before any point exists or on a native build older than this
field.
| Field | What it is |
|---|---|
target |
which raycast layer actually hit: a confirmed existingPlaneGeometry or a guessed estimatedPlane. Read off ARRaycastResult.target, not inferred |
tracking |
raw ARKit tracking state at the tap. Six values — the five .limited reasons are not collapsed into one word |
sessionAgeMs |
milliseconds since the last run(...). Resets with the coordinate system, so it answers "was the session still warming up?" |
cameraDistanceMm |
camera centre to the placed point |
rayAngleDeg |
the ray's angle to the surface: 90° is dead-on, 0° is grazing |
planeAlignment, planeWidthMm, planeHeightMm |
the ARPlaneAnchor that was hit, if any. All three null when the hit landed on an estimated plane |
Why it exists: a tile edge measured 382mm against a true 400, two tiles measured 795 against 800, and every explanation anyone proposed fitted both numbers equally well — because the sample said nothing about the conditions either point was placed under. None of this feeds the distance calculation. It is a recording layer, and it says what the machine did.
Every field is nullable on purpose. No camera frame, a value ARKit adds in a
later iOS, an older native build — all of it comes back null rather than a
plausible default, because a plausible default here is manufactured evidence.
dispose() is not optional #
iOS has no dispose callback for a platform view — FlutterPlatformView has
exactly one method, view(). Nothing on the native side is told when Flutter
drops the widget. So ArMeasureController.dispose() from your
State.dispose() is the only thing that turns the camera off on time; without
it the session runs until the engine happens to release the view.
The number drifts, and that is the point #
Once both points are down, the distance is recomputed from every ARKit frame, and a new sample goes out whenever it has moved by more than 0.5mm — paced to at most 15Hz, with a trailing emit so the last refinement is never the one that gets dropped. You watch the number settle, and that settling is the signal that the reading is worth keeping. Freezing it is your job; this package keeps reporting.
The two points are ARAnchors, which is how you tell ARKit that you care about
those spots. But nothing here relies on ARKit updating them: ARAnchor.transform
is read-only, Apple's own guidance for a moving object is to remove the anchor
and add a new one, and the classes Apple documents as self-updating are
subclasses (ARPlaneAnchor, ARGeoAnchor) rather than a plain app-added
anchor. session(_:didUpdate anchors:) only promises that ARKit may update.
So the transforms are read back out of ARFrame.anchors, matched by
identifier, on every frame. If ARKit revises a point, that revision shows up.
If it never does, the number is simply constant — which is the truth, rather
than a stale reading dressed up as a live one.
The surface takes no touches #
ARSCNView is created with isUserInteractionEnabled = false, so UIKit's hit
test never stops there and every touch falls straight through to Flutter. Put
your buttons in a Stack on top of the view and treat it as a picture.
One consequence worth knowing: the Reset button Apple's coaching overlay shows
during relocalization is not tappable either. Nothing is stranded by that — the
session gives up on relocalization after five seconds by itself, and
ArMeasureController.reset() does the same job from your own UI.
Tolerance #
tolMm is max(2mm, 0.5%) on a device with scene reconstruction and
max(5mm, 1.5%) without. These are working assumptions, not measured
constants — but a number shipped without a tolerance is a promise of precision
the sensor cannot keep.
It never throws #
A missing plugin registration, a simulator, a ray that hit nothing, a command
aimed at a view that is already gone — all of them surface as sentinel values
(isAvailable() => ArAvailability(false, false), parseSample() => null,
placePoint() => false, every other command a silent no-op). The sample
stream swallows both malformed frames and channel errors, because a stream
that dies leaves the screen frozen on its last frame with nothing to say why.
You do not need a try around any call in this package.
Why a package and not a few files in your app #
Dropping a .swift file into ios/Runner/ only compiles it if it is in
the Xcode target. With a classic project.pbxproj, adding the file by hand
does not add it to the target — and nothing fails loudly. The code is simply
never built.
A plugin package has its own podspec, so every Swift file in it is compiled. That is the whole reason this exists as a package.
License #
MIT.