• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bluetooth mapping in Swift

Bluetonium: Bluetooth library in Swift

Bluetonium is part of the E-sites iOS Suite.


Bluetonium is a Swift Library that makes it easy to communicate with Bluetooth devices.

forthebadge forthebadge forthebadge

Build Status CocoaPods Compatible codecov Carthage compatible Platform Quality

Features

  • 🎲 Services and characteristics mapping
  • πŸ‘“ Default data transformers
  • πŸ”§ Reading & writing to peripherals
  • πŸŒ” Background mode
  • πŸ“» Scanning and connecting to peripherals
  • πŸ¦… Swift 3 & 4

Requirements

  • iOS 8.0+
  • Xcode 7.2+

Installation

CocoaPods

Add the following to your Podfile:

pod 'Bluetonium'

Make sure that you are integrating your dependencies using frameworks: add use_frameworks! to your Podfile. Then run pod install.

Carthage

Add the following to your Cartfile:

github "e-sites/Bluetonium"

Run carthage update and follow the steps as described in Carthage's README.

Usage

Get devices

The Manager will handle searching for devices and connecting to them.

import Bluetonium

let manager = Manager()
manager.delegate = self
manager.startScanForDevices()

If a device is found you will get notified by the func manager(_ manager: Manager, didFindDevice device: Device) delegate call. You can also get all found devices in the foundDevices array of your manager.

Connect to a device

Connecting to a device is simple.

manager.connect(with: device)

The device is a device form the foundDevices array.

Create and register a ServiceModel

A ServiceModel subclass will represents a Service, all the properties represent the Characteristics.

This example represents the Battery Service

class BatteryServiceModel: ServiceModel {
	enum Characteristic : String {
		case batteryLevel = "2A19"
	}

	var batteryLevel: UInt8 = 0
	
	override var serviceUUID:String {
		return "180F"
	}
	
	override func mapping(_ map: Map) {
		batteryLevel <- map[Characteristic.batteryLevel.rawValue]
	}
	
}

Register a ServiceModel subclass. Make sure you do this before the device is actually connected.

let batteryServiceModel = BatteryServiceModel()

func manager(_ manager: Manager, willConnectToDevice device: Device) {
	device.register(serviceModel: batteryServiceModel)
}

ServiceModel subclass

A ServiceModel subclass will represents a Service, all the properties represent the Characteristics. Interacting with the peripheral is only possible once the characteristic did became available through the func characteristicBecameAvailable(withUUID UUID: String) function.
Or when the serviceReady boolean is set to true.

It's recommended to create a struct containing static properties of the UUID's along with your ServiceModel this way your app doesn't have to hardcode the UUID in different places and prevents errors. (See example: HeartRateServiceModel in project)

Reading

batteryServiceModel.readValue(withUUID: "2A19")

// Or with completion
batteryServiceModel.readValue(withUUID: "2A19") { value in
	print(value)
}

Writing

batteryServiceModel.batteryLevel = 10
batteryServiceModel.writeValue(withUUID: "2A19")

Custom DataTransformers

It is possible that your characteristic has a custom data format or has a data format not yet supported. Then you can create your own custom DataTransformer for that property.

The custom DataTransformer needs to conform to the DataTransformer protocol which has two functions.

class HeartRateDataTransformer: DataTransformer {
    
    func transform(dataToValue data: Data?) -> MapValue {
    	// Used when reading from the characteristic.
    	// Transform Data to your property MapValue.
    }
    
    func transform(valueToData value: MapValue?) -> Data {
    	// Used when writing to the characteristic.
    	// Transform your property MapValue to Data.
    }
    
}

To register your custom DataTransform you can add it to the mapping function:

func mapping(_ map: Map) {
	heartRate <- (map["2A37"], HeartRateDataTransformer())
}

Set notifications for a characteristic

The ServiceModel has a function that will let you register for value changes on the peripheral. When you return true for one of you characteristics it will automatically update the property.

func registerNotifyForCharacteristic(withUUID UUID: String) -> Bool

Contributions

Feedback is appreciated and pull requests are always welcome. Let's make this list longer!

License

Bluetonium is released under the MIT license. See LICENSE for details.

More Repositories

1

perimeter.js

Creates an invisible perimeter around a target element and monitors mouse breaches.
JavaScript
299
star
2

Natrium

A pre-build (Swift) script to alter your Xcode project at pre-build-time per environment, build configuration and target.
Swift
145
star
3

Zepto-Builder

Zepto Builder will let you generate a custom version of Zepto that just includes the modules you need.
JavaScript
112
star
4

php-mysql-mysqli-wrapper

PHP
98
star
5

ESTimePicker

A custom time picker just like the Google Calendar Android App
Objective-C
86
star
6

ESArcProgressView

A progress view to be used within Apple Watch projects
Objective-C
80
star
7

ESDatePicker

A custom date picker similar like the date picker of the Sunrise app and the Google Calendar android app
Objective-C
31
star
8

tinymce-emmet-plugin

A TinyMCE plugin that provides an HTML editor including all Emmet features
JavaScript
24
star
9

magento-advanced-code-editor

An advanced code editor that'll make it much easier to write clean markup for CMS pages, static blocks, product pages and Transactional Emails.
JavaScript
19
star
10

Jsend

Ajax wrapper that handles JSON data exchange according to the non-official JSend spec
JavaScript
18
star
11

ESOpenSourceLicensesKit

A script to generate an HTML file of all the license files used with your CocoaPods Project
Swift
14
star
12

ingenico.css

An elegant front-end boilerplate that'll make it easier to cope with the default Ingenico payment view
HTML
13
star
13

UnityXcodeBridge

A step-by-step tutorial on how to make a bridge between Unity's C# and Xcode's Objective-C
12
star
14

iOS-Suite

The a part of the E-sites iOS library suite
9
star
15

Palladium

A lightweight framework to schedule and cancel local notifications.
Swift
9
star
16

gulp-frontend-tasks

A bundle of useful Gulp tasks to automate your front-end workflow
JavaScript
8
star
17

RxSwiftly

An E-sites collection of RxSwift extensions
Swift
7
star
18

PAPI

A lightweight jQuery plugin that makes working with the Postcode API (@postcodeapi) easy as pie.
HTML
6
star
19

Fluorine

A small UIView helper to create simple linear gradients
Swift
5
star
20

Aluminum

Applying the robot pattern to your UI XCTests
Swift
4
star
21

populate

A small yet useful jQuery plugin that populates <select> elements with option nodes. It will accept either an object literal, a HTML fragment or a serialized string
JavaScript
4
star
22

Erbium

Light weight tool for detecting the current device's capabilities
Swift
3
star
23

Cobalt

The E-sites Swift iOS API Client used for standard restful API's
Swift
3
star
24

Pipelines

A macOS statusbar app that monitors Buildkite builds
Swift
3
star
25

Oganesson

A small swift helper class for using an ObjectPool
Swift
3
star
26

ESFlatButton

A beveled flat button
Objective-C
3
star
27

MaterialDesignSpinner

An iOS activity spinner modeled after Google's Material Design Spinner
Swift
3
star
28

esl

ESL is our internally developed php-code library with commonly used functions.
PHP
3
star
29

Bismuth

FIFO queue handling
Swift
2
star
30

Dysprosium

Deallocation helper for runtime objects
Swift
2
star
31

Einsteinium

A collection of swift extensions
Swift
2
star
32

XcodeLocalizableStringsBuildScript

A shell script to show errors for any missing translations used by NSLocalizedString at runtime
Shell
2
star
33

require-matchmedia-plugin

Require.js plugin for loading files conditionally, based on matchMedia.
JavaScript
2
star
34

Bohrium

A small helper class to benchmark specific code parts at runtime
Swift
2
star
35

Radon

A lightweight commandline tool to automatically generate strong-typed image references.
Swift
2
star
36

Lithium

_The_ E-sites logging framework
Swift
2
star
37

swift-log-papertrail

Swift
2
star
38

PinFieldView

Entering PIN codes made easy
Swift
1
star
39

Dupnium

A small helper class for localization
Swift
1
star
40

ObjectPool

A small swift helper class for using an ObjectPool
Swift
1
star
41

swift-log-proxy

Swift
1
star
42

ESMonitor

A draggable system monitor inside your application. Shake to show.
Swift
1
star
43

Francium

A small library to use for your file system.
Swift
1
star
44

ESDateHelper

Helper category for making the usage of NSDates easier
Objective-C
1
star
45

fe-boilerplate-poc

Frontend developer boilerplate proof of concept
CSS
1
star