Saturday, February 22, 2014

Sending Message from PC through mobile using AT Commands over Bluetooth


AT Commands:
              AT or attention are set of commands those are used to communicate with GSM modems.I am not going much about it as you can google it.I tried using the AT commands through Bluetooth  which was successful and finally integrated it to JAVA so that I can send message through my mobile using an interface instead of hyper-terminal.Before getting it started check your mobile if it supports the commands or not by using Hyper-terminal or any serial port Software.


               In the below codes i have used RxTx serial communication library that is free to use and my communication port was 'COM9' please check it for you and then changing it compile the code.

Main.java //Main GUI

package com.oksbwn.Y2014.Message_Mobile;



import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;



import javax.swing.*;



public class Main {



     JFrame frame=new JFrame();

     Dimension gh=java.awt.Toolkit.getDefaultToolkit().getScreenSize();

    public static void main(String[] args) throws InterruptedException { }

    public Main(){



        

        frame.getContentPane().setLayout(null);

        frame.setUndecorated(true);

          frame.addMouseListener(mouseListener);

          frame.addMouseMotionListener(mouseListener);

        frame.setBackground(new Color(Color.white.getRed(), Color.white.getGreen(),Color.white.getBlue(),1));

        frame.getContentPane().setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),50));

        frame.setBounds(gh.width/2-100,gh.height/2-110,200,250);

        

        frame.setType(JFrame.Type.UTILITY);

        

        final JLabel cloesButton = new JLabel("X");

          cloesButton.setForeground(Color.white);

          cloesButton.setToolTipText("Close");

          cloesButton.addMouseListener(new MouseAdapter() {

                @Override

                public void mouseClicked(MouseEvent arg0) {

                     frame.dispose();

                     }

               @Override

               public void mouseEntered(MouseEvent arg0) {

                    cloesButton.setForeground(Color.red);

                   }

              @Override

               public void mouseExited(MouseEvent arg0) {

                    cloesButton.setForeground(Color.white);

                   }});

          cloesButton.setSize(15,15);

          cloesButton.setBounds(frame.getWidth()-15,0, 15,15);

          frame.getContentPane().add(cloesButton);

          



            final JLabel head = new JLabel("MESSENGER");

            head.setBounds(50,0,100,20);

            head.setForeground(Color.blue);

            head.setOpaque(false);

            frame.getContentPane().add(head);

            

        final JTextField no=new JTextField();

        no.setBounds(0,20,200,20);

        no.setForeground(Color.white);

        no.setOpaque(false);

        frame.getContentPane().add(no);

        

        final JEditorPane text=new JEditorPane();

        text.setBounds(0,50,200,170);

        text.setOpaque(false);

        text.setForeground(Color.white);

        frame.getContentPane().add(text);

        

        JButton send=new JButton("SEND");

        send.setBounds(0,230,200,20);

        send.addMouseListener(new MouseAdapter() {

            @Override

            public void mouseClicked(MouseEvent arg0) {

                 new sendMessage(no.getText(),text.getText());

                 }});

        frame.getContentPane().add(send);



        frame.setVisible(true);



        }

    final  MouseAdapter mouseListener = new MouseAdapter() {

        int x, y;

        @Override

        public void mousePressed(MouseEvent e) {

            if (e.getButton() == MouseEvent.BUTTON1) {

                x = e.getX();

                y = e.getY();

            }

        }



        @Override

        public void mouseDragged(MouseEvent e) {

            if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {

                frame. setLocation(e.getXOnScreen() - x, e.getYOnScreen() - y);

            }

        }

    };

}

 sendMessage.java

package com.oksbwn.Y2014.Message_Mobile;



import com.oksbwn.voiceEnable.Voice;



public class sendMessage {

public sendMessage(String phone,String message)

  {

    byte[] configureTextMode={0x41,0x54,0x2B,0x43,0x4D,0x47,0x46,0x3D,0x31,0x0D}; 

    byte[] SetSMSNo={0x41,0x54,0x2B,0x43,0x4D,0x47,0x53,0x3D,0x22,

            (byte) phone.charAt(0),(byte) phone.charAt(1),(byte) phone.charAt(2),(byte) phone.charAt(3),(byte) phone.charAt(4),(byte) phone.charAt(5),(byte) phone.charAt(6),(byte) phone.charAt(7),(byte) phone.charAt(8),(byte) phone.charAt(9),

            0x22,0x0d}; 

    //byte[] BatteryLevel={0x41,0x54,0x2B,0x43,0x42,0x43,0x0D}; 

    SerialComm ad= new SerialComm();

    ad.SendData(configureTextMode);

    if(ad.readSerial().trim().replaceAll(" ","").contains("OK"))

    {}

    ad.SendData(SetSMSNo);

    if(ad.readSerial().trim().replaceAll(" ","").contains(">"))

    {}

    ad.SendData(message.getBytes());

   byte[] end={'O','K','S','B','W','N',0x1A};

    ad.SendData(end);

    if(ad.readSerial().trim().replaceAll(" ","").contains("OK"))

    {

        new Voice("Sir i have Send the Message");    

    }

    ad.Close();

  }

}



SerialComm.java

package com.oksbwn.Y2014.Message_Mobile;

import gnu.io.CommPort;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.HashMap;





public class SerialComm {    

    private Enumeration<?> ports = null;

    private HashMap<String, CommPortIdentifier> portMap = new HashMap<String, CommPortIdentifier>();

    private CommPortIdentifier selectedPortIdentifier = null;

    private SerialPort serialPort = null;

    private InputStream input = null;

    private OutputStream output = null;

    final static int TIMEOUT = 2000;

    final static int SPACE_ASCII = 32;

    final static int DASH_ASCII = 45;

    final static int NEW_LINE_ASCII = 10;

    CommPort commPort = null;

    public static void main(String[] args) {byte[] bt={0x00,0x25,0x26};SerialComm ad= new SerialComm();

    ad.SendData(bt);}

    

    public SerialComm()

    {

         ports = CommPortIdentifier.getPortIdentifiers();

           while (ports.hasMoreElements())

            {

                CommPortIdentifier curPort = (CommPortIdentifier)ports.nextElement();

                if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL)

                {

                    portMap.put(curPort.getName(), curPort);

                }

            }

           String selectedPort = "COM9";//Add the Port

           selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);

           try {

            commPort = selectedPortIdentifier.open("serial", TIMEOUT);

              serialPort = (SerialPort)commPort;

              serialPort.setRTS(true);

              serialPort.setDTR(true);

              output = serialPort.getOutputStream();

              input = serialPort.getInputStream();

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    

        

    }

    public void    SendData(byte[] bt){

         

           try

           {

             output.write(bt);

             output.flush();

              }catch (Exception e)

           {}

        }   

    public void    Close(){

              try

               { 

                 serialPort.close();

                 input.close();

                 output.close();

                  }catch (Exception e)

               {}

            }

    private byte[] readBuffer = new byte[400];

    

public  String  readSerial() 

   {

        String read = null;

        try {



            //int avBytes=input.read();

            input.read(readBuffer,0,50);

            input.read(readBuffer,0,150);

            input.read(readBuffer,0,200);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            input.read(readBuffer,0,300);

            read=new String(readBuffer,0,400);

           

        } catch (Exception e) {e.printStackTrace();

        }

       

      return read;

    }

}

thanks...pls leave your comment...

No comments:

Post a Comment