2
votes

I am working on an iPhone app and need to write value on the bluetooth low energy device that support Immediate Alert service using CoreBluetooth framework on iOS 7.0 and 8.0. Connection with the device working perfectly but whenever i try to write a value on the device nothing is happening (the value is not saved on device. So, device is not ringing). Below is the code used for writing value:

+ (void)writeValueForCharacteristic:(CBCharacteristic *)characteristic peripheral:(CBPeripheral *)peripheral alarm:(BOOL)alarm
{    
    NSData  * data = nil;
    if (alarm) {
        uint8_t value = 2;
        data = [NSData dataWithBytes:&value length:sizeof(value)];
    } else {
        uint8_t value = 0;
        data = [NSData dataWithBytes:&value length:sizeof(value)];
    }
    [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}

Logs and status of device before the execution above code:

- (void)beginAlarm
{
    NSLog(@"characteristic : %@",discAlertCharacteristic);
    NSLog(@"peripheral : %@",discPeripheral);
    NSLog(@"service : %@",service);

    [BluetoothUtility writeValueForCharacteristic:discAlertCharacteristic peripheral:discPeripheral alarm:YES];
}
characteristic : CBCharacteristic: 0x146ea8d0, UUID = 2A06, properties = 0x14, value = (null), notifying = NO
service : CBService: 0x146ecc90, isPrimary = YES, UUID = 1802
peripheral : CBPeripheral: 0x146dd110, identifier = B0A195DC-7273-B3F5-BA70-0219A61F8904, name = LA-TAG a87, state = connected

I have tested my Bluetooth device using BLE Utility app and its working properly.

1
What do you mean by "nothing is happening"? The value is not saved to the peripheral?Michał Ciuba
Also please show the code which prints the logs you posted (in which methods are the logs printed?).Michał Ciuba
thanks Michal. I've updated question.Indra Mohan
Your code looks fine. Do you call discoverCharacteristics:forService: on peripheral before beginAlarm is called?Michał Ciuba
Yes, I've called discoverCharacteristics:forService method before beginAlarmIndra Mohan

1 Answers

0
votes
if (getCharacteristic.properties.rawValue == 0xE){

              let sentValue:[UInt8] = [0x38, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x02]

              let sentData = Data(bytes: sentValue)

              self.peripheralDevice.writeValue(sentData, for: getCharacteristic, type: CBCharacteristicWriteType.withResponse)

 }