Giter Club home page Giter Club logo

nfctoble's Introduction

NFCtoBLE

A simple way to connect an iOS device to a BLE peripheral by giving all essential tools threw property wrapper.

GitHub license Platform Swift

Principle

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.

Installation

Swift package manager

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"]),
]

Cathage

You can use Carthage to install NFCtoBLE by adding it to your Cartfile.

github "yanngodeau/NFCtoBLE"

Manual

  1. Put NFCtoBLE repo somewhere in your project directory.
  2. In Xcode, add NFCtoBLE.xcodeproj to your project
  3. On your app's target, add the NFCtoBLE framework:
    1. as an embedded binary on the General tab.
    2. as a target dependency on the Build Phases tab.

Usage

@NFCTag

@NFCTag is a property wrapper used to combine all tag reading and writing logic, as well as pairing management.

Mark a property as @NFCTag

@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.

Write tag

@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

Read tag

@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

Pairing

@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

Handle received values

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
})

Contribute

  • 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

License

NFCtoBLE is distributed under the MIT License.

Author

nfctoble's People

Contributors

yanngodeau avatar

Stargazers

Devzhr avatar Rico Becker avatar  avatar

Watchers

 avatar

nfctoble's Issues

Is still working?

Hi Yann,
Thanks for your hard work here. It's quite impressive.

I'd like to know if this library is still working fine.

Could you indicate to me an easy way to test it?

Thanks,
Carlos

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.