Skip to content
Snippets Groups Projects
Commit 373ecec5 authored by Salles Coralie's avatar Salles Coralie
Browse files

merge conflict

parents 0e3a32e3 cc794012
No related branches found
No related tags found
No related merge requests found
/.metadata/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BluetoothJavaServer.src.edu.kufpg.bluetooth.server;
import Vue.FXMLVueController;
import app.App;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 puiss
*/
public class BluetoothManager implements DiscoveryListener {
private Object lock=new Object();
private static ArrayList<RemoteDevice> devices;
private final List<ServiceRecord> serviceRecords = new ArrayList<ServiceRecord>();
private final List<BluetoothConnection> connections = new ArrayList<BluetoothConnection>();
private BluetoothThread t;
private FXMLVueController observer;
public BluetoothThread getWorkingThread(){
return t;
}
public BluetoothManager(FXMLVueController observer){
this.devices = new ArrayList<RemoteDevice>();
this.observer = observer;
}
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
String name;
try {
name = btDevice.getFriendlyName(false);
} catch (Exception e) {
name = btDevice.getBluetoothAddress();
}
devices.add(btDevice);
System.out.println("device found: " + name);
}
@Override
public void inquiryCompleted(int arg0) {
synchronized(lock){
lock.notify();
}
}
@Override
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized (lock) {
lock.notify();
}
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
System.out.println("hey ! " + servRecord);
for(ServiceRecord s : servRecord) {
System.out.println("-------"+ s + "-------");
}
serviceRecords.addAll(Arrays.asList(servRecord));
}
public boolean launch(){
try{
UUID serialPortUuid = new UUID(BluetoothServiceUuids.SERIAL_PORT.getHexValue());
UUID[] desiredServiceUuids = new UUID[] { serialPortUuid };
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
for(RemoteDevice d : devices) {
if(d.getBluetoothAddress().equals("201611073171")) {
System.out.println("Thingz trouve");
synchronized(lock) {
int result = agent.searchServices(null, desiredServiceUuids, d, this);
if( DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE == result) {
System.out.println(" but it could not be reached");
continue;
}
lock.wait();
System.out.println("service records found");
if(null != serviceRecords && serviceRecords.size() > 0) {
System.out.println("YA DES SERVICES");
for(ServiceRecord serviceRecord: serviceRecords) {
String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // I have no idea what these args are
StreamConnection connection;
try {
connection = (StreamConnection)Connector.open(connectionUrl);
DataInputStream inputStream = new DataInputStream(connection.openDataInputStream());
BluetoothConnection bluetoothConnection = new BluetoothConnection(d.getBluetoothAddress(), inputStream);
bluetoothConnection.setOutputStream(new DataOutputStream(connection.openOutputStream()));
connections.add(bluetoothConnection);
System.out.println("Connected to " + d.getBluetoothAddress());
return true;
} catch (IOException e) {
System.out.println("Could not connect to " + d.getBluetoothAddress() + ": " + e.getMessage());
e.printStackTrace();
return false;
}
}
}
}
}
}
} catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
/**
*
*/
public void startThread() throws Exception {
try {
if(connections.size() > 0) { // we are connected
System.out.println("start");
this.t = new BluetoothThread(connections.get(0), observer);
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package BluetoothJavaServer.src.edu.kufpg.bluetooth.server;
import Vue.FXMLVueController;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.IOUtils;
public class BluetoothThread extends Thread {
private BluetoothConnection connection;
private boolean keepGoing = true;
public BluetoothThread(BluetoothConnection connection) {
private String buffer;
private FXMLVueController observer;
public BluetoothThread(BluetoothConnection connection, FXMLVueController observer) {
this.connection = connection;
this.observer = observer;
}
public void notifyObserver(){
observer.update(this);
}
public String getBuffer(){
return this.buffer;
}
@Override
public void run() {
System.out.println("hey c parti");
// If there are custom settings to be written to each headset, add them at this point
// try {
// connection.getOutputStreamWriter().write(MindwaveSerialPacket.Codes.DISCONNECT.getHexValue());
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
public void run() {
while (keepGoing) {
System.out.println("hi");
byte b;
try {
// Each packet must start with two SYNC bytes in a row
System.out.println("New packet found");
byte[] payload = new byte[100];
byte[] payload = new byte[16];
connection.getInputStream().read(payload);
String str = new String(payload, StandardCharsets.UTF_8);
System.out.println("payload : " + str);
this.buffer = new String(payload, StandardCharsets.UTF_8);
notifyObserver();
connection.getOutputStream().write("STOP".getBytes());
//connection.getOutputStream().write("SOUND".getBytes());
} catch (Exception e) {
System.out.println("Could not read stream" + e);
......@@ -50,7 +52,12 @@ public class BluetoothThread extends Thread {
}
public void stopThread() {
keepGoing = false;
try {
keepGoing = false;
finalize();
} catch (Throwable ex) {
Logger.getLogger(BluetoothThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
......
......@@ -5,8 +5,12 @@
*/
package Vue;
import BluetoothJavaServer.src.edu.kufpg.bluetooth.server.BluetoothManager;
import BluetoothJavaServer.src.edu.kufpg.bluetooth.server.BluetoothThread;
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;
......@@ -63,11 +67,24 @@ public class FXMLVueController implements Initializable {
@FXML
private Label activationMessage;
private boolean isConnected = false;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
/**
BluetoothManager bluetoothManager = new BluetoothManager(this);
bluetoothManager.launch();
try {
bluetoothManager.startThread();
} catch (Exception ex) {
Logger.getLogger(FXMLVueController.class.getName()).log(Level.SEVERE, null, ex);
}
**/
//Put the gridPane Accueil visible
accueil.setVisible(true);
currentGridPane = accueil;
......@@ -136,4 +153,9 @@ public class FXMLVueController implements Initializable {
private void clickActivationBluetooth(ActionEvent event) {
}
public void update(BluetoothThread t){
// MAJ LA VUE
System.err.println(t.getBuffer());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>app</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
......@@ -44,13 +44,7 @@ import javax.obex.ResponseCodes;
*
* @author invite
*/
public class App extends Application implements DiscoveryListener {
private static Object lock=new Object();
private static ArrayList<RemoteDevice> devices;
static DiscoveryListener listener = new App();
final static List<ServiceRecord> serviceRecords = new ArrayList<ServiceRecord>();
final static List<BluetoothConnection> connections = new ArrayList<BluetoothConnection>();
public class App extends Application {
@Override
public void start(Stage stage) throws Exception{
......@@ -66,101 +60,6 @@ public class App extends Application implements DiscoveryListener {
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
devices = new ArrayList<RemoteDevice>();
try{
UUID serialPortUuid = new UUID(BluetoothServiceUuids.SERIAL_PORT.getHexValue());
UUID[] desiredServiceUuids = new UUID[] { serialPortUuid };
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
for(RemoteDevice d : devices) {
if(d.getBluetoothAddress().equals("201611073171")) {
System.out.println("Thingz trouve");
synchronized(lock) {
int result = agent.searchServices(null, desiredServiceUuids, d, listener);
if( DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE == result) {
System.out.println(" but it could not be reached");
continue;
}
lock.wait();
System.out.println("service records found");
if(null != serviceRecords && serviceRecords.size() > 0) {
System.out.println("YA DES SERVICES");
for(ServiceRecord serviceRecord: serviceRecords) {
String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // I have no idea what these args are
StreamConnection connection;
try {
connection = (StreamConnection)Connector.open(connectionUrl);
DataInputStream inputStream = new DataInputStream(connection.openDataInputStream());
BluetoothConnection bluetoothConnection = new BluetoothConnection(d.getBluetoothAddress(), inputStream);
bluetoothConnection.setOutputStream(new DataOutputStream(connection.openOutputStream()));
connections.add(bluetoothConnection);
System.out.println("Connected to " + d.getBluetoothAddress());
} catch (IOException e) {
System.out.println("Could not connect to " + d.getBluetoothAddress() + ": " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
if(connections.size() > 0) { // we are connected
System.out.println("start");
BluetoothThread t = new BluetoothThread(connections.get(0));
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
launch(args);
}
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
String name;
try {
name = btDevice.getFriendlyName(false);
} catch (Exception e) {
name = btDevice.getBluetoothAddress();
}
devices.add(btDevice);
System.out.println("device found: " + name);
}
@Override
public void inquiryCompleted(int arg0) {
synchronized(lock){
lock.notify();
}
}
@Override
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized (lock) {
lock.notify();
}
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
System.out.println("hey ! " + servRecord);
for(ServiceRecord s : servRecord) {
System.out.println("-------"+ s + "-------");
}
serviceRecords.addAll(Arrays.asList(servRecord));
}
launch(args);
}
}
......@@ -6,9 +6,11 @@
#include "Bluetooth.h"
#include "Son.h"
#include "Potentiometer.h"
#include "MotionSensor.h"
#define SPAN 3000
#define HEADER_LIGHT_ON "LIGHT"
#define HEADER_SOUND_ON "SOUND"
#define HEADER_PLANT_OK "STOP"
#define REMAINING_TIME 300000 // 5MIN
#define DATASPAN 10000 // 5MIN
#define ALARMSPAN 10000 // 10SEC
......@@ -40,6 +42,7 @@ Luminosite luminosite;
Potentiometer pot;
Son buzzer;
Bluetooth bluetooth;
MotionSensor motion;
int humi;
int lumi;
......@@ -49,6 +52,7 @@ int dataTimeStamp;
unsigned long timeStamp;
boolean ledHasBeenTurnedOn;
boolean alarmHasBeenTurnedOn;
boolean plantInNeed;
unsigned long timePushed;
unsigned long alarmTimeStamp;
unsigned long lightTimeStamp;
......@@ -63,7 +67,11 @@ void updateSensors(){
void display(){
monEcran1.setContrast(70, false);
updateSensors();
monEcran1.printMsg("CRYING PLANT !",0);
if(plantInNeed){
monEcran1.printMsg("CRYING PLANT !",0);
} else {
monEcran1.printMsg("PLANT IS OK !",0);
}
monEcran1.printMsg(((String)"TEMP : ") + temp,2);
monEcran1.printMsg(((String)"HUMI : ") + humi,3);
monEcran1.printMsg(((String)"LUMI : ") + lumi,4);
......@@ -96,9 +104,13 @@ void receiveFromBluetooth() {
monEcran1.printMsg(buffer,0);
if (buffer.startsWith(HEADER_LIGHT_ON)){
dealWithLight();
plantInNeed = true;
}
else if (buffer.startsWith(HEADER_SOUND_ON)){
dealWithSound();
plantInNeed = true;
} else if(buffer.startsWith(HEADER_PLANT_OK)) {
plantInNeed = false;
}
}
......@@ -132,6 +144,7 @@ void setup()
timeStamp = 0;
ledHasBeenTurnedOn = false;
alarmHasBeenTurnedOn = false;
plantInNeed = false;
timePushed = 0;
alarmTimeStamp = 0;
dataTimeStamp = 0;
......@@ -180,7 +193,12 @@ if(millis() - timePushed < REMAINING_TIME){
ledHasBeenTurnedOn = false;
maLed1.eteint();
}
}
}
if(plantInNeed && motion.detectsStartOfMotion()){
dealWithSound();
dealWithLight();
}
}
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