Giter VIP home page Giter VIP logo

bluetonium's Introduction

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

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.

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.

bluetonium's People

Contributors

basvankuijck avatar dickverbunt avatar jason-cooke avatar readmecritic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bluetonium's Issues

-

Does it have?

Problem when writing to a Serial Service

I implemented a serial service model, that will write and read.
A problem when trying to write...in the writeValue() method of ServiceModel the transformer(forUUID: UUID) return nill as no characteristic for this was found.

What is happening?

This is my SerialServiceMode:

import Foundation
import Bluetonium
import CoreBluetooth

struct SerialServiceModelConstants {
    static let serviceUUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
    static let serialWriteUUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
    static let serialReadUUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

}

class SerialServiceModel : ServiceModel{
    weak var delegate : SerialServiceModelDelegate?

    var recentMessage : NSData = NSData()
    var sendMessage : NSData = NSData()

    override func serviceUUID() -> String {
        return SerialServiceModelConstants.serviceUUID
    }

    override func mapping(map: Map) {
        recentMessage <- map[SerialServiceModelConstants.serialReadUUID]
        sendMessage <- map[SerialServiceModelConstants.serialWriteUUID]
    }

    override func registerNotifyForCharacteristic(withUUID UUID: String) -> Bool {
        print("Serial Register Notify Become Available \(UUID)")
        return true
    }

    override func characteristicBecameAvailable(withUUID UUID: String) {
        readValue(withUUID: UUID)
        print("Serial Service Characteristic Become Available \(UUID)")
    }

    override func characteristicDidUpdateValue(withUUID UUID: String) {
        if UUID == SerialServiceModelConstants.serialReadUUID{
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                self.delegate?.receiveFromSerial(String(data: self.recentMessage, encoding: NSUTF8StringEncoding)!)
            }
        }
    }


    func writeToSerial(withInfo text : String){
        self.sendMessage = text.dataUsingEncoding(NSUTF8StringEncoding)!
        self.writeValue(withUUID: SerialServiceModelConstants.serialWriteUUID)
        self.sendMessage = NSData()
    }

}

protocol SerialServiceModelDelegate: class {
    func receiveFromSerial(serialPrint : String)
}

didFindDevice not call

My code as below but when i click button Scan function didFindDevice never responds. Did I declare something wrong?
screen shot 2017-12-14 at 17 26 53

Connect but no write

Iโ€™m having this issue I am able to list devices, then to connect to the chosen one but when I trie to write value, nothing happens Iโ€™m ina hurry and didnโ€™t mind to pay for the service of helping me out solving this as is due on Thursday contact please at [email protected]

Swift 3 compatibility

Hey, I'd love to see this lib available in Swift 3 so I can implement it in my project.

Thanks ๐Ÿ˜„

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.