/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cnam.nsy208.sudoku.bluetooth;
import cnam.nsy208.sudoku.main.Sudoku;
import cnam.nsy208.sudoku.model.TimerClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
/**
*
* @author Martial
*/
public class BtClient implements DiscoveryListener {
private DiscoveryAgent _discoveryAgent ;
private UUID[] _uuidSet ;
private String _serviceUrl ;
private InputStream _input ;
private OutputStream _output ;
private StreamConnection _conn ;
private BtInterface _parent ;
private TimerClient _timer ;
private int _active ;
/**
* Initialise un client BT
* @param uid : uid du serveur
* @param parent : parent
*/
public BtClient( UUID uid , BtInterface parent ) {
try {
_parent = parent ;
_input = null;
_output = null;
_conn = null;
_uuidSet = new UUID[1];
_uuidSet[0] = uid ;
LocalDevice localDevice = LocalDevice.getLocalDevice();
_discoveryAgent = localDevice.getDiscoveryAgent();
_discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
_timer = new TimerClient(Sudoku.TIMEOUT_GAME_SEC);
_active = 1;
} catch (Exception e) {
System.out.println(e);
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));
// Search for Services
_discoveryAgent.searchServices(null, _uuidSet, btDevice, this);
} catch (Exception e) {
System.out.println("Device Discovered Error: " + e);
}
}
public void inquiryCompleted(int discType) {
System.out.println("InquiryCompleted");
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
System.out.println("ServicesDiscovered");
// in this example there is only one service
for (int i = 0; i < servRecord.length; i++) {
_serviceUrl = servRecord[i].getConnectionURL(0, false);
}
}
public void serviceSearchCompleted(int transID, int responseCode) {
if (responseCode == SERVICE_SEARCH_ERROR) {
System.out.println("SERVICE_SEARCH_ERROR\n");
}
if (responseCode == SERVICE_SEARCH_COMPLETED) {
System.out.println("SERVICE_SEARCH_COMPLETED\n");
System.out.println("Service URL: " + _serviceUrl );
_parent.init() ;
}
if (responseCode == SERVICE_SEARCH_TERMINATED) {
System.out.println("SERVICE_SEARCH_TERMINATED\n");
}
if (responseCode == SERVICE_SEARCH_NO_RECORDS) {
System.out.println("SERVICE_SEARCH_NO_RECORDS\n");
}
if (responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE) {
System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
}
}
/**
* Ouvre la connexion BT
*/
public void open() {
try {
_conn = (StreamConnection) Connector.open( _serviceUrl , Connector.READ_WRITE , true );
_input = _conn.openInputStream();
_output = _conn.openOutputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Ferme la connexion BT
*/
public void close() {
try {
_input.close();
_output.close();
_conn.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Lit des données sur la connexion BT
* @return
*/
public String readData() {
byte[] data = null;
try {
// 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);
}
return new String(data);
}
/**
* Ecrit des données sur la connexion BT
* @param msg : Données à écrire
*/
public void writeData(String msg ) {
try {
_output.write(msg.length());
_output.write(msg.getBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Affecte la valeur de l'état actif du client
* @param i
*/
public void setActive(int i){
_active = i;
}
/**
* Retourne l'état actif du client
* @return Etat
*/
public int getActive(){
return _active;
}
/**
* Réciupére la valeur courante du timer de durée max de la partie
* @return valeur courante du timer en seconde
*/
public int getTimer(){
return _timer.getCptSec();
}
/**
* Démarre le timer de durée max de la partie
*/
public void startTimer(){
_timer.start();
}
}