The Nearby Interaction framework:
makes it easy to take advantage of the unique capabilities of U1, Apple’s chip for Ultra-Wideband technology
enables creating precise and spatially-aware interactions between nearby devices
iOS examples:
Find My’s Precision Finding with AirTag
Handoff gestures between iPhone and HomePod mini
Nearby Interaction quick start
Create a
NISessioninstance - this is the main object through which you’ll configure and run your spatial interactions with nearby devicesSet the delegate on the
NISessioninstance, which needs to be an object conforming toNISessionDelegate- The delegate will receive updates from the frameworkCreate a configuration object (a
NIConfigurationsubclass) - E.g.,NINearbyPeerConfigurationCall run on the
NISessioninstance with your configurationThe framework will start providing updates to your delegate - most importantly, you will get a stream of
NINearbyObjectupdates, each containingdistanceand, optionally,direction, to nearby devices that are actively participating in the session
Permission prompt updates
Starting from iOS 15, the permission prompt is similar to other system permissions like user location:
the permission prompt will be shown on the first time your app runs an
NISessionthe permission will only be shown only once per app install
the permission can be changed in the Settings.app’s Privacy page under Nearby Interactions
Add a NSNearbyInteractionUsageDescription entry in your app Info.plist.
Third-party accessory API
U1-compatible development kits
Sample app code
Configuring for accessory interaction
Nearby Interaction expects your app and your accessory to have some sort of capability to exchange data between them - what technology to use to exchange data is entirely up to you (e.g., Bluetooth, LAN, Internet)
To start a session with an accessory, you’ll create a
NINearbyAccessoryConfigurationThis configuration expects an Accessory Configuration Data, which the accessory needs to send to the iDevice via the data channel (before connecting via the Nearby Interaction framework), this data will be known by the accessory manufacturers.
/// Handle configuration data from the accessory.
private func setupAccessory(_ configData: Data, name: String) {
do {
config = try NINearbyAccessoryConfiguration(data: configData)
} catch let error {
print("Bad config data from accessory \(name). Error: \(error)")
return
}
// Cache the token to correlate updates with this accessory.
cacheToken(config! .accessoryDiscoveryToken, accessoryName: name)
}Once we have our NINearbyAccessoryConfiguration ready, we can start a NISession and start the session.
Similarly to how Nearby Interaction needed configuration data from the accessory, the accessory also needs configuration data from Nearby Interaction in order to know how to configure itself. This data needs to be in a format called Shareable Configuration Data
When you run a session with an accessory configuration, Nearby Interaction will provide the Shareable Configuration Data to your app through a delegate callback
You’ll use your data channel to send the shareable configuration data back to the accessory
/// Send the Shareable Configuration Data to the accessory.
func session(_ session: NISession, didGenerateShareableConfigurationData: Data, for object: NINearby0bject) {
// Get the data link for this accessory from a helper function.
guard let conn = getConnection(object: object) else { return }
// Send shareable configuration data.
conn.sendShareableConfigurationData(data)
}Once the code running on the accessory receives the data, it needs to provide it as-is, and as quickly as possible, to the Ultra-Wideband hardware on board.
Both the Sharable COnfiguration Data and Accessory Configuration Data are part of Apple’s specification.
The document is intended for chipset and module manufactures, and it contains the necessary details for creating Ultra-Wideband solutions that use industry standards to interoperate with U1 in iPhone.
