0

I referred Sample app provided by Apple for CoreBluetooth and I succeeded in sending Data from Peripheral to Central, Now I need to write Data from Central to Peripheral. After Googling i found that It can be done using [_discoveredPeripheral writeValue:aData forCharacteristic:charc type:CBCharacteristicWriteWithResponse];

Following is my Implementation of Central, to send Message to peripheral:

-(void)sendMessage:(NSString *)strMessage{
    NSData *aData = [strMessage dataUsingEncoding:NSUTF8StringEncoding];
    CBMutableCharacteristic *charc =[[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyWrite value:nil permissions: CBAttributePermissionsWriteable];
    [_discoveredPeripheral writeValue:aData forCharacteristic:charc type:CBCharacteristicWriteWithResponse];        
}

When I call this method, It is not able to write data, instead of that I can see a CoreBluetooth Warning on console as below,

CoreBluetooth[WARNING] <CBMutableCharacteristic: 0x15e8e420 UUID = 08590F7E-DB05-467E-8757-72F6FAEB13D4, Value = (null), Properties = 0x8, Permissions = 0x2, Descriptors = (null), SubscribedCentrals = (
)> is not a valid characteristic for peripheral <CBPeripheral: 0x15d91250 identifier = 37C314AF-FDB3-1F24-6937-8780B97AAB45, Name = "iPad", state = disconnected>

It would be nice if someone give the best way to get Object of Peripheral and how to initiate sending of data from Central to Peripheral.

EDIT I have tried the way that is shown in How to get the characteristic from UUID in objective-C?

But, In this case I am not able to Loop Services whenever I try for that It returns services=nil.

Community
  • 1
  • 1
Mrug
  • 4,963
  • 2
  • 31
  • 53
  • You have to scan the characteristics, save it in a iVar, and then use this one to do: `[_discoveredPeripheral writeValue:data forCharacteristic:charcFoundByScan type:CBCharacteristicWriteWithResponse];` – Larme Mar 17 '15 at 09:22
  • possible duplicate of [How to get the characteristic from UUID in objective-C?](http://stackoverflow.com/questions/28737179/how-to-get-the-characteristic-from-uuid-in-objective-c) – Larme Mar 17 '15 at 09:23
  • @Larme: I tried this too, but for traversing services when I loop _discoveredPeripheral.services it return count 0. So not able to proceed loop. – Mrug Mar 17 '15 at 09:27
  • 1
    You need to show this code. When you do `_discoveredPeripheral = peripheral;` in `didDiscoverPeripheral:`, you need to do: `[_discoveredPeripheral scanForServices:nil]`; And then loop again in the delegate method etc. You should look at the Apple's sample code (Thermometer, etc.) about CoreBluetooth. – Larme Mar 17 '15 at 09:29
  • 1
    Thanks, I tried it and now atleast, now I can make successful call of writeValue. But now new issue has arrived. In Delegate "didWriteValueForCharacteristic" I am receiving Error. `Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo=0x14ea3e10 {NSLocalizedDescription=Unknown error.}` – Mrug Mar 17 '15 at 10:34
  • Does the characteristic supports "WithResponse"? – Larme Mar 17 '15 at 11:51
  • Yes, Its done bro. The issue was related to permission. It was read, where as, i should use write permission.BTW Thanks @Larme – Mrug Mar 17 '15 at 12:06

0 Answers0