views:

478

answers:

1

I'm trying to write a simple module that will enable sending SMS. I using bluetooth to connect to the mobile using the below example:

file: bt-sendsms.py

import bluetooth

target = '00:32:AC:32:36:E8' #  Mobile address 
print "Trying to send SMS on %s" %  target

BTSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
BTSocket.connect((target, 2)) # BT Address
BTSocket.send('ATZ\r')
BTSocket.send('AT+CMGF=1\r')
#sockfd.send('AT+CSCA="+972547716507"\r') # This line changes the SMSC address - do not modify unless required 
BTSocket.send('AT+CMGS="+972547877763"\r') # TO Phone Number
BTSocket.send('This is a test message - port 2.\n')
BTSocket.send(chr(26)) # CTRL+Z

print "SMS sent"
sockfd.close()
print "Closed"

My problem is that I'm unable to verify or get an error code for the SMS sending or any of the socket operation.

Any referral to the right direction will be appreciated

+1  A: 

From the Python you look like you are opening any old RFCOMM channel and hoping it will magically take the AT commands and do the messaging.

I think (and I could be wrong) that you need to connect to a specific profile/sevice channel and I think for SMS it is the the Messaging Access Profile (MAP), which is not yet standardised so finding a phone with it on, well I won't say impossible but very, very unlikely. Otherwise, some phones will support AT commands for messaging but this is outside the specs e.g. I have it on authority that Sony-Ericson phones will support it though the Dial-Up Networking profile (DUN).

So, first of all, does your mobile device support some out of spec AT commands for SMS and if so, on a certain profile or on an ad-hoc proprietary one? Next, you need to connect to that profile.

You can browse the supported services etc... using the following Python (checks all surrounding BT devices)...

import bluetooth

def whats_nearby():
    name_by_addr = {}
    nearby = bluetooth.discover_devices(flush_cache=True)
    for bd_addr in nearby:
        name = bluetooth.lookup_name( bd_addr, 5)
        print bd_addr, name
        name_by_addr[bd_addr] = name
    return name_by_addr

def what_services( addr, name ):
    print " %s - %s" % ( addr, name )
    for services in bluetooth.find_service(address = addr): 
        print "\t Name:           %s" % (services["name"]) 
        print "\t Description:    %s" % (services["description"]) 
        print "\t Protocol:       %s" % (services["protocol"]) 
        print "\t Provider:       %s" % (services["provider"]) 
        print "\t Port:           %s" % (services["port"]) 
        print "\t service-classes %s" % (services["service-classes"])
        print "\t profiles        %s" % (services["profiles"])
        print "\t Service id:  %s" % (services["service-id"]) 
        print "" 

if __name__ == "__main__":
    name_by_addr = whats_nearby()
    for addr in name_by_addr.keys():
        what_services(addr, name_by_addr[addr])

Once you find the correct service/profile, your next problem will be negotiating security (pin code for pairing), which I haven't worked out how to do yet!

See the www.bluetooth.org for all your Bluetooth needs!

Pev
Thanks for your response, but I got the discovery issue covered. As I wrote before my problem is with getting a valid response to the connection and AT commands
eLAN
I use pybluez (http://code.google.com/p/pybluez/)
eLAN
So you are sure that the mobile device supports these AT commands on the RFCOMM channel then?
Pev