SkyBlue
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
- 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;
- 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
}];
}];
- 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
}
}];
- You can access the connected device with
connectedPeripherals
, this is a NSDictionary<NSUUID *, BLEPeripheral *> to keep the connected peripherals. The key is the peripheral UUID.
_bleManager.connectedPeripherals
- 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];
- Read
[_bleManager readPeripheral:peripheral withCharacteristic:characteristic completion:^(NSData *data, NSError *error) {
// Error is not null if read fail
}];
- Write
[_bleManager sendData:data
toPeripheral:peripheral
characteristicUUID:characteristicUUID
completion:^(NSNumber *success, NSError *error) {
// error indicate the write operation is success or not
}];
- Disconnect
[_bleManager disconnectPeripheral:peripheral
completion:^(NSNumber *success, NSError *error) {
// Indicate disconnect or not
};
- Notification
/// Disconnect notification
extern NSString *BLENotificationDisconnected;
/// Scan stop notification
extern NSString *BLENotificationScanStopped;
CHANGELOG
v0.5.0
- Do not specify service uuid when discovering services
v0.4.0
- Remove the absence interval property
- Remove the absence checking logic
- Change the scan callback back function to return a peripheral at a time
v0.3.0
- 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
- Fix connect callback invoke before peripheral state changed issue.
v0.2.0
- Remove NSAssert from the BLEManager
- Support creating multiple connections to different peripherals simultaneously.
- 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.