0Write major value
suggest change- You need to discover the services and characteristics.
- You don’t need to read a value from the characteristic before writing over it.
- This example continues after a read. Modify
peripheral(_:didUpdateValueFor:error:). - Add a
new_majorvariable and areset_characteristicvariable.
var reset_characteristic : CBCharacteristic!
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for characteristic in service.characteristics! {
print("Characteristic: \(characteristic)\n error: \(error)")
if(characteristic.uuid.uuidString == "FFF2"){
peripheral.readValue(for: characteristic)
}
if(characteristic.uuid.uuidString == "FFFF"){
reset_characteristic = characteristic
}
}
}
let new_major : UInt16 = 100
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("Characteristic read: \(characteristic)\n error: \(error)")
let major = UInt16.init(bigEndian: UInt16(data: characteristic.value!)!)
print("major: \(major)")
peripheral.writeValue(new_major.data, for: characteristic, type: CBCharacteristicWriteType.withResponse)
}
- The phone is little-endian. Core Bluetooth does not swap bytes for you. This MINEW device (NRF51822, ARM) expects the major value in big-endian order, so the value is byte-swapped before it is written.
- The BLE device documentation says what each characteristic reads and writes, and whether a write uses
CBCharacteristicWriteType.withResponse.
UInt16(data:) and UInt16.data are not in the Swift standard library. This sample assumes an extension, not shown here, that converts between UInt16 and Data. service.characteristics is force-unwrapped and is nil until discovery succeeds.
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
print("Characteristic write: \(characteristic)\n error: \(error)")
if(characteristic.uuid.uuidString == "FFF2"){
print("Resetting")
peripheral.writeValue("minew123".data(using: String.Encoding.utf8)!, for: reset_characteristic, type: CBCharacteristicWriteType.withResponse)
}
if(characteristic.uuid.uuidString == "FFFF"){
print("Reboot finish")
cb_manager.cancelPeripheralConnection(peripheral)
}
}
- To update GATT server information you have to reboot the device programmatically, or save the data and power-cycle it manually.
FFFFis the characteristic that does that on this device.minew123is the default password to reboot or save information in this case.- Run the app and watch the console for errors. You will not see the new value yet.
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("Characteristic read: \(characteristic)\n error: \(error)")
let major = UInt16.init(bigEndian: UInt16(data: characteristic.value!)!)
print("major: \(major)")
//peripheral.writeValue(new_major.data, for: characteristic, type: CBCharacteristicWriteType.withResponse)
}
- Last step: comment out the write in
didUpdateValueForand run the app again. You should then see the new value.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents