A simple way to connect an iOS device to a BLE peripheral by giving all essential tools threw property wrapper.
The iOS device uses the pairing key read from the NFC tag to locate the device to connect. Of course, the device must advertise the same pairing key.
Please be aware that this framework does not manage the configuration of your Bluetooth device. It is up to you to configure it properly to advertise the pairing key you want.
Go to File | Swift Packages | Add Package Dependency...
in Xcode and search for « NFCtoBLE » .
If you have Swift.package file, include the following dependency:
dependencies: [
// [...]
.package(name: "NFCtoBLE",
url: "https://github.com/yanngodeau/NFCtoBLE.git",
.upToNextMajor(from: "x.y")) // Replace x.y with your required version
]
and add it to your target:
targets: [
// [...]
.target(
name: "<Your target name>",
dependencies: ["NFCtoBLE"]),
]
You can use Carthage to install NFCtoBLE
by adding it to your Cartfile
.
github "yanngodeau/NFCtoBLE"
- Put NFCtoBLE repo somewhere in your project directory.
- In Xcode, add
NFCtoBLE.xcodeproj
to your project - On your app's target, add the NFCtoBLE framework:
- as an embedded binary on the General tab.
- as a target dependency on the Build Phases tab.
@NFCTag
is a property wrapper used to combine all tag reading and writing logic, as well as pairing management.
@NFCTag(pairingKey: "c20c8a91-badb-4d5f-a563-960f8e3b1608") myPeripheral = MyPeripheral()
myPeripheral
is the object describing the Bluetooth device you plan to connect to. It can be whatever you want, or whatever you think is useful to write on the tag to describe the Bluetooth device.
The pairingKey
parameter represents the pairing identifier used to locate a BLE device. It must be the same as the one announced by the device you plan to connect to.
If you don't know the pairing key and intend to read a tag to retrieve it, you can set the key to nil
. The key will be redefined when the tag is read.
@NFCTag(pairingKey: "c20c8a91-badb-4d5f-a563-960f8e3b1608") myPeripheral: MyPeripheral()
_myPeripheral.write { manager in
// Session did become active
manager.setMessage("👀 Place iPhone near the tag to be written on")
} didWrite: { manager, result in
// Tag has been written
switch result {
case .failure:
manager.setMessage("👎 Failed to write tag")
case .success:
manager.setMessage("🙌 Tag successfully written")
}
didBecomeActive
: handles actions on session activation
didWrite
: handles actions when the tag has been written
@NFCTag(pairingKey: nil) myPeripheral: MyPeripheral()
_myPeripheral.read { manager in
manager.setMessage("👀 Place iPhone near the tag to read")
} didRead: { manager, result in
switch result {
case .failure(let error):
manager.setMessage("👎 Failed to read tag")
case .success:
manager.setMessage("🙌 Tag read successfully")
}
didBecomeActive
: handles actions on session activation
didRead
: handles actions when the tag has been read
@NFCTag(pairingKey: "c20c8a91-badb-4d5f-a563-960f8e3b1608") myPeripheral: MyPeripheral()
var usedServices : [CBUUID] // Target services to be used
_myPeripheral.scanToConnect(withservices: [usedServices]) { manager in
// Session did become active
} didConnect: { manager, result in
switch result {
case .failure(let error):
print("Tag. connection failure")
case .success:
print("Connected to peripheral")
connectedPeripheral = try? result.get() // Get the connected peripheral
}
didBecomeActive
: handles actions on session activation
didRead
: handles actions when the tag has been read
didConnect
: handles actions when the tag has been connected
You can handle the received value by adding an action using the Peripheral
function onValueUpdated(_ perform: @escaping DidUpdateValue)
:
connectedPeripheral?.onValueUpdated({ characteristicUUID, data in
// Perform action
})
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
NFCtoBLE is distributed under the MIT License.
- Yann Godeau - @yanngodeau