GithubHelp home page GithubHelp logo

krohling / sible Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emberlight/sible

1.0 2.0 0.0 608 KB

Simple Bluetooth Low Energy Framework for iOS

License: MIT License

Ruby 1.18% Objective-C 97.65% Shell 1.17%

sible's Introduction

SIBLE - Simple Bluetooth Low Energy for iOS

Introduction

Sible is an abstraction layer on top of the iOS Bluetooth Low Energy (BLE) API's. When developing sophisticated BLE applications the process of discovering Services, Characteristics and initiating read/write requests quickly becomes complex and difficult to manage. Sible encapsulates this complexity and makes it possible to interact with BLE devices using a much simpler pattern that is intuitive and easy to use.

Quick start

  1. Install CocoaPods with gem install cocoapods.
  2. Create a file in your XCode project called Podfile and add the following line:
pod 'Sible'
  1. Run pod install in your Xcode project directory. CocoaPods should download and install the Sible library, and create a new Xcode workspace. Open up this workspace in Xcode.

Usage

Scan for nearby devices

Sible *sible = [[Sible alloc] init];
[sible scan:@"SERVICE-UUID" WithDuration:5.0 Handler:^(NSArray *scans, NSError *error) {
  for (PeripheralScan *scan in scans) {
    NSLog(@"Peripheral: %@", scan.peripheral);
    NSLog(@"Advertisement: %@", scan.advertisement);
    NSLog(@"RSSI: %@", scan.RSSI);
  }
}];

Reading a BLE Characteristic

Sible *sible = [[Sible alloc] init];
[sible read:peripheral ServiceUUID:@"SERVICE-UUID" CharacteristicUUID:@"CHAR-UUID" 
  Handler:^(CBPeripheral *peripheral, NSData *data, NSError *error) {
    NSLog(@"Result: %@", data);
}];

Writing a single byte to a BLE Characteristic

Sible *sible = [[Sible alloc] init];
[sible write:device.peripheral ServiceUUID:@"SERVICE-UUID" CharacteristicUUID:@"CHAR-UUID" Byte:0x99 
  Handler:^(CBPeripheral *peripheral, NSError *error) { }];

Writing an ASCII string to a BLE Characteristic

Sible *sible = [[Sible alloc] init];
[sible write:device.peripheral ServiceUUID:@"SERVICE-UUID" CharacteristicUUID:@"CHAR-UUID" String:@"my data" 
  Handler:^(CBPeripheral *peripheral, NSError *error) { }];

Writing a collection of bytes to a BLE Characteristic

unsigned char bytes = {0x01, 0x02, 0x03};
NSData *data = [NSData dataWithBytes:bytes length:3];
Sible *sible = [[Sible alloc] init];

[sible write:device.peripheral ServiceUUID:@"SERVICE-UUID" CharacteristicUUID:@"CHAR-UUID" Value:data 
  Handler:^(CBPeripheral *peripheral, NSError *error) { }];

Interacting With Multiple Devices In Parallel

It's even possible to execute operations against multiple peripherals at the same time. The example below will execute writes to a list of devices in parallel.

for (CBPeripheral *peripheral in myListOfPeripherals) {
  [sible write:peripheral ServiceUUID:@"SERVICE-UUID" CharacteristicUUID:@"CHAR-UUID" Byte:0x99 Handler:^(CBPeripheral *peripheral, NSError *error) { }];
}

Sible Transactions

Transactions are a simple way of orchestrating a number operations that should occur in sequence. These operations can be reads or writes, and can even be executed against different Peripherals. All of the operations enqueued in a Transaction are executed serially, one at a time, until they all complete. If an operation fails during the execution of a transaction, the transaction stops and calls the handling block with the appropriate error.

Transaction Example

//This transaction executes 2 write operations and then a read

SiblePeripheralTransaction *transaction = [[SiblePeripheralTransaction alloc] initWithPeripheral:peripheral];
[transaction enqueueWriteOperation:@"SERVICE1-UUID" CharacteristicUUID:@"CHAR1-UUID" Byte:0x01];
[transaction enqueueWriteOperation:@"SERVICE2-UUID" CharacteristicUUID:@"CHAR2-UUID" String:@"my data"];
SibleReadOperation *readOp = [transaction enqueueReadOperation:@"SERVICE3-UUID" CharacteristicUUID:@"CHAR3-UUID"];
  
[sible executeTransaction:transaction Handler:^(SibleTransaction *transaction, NSError *error) {
    NSLog(@"Result: %@", readOp.readValue);
}];

Transaction Example with multiple Peripherals

//This transaction executes 2 write operations and then a read.  
//Each operation is executed against a different peripheral.

SibleTransaction *transaction = [[SibleTransaction alloc] init];
[transaction enqueueWriteOperation:peripheral1 ServiceUUID:@"SERVICE1-UUID" CharacteristicUUID:@"CHAR1-UUID" Byte:0x01];
[transaction enqueueWriteOperation:peripheral2 ServiceUUID:@"SERVICE2-UUID" CharacteristicUUID:@"CHAR2-UUID" String:@"my data"];
SibleReadOperation *readOp = [transaction enqueueReadOperation:peripheral3 ServiceUUID:@"SERVICE3-UUID" CharacteristicUUID:@"CHAR30UUID"];
  
[sible executeTransaction:transaction Handler:^(SibleTransaction *transaction, NSError *error) {
    NSLog(@"Result: %@", readOp.readValue);
}];

sible's People

Contributors

krohling avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.