Jump to content

Powershell to set clock on SonyEricsson phone via Bluetooth

Posted by OLD CS1, 11 March 2019 · 128 views

Powershell to set clock on SonyEricsson phone via Bluetooth

SonyEricsson's PC Suite had a nice feature which would synchronize your phone's clock to your PC's clock when establishing a connection.  I found more often than not my frequently-synchronized computer time differed by a minute or more from the Cingular/AT&T network time.
(quick bitching about Sony)


Since PC Companion no longer synchronizes my phone's clock with my PC, using the SonyEricsson AT Commands reference guide I wrote a Powershell script to take up the slack.  The script opens the Bluetooth COM port set in the first line (no error checking, sorry so lazy) which is COM14 on my laptop, then sends the AT*EDST and AT+CCLK commands to set daylight saving time status and the clock, respectively.  Setting DST is extremely important for setting appointments as I do not enjoy being an hour late or early.
Since Get-Date returns the local time adjusted for DST, the time submitted to the phone must compensate. The IsDaylightSavingTime() return from the Get-Date object is perfect for this as it returns 0 for "not DST" and 1 for "DST," so this is subtracted from the current hour.  The important here trick is to clear DST before setting the time.  Otherwise, the phone accepts the given time as the current time including DST, but since DST has already been accounted for the time gets set an hour off.
*EDST is sent to the phone first, per SE recommendations, then +CCLK.  $seTZ holds the time zone offset, which is the offset from UTC in quarter-hours, calculated by comparing the local time to UTC, adjusting for local DST, then multiplying by 4.  Finally, the real DST setting is sent.
I put one-second pauses between sending each of the three commands to the phone.  Without this pause commands will sometimes get missed.  I suspect this is due to not reading the phone's responses which I do not try to read.  This is a very primitive script which does not check to make sure the port is even open, so why even bother registering an event for reading the port? ;)
$sePort = new-Object System.IO.Ports.SerialPort COM14,9600,None,8,one
Start-Sleep -Seconds 1
$seTime = Get-Date
$seEDST = If ($seTime.IsDaylightSavingTime()) { 1 } else { 0 }
$seTZ = (($seTime-$seTime.ToUniversalTime()).Hours-$seEDST) * 4
$seCommand = "at+cclk=`"$($seTime.AddHours(-$seEDST).ToString("yy/MM/dd,HH:mm:ss"))$($seTZ)`""
Write-Host Time command $seCommand sent to SonyEricsson phone.
Start-Sleep -Seconds 1
Write-Host Setting DST on SonyEricsson phone to `"$seEDST`"
Start-Sleep -Seconds 1
Write-Host ** DONE **