Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Sending/Receiving SMS from MIDlets

// Lets send an SMS now.
//get a reference to the appropriate Connection object using an appropriate url
MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
//generate a new text message
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set the message text and the address
tmsg.setPayloadText(message);
tmsg.setAddress("sms://" + number);
//finally send our message
conn.send();


// Time to receive one.
//get reference to MessageConnection object
MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
//set message listener
conn.setMessageListener(
   new MessageListener() {
      public void notifyIncomingMessage(MessageConnection conn) {
         Message msg = conn.receive();
         //do whatever you want with the message
         if(msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            System.out.println(tmsg.getPayloadText());
         } else if(msg instanceof BinaryMessage) {
            .....
         } else {
            ......
         }
      }
   }
);

Send and receive SMS text messages with Ruby and a GSM/GPRS modem

require 'serialport'
require 'time'

class GSM
  
  SMSC = "+447785016005"  # SMSC for Vodafone UK - change for other networks

  def initialize(options = {})
    @port = SerialPort.new(options[:port] || 3, options[:baud] || 38400, options[:bits] || 8, options[:stop] || 1, SerialPort::NONE)
    @debug = options[:debug]
    cmd("AT")
    # Set to text mode
    cmd("AT+CMGF=1")
    # Set SMSC number
    cmd("AT+CSCA=\"#{SMSC}\"")    
  end
  
  def close
    @port.close
  end
  
  def cmd(cmd)
    @port.write(cmd + "\r")
    wait
  end
  
  def wait
    buffer = ''
    while IO.select([@port], [], [], 0.25)
      chr = @port.getc.chr;
      print chr if @debug == true
      buffer += chr
    end
    buffer
  end

  def send_sms(options)
    cmd("AT+CMGS=\"#{options[:number]}\"")
    cmd("#{options[:message][0..140]}#{26.chr}\r\r")
    sleep 3
    wait
    cmd("AT")
  end
  
  class SMS
    attr_accessor :id, :sender, :message, :connection
    attr_writer :time
    
    def initialize(params)
        @id = params[:id]; @sender = params[:sender]; @time = params[:time]; @message = params[:message]; @connection = params[:connection]
    end
    
    def delete
      @connection.cmd("AT+CMGD=#{@id}")
    end
    
    def time
      # This MAY need to be changed for non-UK situations, I'm not sure
      # how standardized SMS timestamps are..
      Time.parse(@time.sub(/(\d+)\D+(\d+)\D+(\d+)/, '\2/\3/20\1'))
    end
  end
  
  def messages
    sms = cmd("AT+CMGL=\"ALL\"")
    # Ugly, ugly, ugly!
    msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
    return nil unless msgs
    msgs.collect!{ |m| GSM::SMS.new(:connection => self, :id => m[0], :sender => m[1], :time => m[2], :message => m[3].chomp) } rescue nil
  end
end


destination_number = "+44 someone else"

p = GSM.new(:debug => false)

# Send a text message
p.send_sms(:number => destination_number, :message => "Test at #{Time.now}")

# Read text messages from phone
p.messages.each do |msg|
  puts "#{msg.id} - #{msg.time} - #{msg.sender} - #{msg.message}"
  # msg.delete
end

PyS60 - listSMS to File.txt

// Salva la lista degli SMS in un file

from time import ctime

import codecs
import inbox

box = inbox.Inbox()
msg = box.sms_messages()

f = codecs.open('E:/Others/listSMS.txt', 'w', 'utf8') # Apre il file in codifica UTF8
for i in msg:
	f.write(box.address(i))
	f.write('\n')
	f.write(ctime(box.time(i))) # Converte i secondi in una stringa rappresentante il tempo
	f.write('\n')
	f.write(box.content(i))
	f.write('\n')
f.close()

print 'Fine'

f = codecs.open('E:/Others/listSMS.txt', 'r', 'utf8')
print f.read()
f.close()

Python - SendSMS over BT and AT

// Send SMS over Bluetooth (AT Command)

import bluetooth

sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sockfd.connect(('00:00:00:00:00:00', 1)) # BT Address
sockfd.send('ATZ\r')
sockfd.send('AT+CMGF=1\r')
sockfd.send('AT+CSCA="+393359609600"\r') # Client TIM ITA
sockfd.send('AT+CMGS="+39xxxxxxxxxx"\r') # TO PhoneNumber
sockfd.send('Messaggio da mandare...\n')
sockfd.send(chr(26)) # CTRL+Z
sockfd.close()

Read SMS with inbox module

The new pys60 (1.3.1) provide 'inbox' module to read SMS.
It can also notify you when a new message arrives.
>>> import inbox
>>> i = inbox.Inbox()
>>> m = i.sms_messages()  # all message ID's
>>> i.content(m[0])     # first message
u’foobar’
>>> i.time(m[0])
1130267365.03125
>>> i.address(m[0])     # Only name is given :(
u’John Doe’
>>> i.delete(m[0])
>>>

I wish the i.address gave more detail information.
Currently, if you need the number, you need to search
the contact database.

See an example of notification callback in the documentation.

Sending sms from nokia series 60

I am really impressed by this.

import messaging
messaging.sms_send(number, text)
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS