ios 蓝牙连接

1、蓝牙订阅写入问题

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error{
    NSLog(@"didDiscoverCharacteristicsForService 发现外设特征");
    //    NSLog(@"Find device UUIDC:%@", peripheral.identifier.UUIDString);
    //
    for (CBCharacteristic *cha in service.characteristics) {
     /**会发现两个特征,只能监听一个,否则每发送命令后每次会多出一些响应,并且5f01 不能read,所以不能调用readValueForCharacteristic方法,会报错*/
        if([[cha.UUID.UUIDString lowercaseString] isEqualToString:@"5f01"]){
            //订阅一个特征的值
            [self.connectPeripheral setNotifyValue:YES forCharacteristic:cha];
            self.nowCBCharacteristic = cha;
            /**
             判断当前订阅的外设特征有什么属性,是否可写入等 都在cha.properties中,注意判断时是一个&
             CBCharacteristicPropertyBroadcast: 允许一个广播特性值,用于描述特性配置,不允许本地特性
             CBCharacteristicPropertyRead: 允许读一个特性值
             CBCharacteristicPropertyWriteWithoutResponse: 允许写一个特性值,没有反馈
             CBCharacteristicPropertyWrite: 允许写一个特性值
             CBCharacteristicPropertyNotify: 允许通知一个特性值,没有反馈
             CBCharacteristicPropertyIndicate: 允许标识一个特性值
             CBCharacteristicPropertyAuthenticatedSignedWrites: 允许签名一个可写的特性值
             CBCharacteristicPropertyExtendedProperties: 如果设置后,附加特性属性为一个扩展的属性说明,不允许本地特性
             BCharacteristicPropertyNotifyEncryptionRequired: 如果设置后,仅允许信任的设备可以打开通知特性值
             
             拥有哪个在写入信息的时候就传什么,否则会没有反馈或者直接失败
             - (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
             */
            if (cha.properties & CBCharacteristicPropertyWriteWithoutResponse){
                self.wirteType = CBCharacteristicWriteWithoutResponse;
            }else{
                self.wirteType = CBCharacteristicWriteWithResponse;
            }
            break;
        }
    }
  
}

你可能感兴趣的:(ios,cocoa,macos)