A simple bluetooth manager block based to interact with peripherals for iOS
Just use cocoapods pod 'FABluetoothManager'
Or add the FABluetoothManager.{h.m}
files to your project
See CocoaPods
#import FABluetoothManager.h
FABluetoothManager *manager = [FABluetoothManager manager];
To start searching for peripherals use the function bellow:
Note : We're using nil as a value for services to find all the peripherals but this is something you won't do on a real enviroment. See Apple Bluetooth
[manager startSearchingForPeriphetals:^(NSArray *peripherals, NSError *error) {
NSLog(@"%@",peripherals);
} services:nil];
Before do any kind of comunication we need to connect to it.
ConnetPeripheralBlock completionBlock = ^(CBPeripheralState state, NSError* error){
if (state == CBPeripheralStateConnected) {
// Optional you can look for services
// [self discoverServices:nil
// peripheral:peripheral
// completion:servicesBlock];
} else {
block(nil, error);
}
};
[manager connectToPeripheral:peripheral completion:completionBlock];
ServicesBlock servicesBlock = ^(NSArray* services, NSError *error){
if (!error) {
NSLog(@"%@",services);
} else {
block(nil, error);
}
};
[manager discoverServices:@[service] // NSArray of CBUUID
peripheral:peripheral
completion:servicesBlock];
[manager writeValue:nsData //NSData to be written
characteristic:characteristic // CBCharacteristic
service:service // CBService
periphera:peripheral // CBPeripheral
completion:^(NSData* data, NSError *error){
[self.centralManager cancelPeripheralConnection:peripheral]; // After write all the information don't forget to disconnect the peripheral
block(data,error);
}
disconnect:nil]; // Handle the disconnect error
- Finish the README.
- Fix typos
Fernando Arellano (fernando.faa@gmail.com)