This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to update BLE device name in ios swift !

I would like to update ble device name from device1 to user2. How i can do this in swift language . i m atrung to update by ios app.

is below way is right? but not able to update

let hexData = "user2"?.dataFromHexString()

peripheral.writeValue(data, forCharacteristic: characteric, type: CBCharacteristicWriteType.WithResponse)

  • Thanks All,

    Successfully updated device name, aplha value and date on ble device...just was missing sequence :(

    Below is the code: for future reference

    @IBAction func updateDevicePressed(){

        self.showActivityView()
        self.updateButton.isEnabled = false
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.updateRegistrationNumber()
        }
    }
    
    func updateRegistrationNumber(){
        
        let hexValReg = deviceName.unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined()
        print(hexValReg)
        let regData = self.dataWithHexString(hex: hexValReg)// 50756E65 pune
        
        deviceNameCharacterToUpdate?.service.peripheral.writeValue(regData as Data, for: deviceNameCharacterToUpdate!, type: CBCharacteristicWriteType.withResponse)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.updateLocation()
        } 
    }
    
    func updateLocation(){
        
        let hex = location.unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined()
        let dataLoca = self.dataWithHexString(hex: hex)
    
        locationCharacterToUpdate?.service.peripheral.writeValue(dataLoca as Data, for: locationCharacterToUpdate!, type: CBCharacteristicWriteType.withResponse)
      
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.updateExpireDate()
        }
    }
    
    func updateExpireDate(){
        
        let data = self.dataWithHexString(hex: hexDateToWrite)// 5976C3A3
        dateCharacterToUpdate?.service.peripheral.writeValue(data, for: dateCharacterToUpdate!, type: CBCharacteristicWriteType.withResponse)
    
              self.updateButton.isEnabled = true
              self.hideActivityView()
    
    }
    
Related