forum.j2me.info
September 07, 2010, 04:23:20 AM *
A A A A
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Forum realeased!
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Can't extablish connection to Bluetooth module & display gauge interface  (Read 802 times)
huige
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 1


View Profile
« on: November 16, 2009, 12:30:15 PM »

Hi all

I can't seem to even establish a connection to the module. My original working code is by starting from displaying one Bluetooth module address in a textbox, then press "process" - it successfully establishes a connection to the module, then the gauge interface will appear. In my modified code below for my project, I hae merely just modified the starting phase - by starting off with choosing 2 options, each options connecting to two different BT modules in 2 different circuits of mine. However, after i have chosen one choice and pressed "process" , and the textbox appeared displaying the BT address, the error then appeared stating "Class Cast java/lang/ClassCastException"  I have

I would appreciate if somebody wil enlighten me about this error of mine, and how should I correct my code to get it working as I have wanted. Emergency my project is to be passed up by end of this week! Thanks! (:

// program code
import java.lang.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.bluetooth.*;

public class BTRFCommClient1 extends BluetoothMIDlet implements ItemStateListener
{
   private Form connForm,controlForm;
   private Command processCommand, connectCommand,exitCommand;
   private InputStream input;
   private OutputStream output;
   private StreamConnection conn;
   private Gauge gauge;
   private Item item;
   private Display display;
   private String theMessage,ans;
   private ChoiceGroup roomChoice;
   private int currentIndex;
   private int roomIndex;
   

   public void startApp() throws MIDletStateChangeException {

      //connForm = new Form("Dimmer");
      //connectCommand = new Command("Connect", Command.OK, 1);
      //connForm.addCommand(connectCommand);
      //connForm.addCommand(new Command("Exit", Command.EXIT,1));
      //connForm.setCommandListener(this);
      
       roomChoice = new ChoiceGroup("Choose a room.", Choice.EXCLUSIVE);
      //roomChoice.append("000195071131",null);
      roomChoice.append("Bedroom",null);
      roomChoice.setSelectedIndex(currentIndex, true);
      currentIndex = roomChoice.append("Kitchen",null);
      exitCommand = new Command("Exit", Command.EXIT,1);
      processCommand = new Command("Process", Command.OK, 1);
      
      connForm = new Form("Room Light");
      roomIndex = connForm.append(roomChoice);
      
      connForm.addCommand(exitCommand);
      connForm.addCommand(processCommand);
      
      connForm.setCommandListener(this);
      Display.getDisplay(this).setCurrent(connForm);
      

    }


   public void pauseApp(){
   }

   public void destroyApp(boolean unconditional){
   }


   public void commandAction(Command c, Displayable d){
      switch (c.getCommandType()){

         case Command.OK:
      {      
                        currentIndex = roomChoice.getSelectedIndex();
         if(roomChoice.getString(currentIndex) == "Bedroom")
      {
         
         
         
         
      TextField address = new TextField("Address", null, 12, TextField.ANY);
      address.setString("000195071131");
      connForm.append(address);

      TextField channel = new TextField("Channel", null,2, TextField.NUMERIC);
      channel.setString("1");
      connForm.append(channel);
      Display.getDisplay(this).setCurrent(connForm);
      
      
      }
      
      if(roomChoice.getString(currentIndex) == "Kitchen")
      {
         
         
         
         
      TextField address = new TextField("Address", null, 12, TextField.ANY);
      address.setString("000195071167");
      connForm.append(address);
      
      TextField channel = new TextField("Channel", null,2, TextField.NUMERIC);
      channel.setString("1");
      connForm.append(channel);
      Display.getDisplay(this).setCurrent(connForm);
      
      
      }      
   
   
       new Thread(this).start();
      break;
      
      }
         case Command.EXIT:
            try {
               input.close();
               output.close();
               conn.close();
            } catch (Exception e) {
            }
         notifyDestroyed();
         break;
      }
   }

   private String getConnectionString() {
      
      // Retrive the TextFields from the Form
      
      TextField address = (TextField)connForm.get(0);
      TextField channel = (TextField)connForm.get(1);
      
      
      
      
      // Create the connection string
      StringBuffer temp = new StringBuffer("btspp://");
      
      temp.append(address.getString());
      temp.append(":");
      temp.append(channel.getString());
      
      connForm.delete(0);
      connForm.delete(0);

      return temp.toString();
   }
   // Establish Connection to the server

   private boolean connectToServer(String connString) {
   try {
      conn = (StreamConnection)Connector.open(connString);
      
      input = conn.openInputStream();
      output = conn.openOutputStream();

      return true;
      } catch (IOException e) {
         connForm.append("Connect failed (IOException: ");
         connForm.append(e.getMessage());
         connForm.append(")\n");

         return false;
      }
   }

   // Retrieve Bluetooth address and channel ID from the Form.
   //This method then establish a connection to the server


   
   public void run() {
         
      
      String connString= getConnectionString();
      connForm.append("Connecting to Server... \n");
      
      if(connectToServer(connString)) {
         connForm.append("Connection Established");
         
          gauge = new Gauge("Brightness: ", true, 1,1);
                    
               connForm.append(gauge);
          itemStateChanged(gauge);
          connForm.setItemStateListener(this);
         
      }
   }
   public void itemStateChanged(Item item)
    {
      if (item == gauge)
       {
         if(gauge.getValue() == 0)
         {
            theMessage = "0";
                        
            new Thread(new Message("0" + "\n", input,output)).start();
            
         }
         
         if(gauge.getValue() == 1)
         {
            theMessage = "1";
            new Thread(new Message("1" + "\n", input,output)).start();
         }
         
         
      
       }
    }
   
class Message implements Runnable
{
   //private String theMessage;
   private InputStream input;
   private OutputStream output;

   public Message(String msg, InputStream in, OutputStream out){
      //theMessage = msg;
      input = in;
      output = out;
      
   }
   
   

   public void run(){
      try{
         
         byte[] data = theMessage.getBytes();
         
      
         output.write(data);
         output.flush();
         int fullLength = data.length;
         int length = input.read(data);
         fullLength -= length;
         //
         StringBuffer buf = new StringBuffer(new String(data,0,length));

         while (fullLength >0) {
            length = input.read(data);
            fullLength -= length;
            buf = buf.append(new String(data, 0, length));
         }

         connForm.append("\n");
         String displayString = buf.toString();
         displayString = displayString.substring(0,displayString.length()-1);
      }
      
      
      catch (IOException e) {
         connForm.append("\n Failed to send message:" + e.getMessage());
      }
      
      
         
      }

   }
}

// Bluetooth Midlet code
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.bluetooth.*;

public class BluetoothMIDlet extends MIDlet implements Runnable,CommandListener
{
   public BluetoothMIDlet(){
   }

   public void startApp() throws MIDletStateChangeException {
         new Thread(this).start();
    }


   public void pauseApp(){
   }

   public void destroyApp(boolean unconditional){
   }

   public void run(){
   }
   public void commandAction(Command c, Displayable d){
         notifyDestroyed();
   }
}
Logged
forum.j2me.info
   

 Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP
Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC
SMF customization services by 2by2host.com
Valid XHTML 1.0! Valid CSS!