BlocksBluetooth 0.0.3

BlocksBluetooth 0.0.3

测试已测试
语言语言 Object-CObjective C
许可 MIT
发布最新发布2016年6月

Joseph LinJoseph Lin维护。



  • 作者:
  • Joseph Lin

基于块的核心蓝牙分类,让你的生活更简单。

这是一个预发布库。v0.0.1主要关注CBCentralManagerCBPeripheral,针对执行中心角色的设备。另一方面,这次发布仅涵盖了基本的CBPeripheralManager任务。

示例

BLE外设的扫描

[[CBCentralManager defaultManager] scanForPeripheralsWithServices:nil options:nil didDiscover:^(CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
    // Handle the returned peripheral
}];

连接到一个外设,然后递归地发现其所有服务和属性

__weak typeof(self) weakSelf = self;
[[CBCentralManager defaultManager] connectPeripheral:self.peripheral options:nil didConnect:^(CBPeripheral *peripheral, NSError *error) {

    weakSelf.stateLabel.text = weakSelf.peripheral.stateString;

    // 1A. Read RSSI
    [peripheral readRSSIAndOnUpdate:^(CBPeripheral *peripheral, NSError *error) {
        weakSelf.RSSILabel.text = [NSString stringWithFormat:@"RSSI: %@", peripheral.RSSI ?: @"--"];
    }];

    // 1B. Discover Services
    [peripheral discoverServices:nil didDiscover:^(NSArray *services, NSError *error) {

        [weakSelf.tableView reloadData];

        for (CBService *service in services) {

            // 2A. Discover Characteristics
            [peripheral discoverCharacteristics:nil forService:service didDiscover:^(NSArray *characteristics, NSError *error) {
                [weakSelf.tableView beginUpdates];
                NSUInteger index = [peripheral.services indexOfObject:service];
                NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
                [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                [weakSelf.tableView endUpdates];

                for (CBCharacteristic *characteristic in characteristics) {

                    // 3A. Read Value For Characteristic
                    [peripheral readValueForCharacteristic:characteristic didUpdate:^(CBCharacteristic *characteristic, NSError *error) {
                        [weakSelf updateTableViewForCharacteristic:characteristic];
                    }];

                    // 3A. Discover Descriptors For Characteristic
                    [peripheral discoverDescriptorsForCharacteristic:characteristic didDiscover:^(NSArray *descriptors, NSError *error) {
                        [weakSelf updateTableViewForCharacteristic:characteristic];
                    }];
                }
            }];

            // 2B. Discover Included Services
            [peripheral discoverIncludedServices:nil forService:service didDiscover:^(NSArray *services, NSError *error) {
                //TODO:
            }];
        }
    }];

} didDisconnect:^(CBPeripheral *peripheral, NSError *error) {

    weakSelf.stateLabel.text = weakSelf.peripheral.stateString;
    [[[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] show];
}];