写 CBCharacteristic 的问题
Issue in writing CBCharacteristic
我目前正在开发一个扫描我公司信标的 BLE 应用程序,我需要更新 CBCharacteristic 的值。
写入函数后我没有收到任何错误,但更改没有反映在 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];
}
}
即使我在写入后没有错误地调用了完成委托方法
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
我错过了什么吗?是因为十六进制值吗?
请指导我
谢谢!
可能是特性是只读的。您可以通过查看 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
}
Swift 3 个版本
if((characteristic.properties.contains(CBCharacteristicProperties.write)) || (characteristic.properties.contains(CBCharacteristicProperties.writeWithoutResponse)))
{
//Do your Write here
}
我目前正在开发一个扫描我公司信标的 BLE 应用程序,我需要更新 CBCharacteristic 的值。
写入函数后我没有收到任何错误,但更改没有反映在 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];
}
}
即使我在写入后没有错误地调用了完成委托方法
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
我错过了什么吗?是因为十六进制值吗?
请指导我
谢谢!
可能是特性是只读的。您可以通过查看 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
}
Swift 3 个版本
if((characteristic.properties.contains(CBCharacteristicProperties.write)) || (characteristic.properties.contains(CBCharacteristicProperties.writeWithoutResponse)))
{
//Do your Write here
}