Configuring a Wi-Fi accessory to join a network
Associate an iOS device with an accessory’s network to deliver network configuration information.
Overview
When you’re building a Wi-Fi accessory, getting it to join the user’s network can be a challenge. The best way to accomplish this is by using EAWiFiUnconfiguredAccessoryBrowser in the MFi Wireless Accessory Configuration process. If you can’t use that process, this sample demonstrates an alternative method.
The sample uses a macOS app to act as an accessory and an iOS app running on an iOS device to configure that accessory. The goal for the iOS app is to deliver network information to the accessory so it can join the user’s standard Wi-Fi network. To do that, the iOS app needs to deliver the user’s network SSID and its passphrase to the accessory. After receiving this information, the accessory can use it to associate itself with the same network as the iOS device.
Configure the sample code project
After downloading the sample, follow these steps to configure it for use:
The macOS target is
BrandXAccessory.
In macOS, set up internet sharing (see Share the internet connection on Mac with other network users).
Set the SSID to
AccessoryWiFiand the passphrase toembedded1.Configure Internet Sharing to share your connection from Ethernet to computers using Wi-Fi.
The iOS target is
BrandXAccessorySetup, and requires the use of a physical device.When running the iOS target, you need to change the bundle identifier so that Xcode can create a unique provisioning profile with the project entitlements for your team. Do this in the Xcode Signing & Capabilities pane by altering the Bundle Identifier text field.
Run the sample code project
The macOS app creates a QR code and a network listener. The QR code contains the accessory network information. The iOS app scans the QR code and sends that network information to the BrandXAccessory app for network configuration.
Build and run the
BrandXAccessoryapp.Build and run the
BrandXAccessorySetupapp on an iOS device, and associate the device with the user’s network.In the
BrandXAccessoryapp, click Next to embed the accessory network information in a QR code. A QR code appears with the following encoded network information:SSID of the accessory network: AccessoryWiFi
Passphrase of the accessory network: embedded1
Service URL for
NWConnectionTLS PSK
TLS PSK identity
In the iOS app, tap Capture Accessory Network Info to bring up a QR code capture session.
Point the iOS device’s camera at the QR code in the
BrandXAccessoryapp to capture, decode, and save the QR data. Tap Next.In the iOS app, tap Get Network Info to capture the user’s network SSID. Tap Next.
In the iOS app, tap Associate to Accessory Network.
NEHotspotConfigurationcreates a Wi-Fi configuration with the accessory’s network data from the QR code, applies it to the device, and updates the device’s screen when the association finishes. Tap Next.In the iOS app, tap Confirm Network to confirm the device’s association with the accessory network. Tap Next.
In the
BrandXAccessoryapp, click Start Listener so theNWListenerbegins listening for incoming connections from the iOS app.In the iOS app, tap Enter Network Passphrase and enter the passphrase for the user’s network. Tap Set Passphrase.
In the iOS app, tap Send Network Data. The sample sends the passphrase to the
BrandXAccessoryapp, along with the user’s network SSID.In the
BrandXAccessoryapp, the view displays the SSID and passphrase of the user’s network.In the iOS app, tap Next, and then tap Remove Network Configuration to disassociate from the accessory network and rejoin the user’s network.