gpt4 book ai didi

ios - 写 CBCharacteristic 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:41 25 4
gpt4 key购买 nike

我目前正在开发扫描我公司信标的 BLE 应用程序,我需要更新 CBCharacteristic 的值。

我在写入函数后没有收到任何错误,但更改并未反射(reflect)在 BLE 设备上。

我已经扫描了所有 CBCharacteristics,然后遍历得到特定的并调用写入函数

for (CBCharacteristic* characteristic in beaconCharacteristics)
{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]]) {
[characteristic.service.peripheral writeValue:[@"BeaconE" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
}

即使我在写入后没有错误地调用了完成委托(delegate)方法

-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}

我错过了什么吗?是因为十六进制值吗?

请指导我

谢谢!

最佳答案

Characteristic 可能是只读的。您可以通过查看 CBCharacteristicProperties 对象找到答案,您可以使用 characteristic.properties 获取该对象

//Check if the Characteristic is writable
if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
{
//Do your Write here
}

下面是一个简单的方法,它接受一个 CBCharacteristicProperties 对象并记录属性。 . .帮助调试。

-(void)logCharacteristicProperties:(CBCharacteristicProperties)properties {

if (properties & CBCharacteristicPropertyBroadcast) {
NSLog(@"CBCharacteristicPropertyBroadcast");
}
if (properties & CBCharacteristicPropertyRead) {
NSLog(@"CBCharacteristicPropertyRead");
}
if (properties & CBCharacteristicPropertyWriteWithoutResponse) {
NSLog(@"CBCharacteristicPropertyWriteWithoutResponse");
}
if (properties & CBCharacteristicPropertyWrite) {
NSLog(@"CBCharacteristicPropertyWrite");
}
if (properties & CBCharacteristicPropertyNotify) {
NSLog(@"CBCharacteristicPropertyNotify");
}
if (properties & CBCharacteristicPropertyIndicate) {
NSLog(@"CBCharacteristicPropertyIndicate");
}
if (properties & CBCharacteristicPropertyAuthenticatedSignedWrites) {
NSLog(@"CBCharacteristicPropertyAuthenticatedSignedWrites");
}
if (properties & CBCharacteristicPropertyExtendedProperties) {
NSLog(@"CBCharacteristicPropertyExtendedProperties");
}
if (properties & CBCharacteristicPropertyNotifyEncryptionRequired) {
NSLog(@"CBCharacteristicPropertyNotifyEncryptionRequired");
}
if (properties & CBCharacteristicPropertyIndicateEncryptionRequired) {
NSLog(@"CBCharacteristicPropertyIndicateEncryptionRequired");
}
}

此外,您应该检查错误以查看 CoreBluetooth 是否抛出错误。

//CBPeripheral Delegate
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

if (error) {
NSLog(@"<error> didWriteValueForCharacteristic %@",[error description]);
//
// Add Error handling for failed Writes
//
}

//Add Handling for successful writes

}

关于ios - 写 CBCharacteristic 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com