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

lunion fe la forsse

parent 5cb6cf0a
No related branches found
No related tags found
No related merge requests found
/*
* 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 Bluetooth;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
/**
*
* @author invite
*/
public class BluetoothDevices {
public BluetoothDevices() throws BluetoothStateException, IOException{
LocalDevice device = LocalDevice.getLocalDevice();
RemoteDevice[] remotedevice = device.getDiscoveryAgent().retrieveDevices(DiscoveryAgent.PREKNOWN);
for(RemoteDevice d : remotedevice)
{
System.out.println("Device Name : "+d.getFriendlyName(false));
System.out.println("Bluetooth Address : "+d.getBluetoothAddress()+"\n");
}
}
}
package edu.kufpg.bluetooth.server;
package BluetoothJavaServer.src.edu.kufpg.bluetooth.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
......
package edu.kufpg.bluetooth.server;
package BluetoothJavaServer.src.edu.kufpg.bluetooth.server;
public enum BluetoothServiceUuids {
SDP(0x0001),
......
package edu.kufpg.bluetooth.server;
package BluetoothJavaServer.src.edu.kufpg.bluetooth.server;
import java.io.EOFException;
import java.io.IOException;
......@@ -6,11 +6,11 @@ import java.nio.charset.StandardCharsets;
import sun.misc.IOUtils;
class BluetoothStreamParseThread extends Thread {
public class BluetoothThread extends Thread {
private BluetoothConnection connection;
private boolean keepGoing = true;
public BluetoothStreamParseThread(BluetoothConnection connection) {
public BluetoothThread(BluetoothConnection connection) {
this.connection = connection;
}
......@@ -41,7 +41,7 @@ class BluetoothStreamParseThread extends Thread {
connection.getOutputStream().write("SOUND".getBytes());
} catch (IOException e) {
} catch (Exception e) {
System.out.println("Could not read stream" + e);
}
}
......@@ -64,7 +64,7 @@ class BluetoothStreamParseThread extends Thread {
if(null != connection.getOutputStream()) {
try {
connection.getOutputStream().flush();
} catch(IOException e) { e.printStackTrace(); }
} catch(Exception e) { e.printStackTrace(); }
connection.getOutputStream().close();
}
}
......
package edu.kufpg.bluetooth.server;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.bluetooth.DataElement;
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;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
public class MyDiscoveryListener implements DiscoveryListener{
private static Object lock=new Object();
public ArrayList<RemoteDevice> devices;
public MyDiscoveryListener() {
devices = new ArrayList<RemoteDevice>();
}
@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));
// for (int i = 0; i < servRecord.length; i++) {
// String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
// if (url == null) {
// continue;
// }
/*
int i=0;
System.out.println("enter the device number \n");
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
try {
String s = br.readLine();
i = Integer.parseInt(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
if (serviceName != null) {
System.out.println("service " + serviceName.getValue() + " found " + url);
//if(serviceName.getValue().equals("OBEX Object Push")){
if(true){
sendMessageToDevice(url);
}
} else {
System.out.println("service found " + url);
}
*/
// }
}
private static void sendMessageToDevice(String serverURL){
// TO DO
}
static DiscoveryListener listener = new MyDiscoveryListener();
final static List<ServiceRecord> serviceRecords = new ArrayList<ServiceRecord>();
final static List<BluetoothConnection> connections = new ArrayList<BluetoothConnection>();
/******************MAIN********************/
public static void main(String[] args) {
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 trouv");
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");
BluetoothStreamParseThread t = new BluetoothStreamParseThread(connections.get(0));
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
*/
package app;
import Bluetooth.BluetoothDevices;
import BluetoothJavaServer.src.edu.kufpg.bluetooth.server.*;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -14,11 +14,43 @@ import javafx.stage.Stage;
import Vue.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.bluetooth.DataElement;
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;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
/**
*
* @author invite
*/
public class App extends Application {
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>();
@Override
public void start(Stage stage) throws Exception{
......@@ -34,7 +66,101 @@ public class App extends Application {
* @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));
}
}
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