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;
        }
    }
  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值