Reputation: 99
I am developing a Bluetooth app. I have connected to the bluetooth device and found the services and characteristics in it. I am facing problem in sending write request from central and receiving response from peripheral. I have written code as below to write data. How will I know that peripheral received that data? Can I show any alert in peripheral. And, in the same way, I want to receive the data from the peripheral so that the central should show an alert that it has got response from peripheral.
Here is the below code I have written
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:
(CBService *)service error:(NSError *)error {
if (!error) {
printf("Characteristics of service with UUID : %s found\r\n",[self
CBUUIDToString:service.UUID]);
for(int i=0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
printf("Found characteristic %s\r\n",[ self CBUUIDToString:c.UUID]);
CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
if([self compareCBUUID:service.UUID UUID2:s.UUID])
{
printf("Finished discovering characteristics");
break;
}
}
}
else {
printf("Characteristic discorvery unsuccessfull !\r\n");
}
}
for writing the value
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:
(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *payloadMessage = @"Hello";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:payload forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
}
and i need to get the version of the ble device i.e GET_VERSION = 0x4a; please help me regarding this or else provide any examples for writing and reading data from the both devices.Thanks in advance
Upvotes: 0
Views: 6272
Reputation: 114846
There are two ways that a Bluetooth central can receive data from a peripheral -
Regards of which method you use, the data will be delivered via a call to your didUpdateValueForCharacteristic:
delegate method.
It seems you are a bit confused as to the purpose of this method as your code issues a write request there, which probably isn't what you want.
When it comes to writing data, you can write the data with or without a response. Your code specifies with response. In this case, when the peripheral acknowledges the write, your didWriteValueForCharacteristic
delegate method is called - this is how you know that the data was received.
Some simple code to implement the behaviour you described in your question is:
-(void)sendData:(NSData *)data {
[self.connectedPeripheral writeValue:data forCharacteristic:self.someCharacteristic type:CBCharacteristicWriteWithResponse];
}
- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrote characteristic" message:@"Successfully wrote characteristic" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
[alert show];
}
else {
NSLog(@"Error writing characteristic %@",error);
}
}
-(void)readCharacteristic {
[self.connectedPeripheral readCharacteristic:self.someOtherCharacteristic];
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Read characteristic" message:[NSString stringWithFormat:@"Successfully read characteristic %@",characteristic.value] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
[alert show];
}
else {
NSLog(@"Error reading characteristic %@",error);
}
}
Upvotes: 3