SkyBlue 0.5.0

SkyBlue 0.5.0

Maintained by Enix Yu.



SkyBlue 0.5.0

  • By
  • enix223

SkyBlue

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

OS >= iOS 8.0

Installation

SkyBlue is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SkyBlue'

How to use

  1. Get single instance
self.bleManager = [BLEManager sharedInstance];

// Set scan timeout to 10 sec, only available when `continousScan` = NO
self.bleManager.scanInterval = 10;

// Scan will not terminate until user invoke `stopScan`
self.bleManager.continousScan = YES;

// Set connect timeout to 10 sec
self.bleManager.connectTimeout = 10;

// Set enumeration timeout to 10 sec
self.bleManager.enumerateTimeout = 10;

// Set write characteristic value timeout to 10 sec
self.bleManager.sendDataTimeout = 10;
  1. Scan
[_bleManager scanPeripheralWithUUIDs:uuids resultCallback:^(NSDictionary<NSUUID *, BLEPeripheral *> *devices, NSError *error) {
    NSMutableArray *devs = [NSMutableArray array];
    [devices enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"Device UUID: @%", key);
        BLEPeripheral *device = obj;
        // Do anything you need with BLEPeripheral
    }];
}];
  1. Connect
[_bleManager connectPeripheral:peripheral completion:^(NSNumber *success, NSError *error) {
    if (error) {
        NSLog(@"Failed to connect BLE Peripheral");
    } else {
        // Connected
        Do everything you need after connected
    }
}];
  1. You can access the connected device withconnectedPeripherals, this is a NSDictionary<NSUUID *, BLEPeripheral *> to keep the connected peripherals. The key is the peripheral UUID.
_bleManager.connectedPeripherals
  1. Subscribe/Unsubscribe
// Subscribe
[_bleManager subscribeNotificationForPeripheral:peripheral
                             characteristicUUID:BLE_CHARACTERISTIC_UUID   // Characteristic UUID String, eg. FFE1
                                 resultCallback:^(NSData *data, NSError *error) {
                                    if (error) {
                                        NSLog(@"Error: %@", error.localizedDescription);
                                    } else {
                                        // Processing `data` as you want
                                    }
                                 }];
                                 
// Unsubscribe
[_bleManager unsubscribeNotificationForPeripheral:peripheral
                                forCharacteristic:characteristic];
  1. Read
[_bleManager readPeripheral:peripheral withCharacteristic:characteristic completion:^(NSData *data, NSError *error) {
    // Error is not null if read fail
}];
  1. Write
[_bleManager sendData:data
 toPeripheral:peripheral
 characteristicUUID:characteristicUUID
 completion:^(NSNumber *success, NSError *error) {
    // error indicate the write operation is success or not
 }];
  1. Disconnect
[_bleManager disconnectPeripheral:peripheral
                       completion:^(NSNumber *success, NSError *error) {
                            // Indicate disconnect or not
                       };
  1. Notification
/// Disconnect notification
extern NSString *BLENotificationDisconnected;

/// Scan stop notification
extern NSString *BLENotificationScanStopped;

CHANGELOG

v0.5.0

  1. Do not specify service uuid when discovering services

v0.4.0

  1. Remove the absence interval property
  2. Remove the absence checking logic
  3. Change the scan callback back function to return a peripheral at a time

v0.3.0

  1. Add advertisement data field to BLEPeripheral class, this is helpful for developer to access the latest advertisement data received from the peripheral.

v0.2.1

  1. Fix connect callback invoke before peripheral state changed issue.

v0.2.0

  1. Remove NSAssert from the BLEManager
  2. Support creating multiple connections to different peripherals simultaneously.
  3. Add write operation timer to detect write value timeout

Author

enix223, [email protected]

License

SkyBlue is available under the MIT license. See the LICENSE file for more info.