how to send byte array through bluetooth???

Forum for discussion about developing multiplayer games for mobile platform including Httpconnection and bluetooth connections.

how to send byte array through bluetooth???

Postby itskrishnaraj » Tue Nov 18, 2008 9:45 am

hi code tiger

i just want to know how to send a byte array through bluetooth connection , i m doing an application in that i have to send a byte array to the other mobile how the sending and recieving occuring ..i got a sample code, in that its sending a string . i jus want to send a byte array ,in example code they are using streamconnnection


-----------------------client send a string when connected-------------------
Code: Select all
StreamConnection conn = null;
         try {
                    string msg = "hello world";
                    conn = (StreamConnection)Connector.open(serviceUrl);
                    OutputStream output = conn.openOutputStream();         
                    output.write(msg);     
                    output.write(msg.getBytes());       
                    output.close();       
         
           System.out.println(readData(conn));   
          
          
         } catch (Exception ex) {
            System.out.println(ex);
         }


-----------------------server replaying for the msg "hello world" ------------

Code: Select all
// Pauses thread until Transmission occurs
      conn = notifier.acceptAndOpen();       
     

      // Read Data Transmission
      String msg = readData(conn);       
      System.out.println("Received Message from Client: " + msg);

      // Send Back a Message
      msg = "Hello Back from Server";       
      output = conn.openOutputStream();       
      output.write(msg.length()); // length is 1 byte     
     output.write(msg.getBytes());     
     //output.close();   


--------------read data()--------------

Code: Select all
public final static String readData(StreamConnection conn) {      //kris
         
          InputStream input = null;
          byte[] data = null;   
         
          try {                   
            input = conn.openInputStream();
           
            // Probably want to throw an exception if length is not greater then 0
            int length = input.read();       
            data= new byte[length];
            length = 0;

            // Assemble data
            while (length != data.length) {     
              int ch = input.read(data, length, data.length - length);
              if (ch == -1) {
                throw new IOException("Can't read data");
              }
              length += ch;
            }     

          } catch (IOException e) {
            System.err.println(e);
          } finally {       
             
            // close input stream   
            if (input != null) {
              try {
                input.close();
              } catch (IOException e) {}
            }
          }
          return new String(data);       
        }   

pls sort it out

thanks in advance
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to send byte array through bluetooth???

Postby itskrishnaraj » Wed Nov 19, 2008 12:38 pm

got solution dear

data send to server

Code: Select all
public void sendToServer(byte[] movedata)
    {
       try
       {
          conn=null;
          conn = (StreamConnection)Connector.open(serviceUrl);
          OutputStream output = conn.openOutputStream();
          output.write(movedata);
        
          int moveLen = movedata.length;
          System.out.println("After Written2: moveLen: " + moveLen);
        
          for ( int i = 0; i < moveLen; i++ )
          {               
             System.out.println("send Data =" + movedata[i]);
          }
       }catch(IOException ioe){}
        
    }


read the data

Code: Select all
public byte[] readData(StreamConnection conn)
    {
       InputStream input = null;
       byte[] data = null; 
       StringBuffer bufferVal = new StringBuffer();
       int ch;
      
       try
       {                   
          input = conn.openInputStream();
          // Probably want to throw an exception if length is not greater then 0
          int length = 5;       
          data= new byte[length];
          length = 0;
         
          while ( ( ch = input.read() ) != -1 )
           {
             bufferVal.append((char) ch);
           }
          
           System.out.println(" not null");
          data = bufferVal.toString().getBytes();
       } catch (IOException e)
       {
          System.err.println(e);
       } finally
       {       
             
            // close input stream   
            if (input != null) {
              try {
                input.close();
              } catch (IOException e) {}
            }
          }
          return data;       
        }   


------------------------read the data --------------

Code: Select all
                                    byte[] receive=new byte[5];
         receive=readData(conn);
         int recieve_len=receive.length;
         
         for(int j=0;j<recieve_len;j++)
         {
            System.out.println("recieved data-----> "+receive[j]);
         }
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to send byte array through bluetooth???

Postby DevelopmentTeam » Thu Nov 20, 2008 9:18 am

Wow, Thanks for your update. Anyway... It might be useful for the others
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: how to send byte array through bluetooth???

Postby richermartyn » Fri Aug 28, 2009 11:24 am

Thank you,
I am Richer Martyn,
I like the information you shared. I extremely thank you. Be connected with me and help after this time also.
Game is Power
richermartyn
 
Posts: 3
Joined: Fri Aug 28, 2009 11:01 am

Re: how to send byte array through bluetooth???

Postby robbinhood » Fri Jun 11, 2010 9:02 am

To send the byte array through the bluetooth . There should be have software to convert the file into the bytes. while sending all the at the same time instead of this send single. You are trying to send a complete screen shot every second? I guess the first thing to consider . Just sending difference frames but you would want to find a native method to do that presumably.
Mistakes are the portals of the discovery.
robbinhood
 
Posts: 4
Joined: Fri Jun 11, 2010 8:53 am


Return to Multiplayer Mobile Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron