Skip to content
Akhil Rao edited this page Jun 22, 2012 · 8 revisions

#TeclaSocket#

##Introduction

TeclaSocket is a Java Library which handles Connectivity with the TeclaShield. It uses Bluecove library for managing bluetooth sockets.In its current version it supports bluetooth RFComm (SPP profile) connection only. This library will provide events for managing bluetooth events like onReceive and onSent so that an event-driven programming model could be adopted.

##Information for the Developer

In order to use TeclaSocket in your project,you will have to reference it in your project by adding TeclaSocket.jar in your java buildpath.You can obtain the source code by cloning the TeclaShield repository and checkout to desktop branch.

$ git clone git://github.com/jorgesilva/TeclaShield.git
$ git checkout desktop   

###Package name: com.akdroid.teclasocket:

You must import the above package as

import com.akdroid.teclasocket.*;

###Classes and Interfaces:

TeclaSocket :

TeclaSocket is the major class and the class that a developer should use for making connectivity sockets. It can be configured as a Client which will accept a connection or the server used in the server itself. UUID of a service is the main parameter that is used to connect to a service. So if you want to connect to a service and in case of this library TeclaShield you will have to provide the UUID value in the form of a String.
UUID is an 128-bit integer used to distinguish a particular service from within a device.

  • If you want to use a socket as server.You should use the following constructor.

      TeclaSocket tsock_server;
      String uuid_to_broadcast= "0000110100001000800000805F9B34FB" ; //the uuid shown here is for TeclaShield 
      boolean authorization=false;
      boolean encryption=false;
      String name_to_be_broadcasted="SPP"; //service name and not the device name
      tsock_server=new TeclaSocket(uuid_to_broadcast,name_to_be_broadcast,authorization,encryption);
    

The above code will be block the thread and is recommended to use in a separate thread.Also the authorization and encryption parameters should match for both the server and the client for the connection to be established.

  • To configure TeclaSocket as client you only need to provide the UUID value as a String and a boolean flag whether to search for devices in the constructor itself.

      TeclaSocket tsock_client;
      String uuid_to_connect= "0000110100001000800000805F9B34FB" ; //the uuid shown here is for TeclaShield 
      boolean do_search=false;
      tsock_client=new TeclaSocket(uuid_to_connect,do_search);
    
  • You can test the status of the connection using the function

      tsock.testconnection(0x80);  //0x80 is an arbitrary byte used to test the connection by sending it through the output stream
    

from now on tsock is a TeclaSocket instance and will be used so in this wiki.

  • In order to connect,

      tsock.connect(); //Connects to TeclaShield
      tsock.connect (new UUID(uuid-of_Service)) //Connects to the service with UUID uuid_of_Service
    
  • In order to disconnect,

      tsock.disconnect();
    
  • In order to send data,use

      tsock.write(byte); //byte is the data to be sent.
    

It should be noted that the current code allows for only a single byte to be sent.If it is required to send other type of data you need to modify the code likewise. You will have to basically write into dataout field of this class by using

    dataout.writeBytes(String_to_be_written); 
  • In order to read received data,

      tsock.receive();  //put the socket in listening mode,
    

The above code will block the thread till something is received.It iis recommended that the receive() function be called in a separate thread.When something is received onReceive event is fired up from the BluetoothEventListener which will provide you with a DataInputStream which can be read to get the received data.

    Byte received=datain.readByte();  //this will read a single byte 
  • Adding a bluetoothevent listener:

      tsock.addBluetoothEventListener(new BluetoothEventListener(){
        public void onConnect(){
              //code to do on connection established
        }
        public void onDisconnect(){
              //code to do on connection lost
        }
        public void onReceive(DataInputStream datain){
              //code to do on data received
        }
        public void onSent(){
              //code to do on a data sent
        }
      });   
    
  • Removing BluetoothEventListener

         tsock.removeBluetoothEventListener(myBluetoothEventListener);
    
  • To scan for available devices :

         tsock.scan_devices();
    

When the scan is completed, it enters the DiscoveryListener's method which is an interface provided by bluecove library and is implemented by TeclaSocket The method is :

    inquiryCompleted(int i){
         //code to be done after inquiry i.e. device search is completed.
    }

BluetoothEvent

It is a class that represents and holds the bluetooth events that occur.It holds the value used to identify a bluetooth event and is fired by TeclaSocket object whenever an event occurs.

BluetoothEventListener

It is an interface that listens to a BluetoothEvent. It is used to listen to bluetoothevents generated for a particular TeclaSocket instance. Adding and Removing a BluetoothEventListener is described above.

Communication

It is a basic interface just to bring uniformity to the code written for sockets.

WiFiSocket

This class is incomplete and does not function.The main objective of this class would be to connect to TeclaShield via an android device over WiFi network.

Clone this wiki locally