BLE

BLE是蓝牙低能耗的简称(Bluetooh Low Energy)。蓝牙低能耗(BLE)技术是低成本、短距离、可互操作的鲁棒性无线技术,工作在免许可的2.4GHz ISM射频频段。它从一开始就设计为超低功耗(ULP)无线技术。它利用许多智能手段最大限度地降低功耗

  • GATT:Generic Attribute Profile
  • ATT: Attribute Protocol
  • L2CAP: Logical Link Control and Adaptation Protocol

CoreBlueTooth

  • CBCentralManager - 中心设备管理,用于搜索周边设备,对应CBPeripheral使用
  • CBPeripheralManager - 周边设备管理,用于作为周边设备,对应CBCentral使用

  • CBPeripheral - 周边设备
  • CBCentral - 中心设备

  • CBService - 设备 服务
  • CBCharacteristic - 设备 服务 特征
  • CBDescriptor - 设备 服务 特征 描述

  • CBError - 错误
  • CBUUID - 唯一码
  • CBAdvertisementData - 广播包
  • CBATTRequest - ATT请求

CBCentralManager

初始化 manager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
self.centerManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    // [self.centerManager stopScan];  可以停止扫描

    - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBCentralManagerStatePoweredOn) {
        NSLog(@"蓝牙 - 打开");
        // 开始扫描,周边设备
        [self.centerManager scanForPeripheralsWithServices:nil options:nil];
    } else {
        NSLog(@"蓝牙 异常,其他状态自行判断");
    }
    }

    - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict {
        NSLog(@"%@",dict);
    }

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
    if (peripheral.name) {
        NSLog(@"扫描到设备 %@",peripheral.name);
    }
    }

调用连接设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[self.centerManager connectPeripheral:currentPer options:nil];                       
    currentPer.delegate = self;

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
        NSLog(@"链接成功");
    }

    - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
        NSLog(@"链接失败");
    }

    - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
        NSLog(@"设备断开链接");
    }

CBPeripheral

扫描 设备的某些 UUID 的服务

1
2
3
4
5
[currentPer discoverServices:@[@"UUID"]];

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {
        NSLog(@"%@",service.UUID);
    }

扫描 设备的某服务的 UUID 服务

1
2
3
4
[currentPer discoverIncludedServices:@[] forService:service];
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error {
        NSLog(@"%@",service.UUID);
    }

扫描 设备的某个服务中的 UUID 特性

1
2
3
4
[peripheral discoverCharacteristics:@[] forService:peripheral.services.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {
        NSLog(@"%@",service.characteristics);
    }

扫描 设备的某个特征 UUID

1
2
3
4
[peripheral discoverDescriptorsForCharacteristic:peripheral.services.lastObject.characteristics.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
        NSLog(@"%@",characteristic);
    }

获取设备 蓝牙信号强度

1
2
3
4
[peripheral readRSSI];
    - (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {
        NSLog(@"%@",RSSI.stringValue);
    }

读取 特征

1
2
3
4
[peripheral readValueForCharacteristic:peripheral.services.lastObject.characteristics.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
        NSLog(@"%@",characteristic);
    }

写入 特征

1
2
3
4
[peripheral writeValue:[NSData data] forCharacteristic:peripheral.services.lastObject.characteristics.lastObject type:CBCharacteristicWriteWithResponse];
    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
        NSLog(@"%@",characteristic);
    }

读取 描述

1
2
3
4
[peripheral readValueForDescriptor:peripheral.services.lastObject.characteristics.lastObject.descriptors.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
        NSLog(@"%@",descriptor);
    }

写入描述

1
2
3
4
[peripheral writeValue:[NSData data] forDescriptor:peripheral.services.lastObject.characteristics.lastObject.descriptors.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
        NSLog(@"%@",descriptor);
    }

添加 监听

1
2
3
4
[peripheral setNotifyValue:YES forCharacteristic:peripheral.services.lastObject.characteristics.lastObject];
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
        NSLog(@"%@",characteristic);
    }

其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 最大数据量?
    NSLog(@"%zi",[peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse]);
    
    // 修改 名称
    - (void)peripheralDidUpdateName:(CBPeripheral *)peripheral {
    NSLog(@"%@",peripheral);
    }

    // 修改 服务
    - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices {
    NSLog(@"%@",peripheral);
    }

    // 修改 RSSI
    - (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    NSLog(@"%@",peripheral.RSSI.stringValue);
    }