iOS central writing is not permitted

删除回忆录丶 提交于 2021-02-19 06:07:31

问题


I am a ios developer. I can take a value from arduino sensor. But i cannot send a message by using the following the method.

[peripheral writeValue:dataToWrite forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

"dataToWrite" value is alloc by using NSString*

NSString* data = @"1";
NSData* dataToWrite = [data dataUsingEncoding:NSUTF8StringEncoding];

and the following code is full code of "DiscoverCharacteristics in service"

//DISCOVER CHAR
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {NSLog(@"DISCOVER_CHAR - Error");return;}

    NSString* data = @"1";
    NSData* dataToWrite = [data dataUsingEncoding:NSUTF8StringEncoding];

    for (CBCharacteristic * characteristic in service.characteristics) {
        NSLog(@"DISCOVER_CHAR - Characteristic : %@",characteristic);
        [peripheral writeValue:dataToWrite forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }
}

In this point, I want to summarize my question. My Question is "Even i used the [Peripheral writeValue:forCharacteristic:type] Method. why an error message is shown in log monitor? " Like "Writing is not permitted."

Do i need to get some permission to writing the message for an Arduino? OR I have to change my following code? OR I have a problem in Arduino(Acutally, Arduino can get a message from other device... so, Arduino source code is fine. maybe...)

NSString* data = @"1";
NSData* dataToWrite = [data dataUsingEncoding:NSUTF8StringEncoding];

I'm a bit of a bluetooth noob. So it's probably something obvious I've overlooked but any help would be much appreciated.

Thank you!:)


回答1:


Thank you a lot!!!!!!! Paulw11. Finally, i sent a data from iphone to Arduino. what i checked properties of my characteristic is <CBCharacteristic: 0x13564a680, UUID = FFE1, properties = 0x16, value = (null), notifying = NO>. My characteristic's properties = 0x16. but i can not find a 0x16 in enum of properties. Still, I don't know the 0x16 meaning. Someone said that "Values representing the possible properties of a characteristic. Since characteristic properties can be combined, a characteristic may have multiple property values set." URL: Interpret Characteristic Properties (iOS and BLE) . Anyway I could find a solution by PaulW11 help! How i can do it? Here is my way of solution. At first, My method is [peripheral writeValue:dataToWrite forCharacteristic:characteristic *type:CBCharacteristicWriteWithResponse]*;. The problem code is type field. I had to understand the type. The type has a meaning of write, read and so on. So, I changed the type from CBCharacteristicWriteWithResponse to CBCharacteristicPropertyWrite. That is what i did. [peripheral writeValue:dataToWrite forCharacteristic:characteristic *type:CBCharacteristicPropertyWrite*]; Thank you again! Paulw11




回答2:


try to print error inside following method:

-(void)peripheral:(CBPeripheral *)peripheral
    didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error 
{ 
    NSLog(@"%@",error.localizedDescription);
}

If the error is "Writing is not permitted", then you might not writing the data correctly.Convert the string value in this format

NSData* data = [@"string_to_write" dataUsingEncoding:NSASCIIStringEncoding];

And try this:

[self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse];

Hope it works!!



来源:https://stackoverflow.com/questions/32918988/ios-central-writing-is-not-permitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!