LGBluetooth =========== Simple, block-based, lightweight library over CoreBluetooth.
- (IBAction)testPressed:(UIButton *)sender
{
[[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4
completion:^(NSArray *peripherals)
{
if (peripherals.count) {
[self testPeripheral:peripherals[0]];
}
}];
}
- (void)testPeripheral:(LGPeripheral *)peripheral
{
// First of all, opening connection
[peripheral connectWithCompletion:^(NSError *error) {
// Discovering services of peripheral
[peripheral discoverServicesWithCompletion:^(NSArray *services, NSError *error) {
// Searching in all services, our - 5ec0 service
for (LGService *service in services) {
if ([service.UUIDString isEqualToString:@"5ec0"]) {
// Discovering characteristics of 5ec0 service
[service discoverCharacteristicsWithCompletion:^(NSArray *characteristics, NSError *error) {
__block int i = 0;
// Searching writable characteristic - cef9
for (LGCharacteristic *charact in characteristics) {
if ([charact.UUIDString isEqualToString:@"cef9"]) {
[charact writeByte:0xFF completion:^(NSError *error) {
if (++i == 3) {
[peripheral disconnectWithCompletion:nil];
}
}];
} else {
// Otherwise reading value
[charact readValueWithBlock:^(NSData *data, NSError *error) {
if (++i == 3) {
[peripheral disconnectWithCompletion:nil];
}
}];
}
}
}];
}
}
}];
}];
}
After running code we can see the result.
Connection with error - (null) Service discovered - Battery Service discovered - Current Time Service discovered - Unknown (5ec0) Characteristic discovered - Unknown (cef9) Characteristic discovered - Unknown (f045) Characteristic discovered - Unknown (8fdb) Characteristic - Unknown (cef9) wrote with error - (null) Characteristic - Unknown (f045) value - 1234567890 error - Characteristic - Unknown (8fdb) value - 11111111111 error - (null) Disconnect with error - (null)
[LGUtils readDataFromCharactUUID:@"f045"
serviceUUID:@"5ec0"
peripheral:peripheral
completion:^(NSData *data, NSError *error) {
NSLog(@"Data : %s Error : %@", (char *)[data bytes], error);
}];
Write example
int8_t dataToWrite = 0xFF;
[LGUtils writeData:[NSData dataWithBytes:&dataToWrite length:sizeof(dataToWrite)]
charactUUID:@"cef9"
serviceUUID:@"5ec0"
peripheral:peripheral completion:^(NSError *error) {
NSLog(@"Error : %@", error);
}];