2018-06-05 iOS蓝牙开发

初始化

  • 启动中心管理器
myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil
;

queue设为nil,中心管理器在主线程中派发事件。

  • 跟踪设备蓝牙状态

中心管理器创建成功后,会调用代理的centralManagerDidUpdateState:方法,来跟踪反馈蓝牙设备的状态。打开/关闭蓝牙,都会调用这里。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

扫描并连接

  • 启动扫描
[myCentralManager scanForPeripheralsWithServices:nil options:nil];

第一个参数设为nil,设备会扫描并获取所有蓝牙广播。
关闭蓝牙后,会停止扫描。但打开蓝牙,自动开始扫描。重启app,依然自动开始扫描。

  • 扫描到外设
- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {
 
    NSLog(@"Discovered %@", peripheral.name);
    self.discoveredPeripheral = peripheral;
  • 停止扫描
[myCentralManager stopScan];
  • 连接设备
[myCentralManager connectPeripheral:peripheral options:nil];
  • 连接成功
(void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral connected");
peripheral.delegate = self;

查询服务

  • 获取外设提供的服务
[peripheral discoverServices:nil];

因为数据包长度限制的原因,蓝牙外设广播的服务是其能提供服务的一部分。如果传nil参数,则是获取外设能提供的所有服务。为了节约电池性能,建议这里不要传入nil,而是传入想获取的服务UUID。

[self.peripheral discoverServices:@[[CBUUID UUIDWithString:self.connectPeripheralUUID]]];
  • 取到服务
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverServices:(NSError *)error
{
   for (CBService *service in peripheral.services) 
   {      
        NSLog(@"Discovered service %@", service);        
        ...    
  }    
  ...
}

查询/订阅特征

  • 发现外设提供的特征
[peripheral discoverCharacteristics:nil forService:interestingService];

同发现设备一样,不建议第一个参数传nil。

  • 获取到外设服务的特征
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
             error:(NSError *)error 
{
    for (CBCharacteristic *characteristic in service.characteristics)
    {
        NSLog(@"Discovered characteristic %@", characteristic);
        ...
    }
    ...
}
  • 获取需要的特征值
[peripheral readValueForCharacteristic:interestingCharacteristic];
  • 获取到特征值
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
    NSData *data = characteristic.value;
    // parse the data as needed
    ...
}

并不是所有的特征值都是可读的,需要查看其属性是否包含 CBCharacteristicPropertyRead。读取不可读的特征值时,peripheral:didUpdateValueForCharacteristic:error:方法会报错。

  • 订阅特征值-适合需要动态更新的特征值,如心率仪
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
  • 订阅成功
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
    if (error) {
        NSLog(@"Error changing notification state: %@",
           [error localizedDescription]);
    }
    ...

查看特征是否包含CBCharacteristicPropertyNotify / CBCharacteristicPropertyIndicate
属性。包含该属性的特征能被订阅。

  • 订阅特征值有变化
[peripheral:didUpdateValueForCharacteristic:error:]

写入数据

  • 特征值
[peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic
        type:CBCharacteristicWriteWithResponse];

写入特征值的时候,若传入type为CBCharacteristicWriteWithResponse,则外设在成功后会通知中心管理器写入成功。

- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
 
    if (error) {
        NSLog(@"Error writing characteristic value: %@",
            [error localizedDescription]);
    }
    ...

若传入type为 CBCharacteristicWriteWithoutResponse,则不会有回调动作。

react native 蓝牙https://blog.csdn.net/withings/article/details/71378562?locationNum=3&fps=1

你可能感兴趣的:(2018-06-05 iOS蓝牙开发)