0
votes

I need to edit the advertisement data of bluetooth peripheral from central manager.

i tried a lot..

The following code provides the details :

1.After the Peripheral connection:

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"Connection successfull to peripheral: %@",peripheral);
    peripheral.delegate = self;
    [peripheral discoverServices:nil];
    //Do somenthing after successfull connection.
}

2.Discovering Services:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

    for (CBService *service in peripheral.services) {
        NSLog(@"Discovering characteristics for service %@", service);
        [peripheral discoverCharacteristics:nil forService:service];
    }

}

3.Discovering characteristics from service:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {

    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"B0702880-A295-A8AB-F734-031A98A512DE"]]) {
            [peripheral readValueForCharacteristic:characteristic];
            NSLog(@"Reading value for characteristic %@", characteristic);
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}

4.Updating Notification State:

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


    NSLog(@"characteristic.properties--------------------->%lu",(unsigned long)characteristic.properties);


    if (error) {
        NSLog(@"Error changing notification state: %@",[error localizedDescription]);
    }
    // Notification has started
    if (characteristic.isNotifying) {
        NSLog(@"Notification began on %@", characteristic);
    }

    NSString* decodeString = @"teststring";
    NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"to write----- %@",encodeData);


    if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
        (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
    {
        [peripheral writeValue:encodeData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }
    else
    {
        NSLog(@"Not permit to write");
    }
}

5.Update Write value in Peripheral:

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

    if (error) {
        NSLog(@"Error writing characteristic value: %@",[error localizedDescription]);
    }

    NSData *data = characteristic.value;
    NSLog(@"FinalData:%@",data);
}

i am new to IOS.Helps are appreciated

thanks in advance..

1
What do want to do to the advertisement data? None of the code you have shown seems relevant to your question. Also, the peripheral advertises so how would you change it from the central?Paulw11
I assume you want to modify the advertisement from central to peripheral, i.e. the advertisement data would be changed next time. so this is a simple service or character like other profiles. now what's your question? did you transfer the data to device? did your device updated the data?Guo Xingmin
i want to update the advertisement data from my central device..Also i need to store some value in peripheral device.Muruganandam Sathasivam

1 Answers

2
votes

There is no general way of setting advertising data on the peripheral from the central. If you want to do something like this you either have to implement the feature on the peripheral (through a purpose made GATT Service), or this feature is offered by the product somehow.

Also note that advertising is a link layer (LL) feature, and those are normally not exposed by iOS. The iOS APIs for BLE is GAP/GATT level.