Skip to content
Snippets Groups Projects
Commit 91f49cd4 authored by Masson Lea's avatar Masson Lea
Browse files
parents d91e8dc1 91654bd4
No related branches found
No related tags found
No related merge requests found
......@@ -57,7 +57,7 @@ public class BluetoothManager implements DiscoveryListener {
}
devices.add(btDevice);
Logger.getLogger(FXMLVueController.class.getName()).log(Level.FINE,"deviceDiscovered","device found: " + name);
System.out.println("device found: " + name);
}
......@@ -80,6 +80,7 @@ public class BluetoothManager implements DiscoveryListener {
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for(ServiceRecord s : servRecord) {
Logger.getLogger(BluetoothManager.class.getName()).log(Level.FINE,s.toString(),servRecord);
System.out.println(s);
}
serviceRecords.addAll(Arrays.asList(servRecord));
}
......@@ -94,17 +95,17 @@ public class BluetoothManager implements DiscoveryListener {
RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
for(RemoteDevice d : devices) {
if(d.getBluetoothAddress().equals(THINGZ_ID)) {
Logger.getLogger(BluetoothManager.class.getName()).log(Level.FINE,"launch","Thingz found");
System.out.println("Thingz found");
synchronized(lock) {
int result = agent.searchServices(null, desiredServiceUuids, d, this);
if( DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE == result) {
Logger.getLogger(BluetoothManager.class.getName()).log(Level.FINE,"launch","But it could not be reached");
System.out.println("But it could not be reached");
continue;
}
lock.wait();
if(null != serviceRecords && serviceRecords.size() > 0) {
Logger.getLogger(BluetoothManager.class.getName()).log(Level.FINE,"launch","Thingz service records found");
System.out.println("Thingz service records found");
for(ServiceRecord serviceRecord: serviceRecords) {
String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // I have no idea what these args are
StreamConnection connection;
......@@ -113,11 +114,11 @@ public class BluetoothManager implements DiscoveryListener {
DataInputStream inputStream = new DataInputStream(connection.openDataInputStream());
BluetoothConnection bluetoothConnection = new BluetoothConnection(d.getBluetoothAddress(), inputStream);
bluetoothConnection.setOutputStream(new DataOutputStream(connection.openOutputStream()));
connections.add(bluetoothConnection);
Logger.getLogger(BluetoothManager.class.getName()).log(Level.FINE,"launch","Connected to " + d.getBluetoothAddress());
connections.add(bluetoothConnection);
System.out.println("Connected to " + d.getBluetoothAddress());
return true;
} catch (IOException e) {
Logger.getLogger(BluetoothManager.class.getName()).log(Level.SEVERE,"launch","Could not connect to " + d.getBluetoothAddress() + ": " + e.getMessage());
} catch (IOException e) {
System.out.println("Could not connect to " + d.getBluetoothAddress() + ": " +e.getMessage());
e.printStackTrace();
return false;
}
......
......@@ -11,13 +11,12 @@ import java.util.logging.Logger;
import sun.misc.IOUtils;
public class BluetoothThread extends Thread implements Observable {
private static final int PACKET_SIZE = 18;
private static final int PACKET_SIZE = 16;
private BluetoothConnection connection;
private boolean keepGoing = true;
private String buffer;
private Observer observer;
private boolean bufferHasToBeUpdated = false;
private String buffer = "";
private Observer observer;
public BluetoothThread(BluetoothConnection connection, Observer observer) {
this.connection = connection;
......@@ -40,14 +39,16 @@ public class BluetoothThread extends Thread implements Observable {
// Each packet must start with two SYNC bytes in a row
byte[] payload = new byte[PACKET_SIZE];
int nbRead = connection.getInputStream().read(payload);
Logger.getLogger(BluetoothThread.class.getName()).log(Level.FINE,"run",nbRead);
if(bufferHasToBeUpdated){
this.buffer = new String(payload, StandardCharsets.UTF_8);
notifyObserver();
String received = new String(payload, StandardCharsets.UTF_8);
if(received.indexOf("F") > -1){ // fin
this.buffer += received.substring(0, received.indexOf("F"));
notifyObserver();
this.buffer = received.substring(received.indexOf("F")+1);
} else {
this.buffer +=received;
}
} catch (Exception e) {
Logger.getLogger(BluetoothThread.class.getName()).log(Level.SEVERE,"Could not read stream",e);
System.out.println("Could not read stream + " + e);
}
}
......@@ -59,7 +60,7 @@ public class BluetoothThread extends Thread implements Observable {
keepGoing = false;
finalize();
} catch (Throwable ex) {
Logger.getLogger(BluetoothThread.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
......
......@@ -11,8 +11,6 @@ import Interfaces.Observable;
import Interfaces.Observer;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
......@@ -111,7 +109,7 @@ public class FXMLVueController implements Initializable, Observer {
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
//bluetoothManager = new BluetoothManager(this);
bluetoothManager = new BluetoothManager(this);
alert = false;
//Put the gridPane Accueil visible
......@@ -188,7 +186,7 @@ public class FXMLVueController implements Initializable, Observer {
this.activationMessage.setStyle("-fx-background-color: #7fca5eff");
this.buttonBluetooth.setDisable(true);
} catch (Exception ex) {
Logger.getLogger(FXMLVueController.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
}
......@@ -201,7 +199,11 @@ public class FXMLVueController implements Initializable, Observer {
public void update(Observable obs) {
if(obs instanceof BluetoothThread){
BluetoothThread t = (BluetoothThread) obs;
Logger.getLogger(FXMLVueController.class.getName()).log(Level.FINE,"update",t.getBuffer());
/*System.out.println("H :" + t.getH());
System.out.println("L :" + t.getL());
System.out.println("T :" + t.getT());
System.out.println("P :" + t.getP());*/
System.out.println("payload : " + t.getBuffer());
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment