« Fixing Apple’s mistakes with other tools: NSDateCountdown beta 2 »

Retrieve SMS from a bluetooth-enabled phone

I have a RAZR and have always wanted to have an archive of the SMS messages that I send and receive. Call it what you will but I just prefer to have a copy of any written communication. I keep all of my emails, IM, and now, my SMS messages.

This is a python script that will retrieve your SMS messages from your bluetooth-enabled phone. Theoretically, this should work with GSM phones but I’ve only tested it on my RAZR. Mileage may vary. It also requires a bit of setup. The full text of the python script that I wrote is available in the extended entry.

First and foremost, you need to install pyserial. On Mac OS X 10.5 Leopard, it should be fairly straightforward. I didn’t have any problems but, again, mileage may vary.

Second, you need to locate the serial port path to your bluetooth phone. You can do this by opening System Preferences and then clicking on Bluetooth preferences. In Bluetooth prefs, click on your phone and then select “Edit Serial Ports…” from the action (gear) menu. The path will be listed at the bottom and likely resemble “/dev/tty.*”. Copy that entire path and replace the serialPath variable in the script.

Now, you should be able to run the script and your SMS files will be saved to a log file in ~/Library/Logs/sms.txt. There’s a line in the script that commented out that you can uncomment if you’d like to delete all of you sms messages automatically. I prefer to do it manually myself in case there’s a problem importing the messages but I left it there just in case.

I run this script using FastScripts. Although it’d also work Apple’s scripts menu. Whenever I want to save my SMS messages, I select it from the scripts menu and I’m ready to roll.

#!/usr/bin/env python

import serial # http://pyserial.sourceforge.net/
import datetime
import os

serialPath = '/dev/tty.Grayson-1'

f = file(os.getenv('HOME')+'/Library/Logs/sms.txt', 'a')
f.write("==========================\n")
f.write(str(datetime.datetime.now())) # Provide a timestamp
f.write("\n\n")

ser = serial.Serial(serialPath, timeout=2)
ser.flushOutput()
ser.write('at+cmgf=1\r') # Set text mode

ser.write('at+cmgl=ALL\r') # List all text messages
while True:
    l = ser.readline().strip()
    if l[:5] == "+CMGL":
        info = l
        message = ser.readline().strip()
        f.write(info + "\n" + message + "\n\n")

    if l == '': break

ser.write('at+cmgf=0\r') # Reset to non-text mode
# ser.write('at+cmgd=1\r') # Delete all messages
ser.close()
f.close()

Leave a Reply