commit 1f31a6037e01c82d2492d1be8647d1dab4664355 Author: Xavier Date: Sun Feb 19 19:26:33 2017 +0100 application bases diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml new file mode 100644 index 0000000..91d19fa --- /dev/null +++ b/src/main/AndroidManifest.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/xavier/btkom/BTKOM.java b/src/main/java/xavier/btkom/BTKOM.java new file mode 100644 index 0000000..8302157 --- /dev/null +++ b/src/main/java/xavier/btkom/BTKOM.java @@ -0,0 +1,140 @@ +package xavier.btkom; + +import android.bluetooth.BluetoothSocket; +import android.util.Log; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InterruptedIOException; +import java.io.OutputStream; +import java.lang.reflect.Array; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.Random; + +import static android.R.attr.id; + +public class BTKOM{ + + private final int SIZE_MESSAGE = 255; + private final int SIZE_HEADER = 48; + private BluetoothSocket socket; + private String senderName = "Android"; + private ArrayList messages; + private int caltoID; + private int androidID; + private boolean isSynchronizing = false; + private OutputStream output; + private InputStream input; + private int available = 0; + + BTKOM(BluetoothSocket socket){ + + messages = new ArrayList(); + this.androidID = new Random().nextInt(); + + try { + socket.connect(); + output = socket.getOutputStream(); + input = socket.getInputStream(); + //TODO generating id + SYN ACK + listen(); + connect(); + }catch(IOException e){ + throw new IllegalArgumentException("Error binding socket"); + } + + } + + void listen(){ + Thread listening = new Thread(new Runnable(){ + int id; + Message msg; + String str; + public void run(){ + ArrayList packets = new ArrayList(); + byte[] buffer = new byte[5120]; + int bytes; + while(!Thread.currentThread().isInterrupted()){ + try { + if(input.available() > 0 && input.available() == available){ + bytes = input.read(buffer); + int part = (buffer[44] << 8) + buffer[45]; + int number = (buffer[46] << 8) + buffer[47]; + if(part < number && part==1){ + str = new String(buffer, 0, SIZE_MESSAGE); + for(int i=1; i future = executor.submit(new Callable(){ + + int isPaired = -1; + BluetoothDevice device; + + public BTKOM call() { + + if(!BluetoothAdapter.checkBluetoothAddress(input)){ end("Error : Invalid address"); return null; } + + BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); + if (bluetooth != null) { + if (!bluetooth.isEnabled()) { + boolean enable = bluetooth.enable(); + if (!enable) { + end("Error : Cannot activate Bluetooth"); + return null; + } + } + + if(!BluetoothAdapter.checkBluetoothAddress(input)){ + end("Error : Invalid bluetooth address"); + return null; + } + device = bluetooth.getRemoteDevice(input); + Set devices = bluetooth.getBondedDevices(); + // On regarde si l'appareil est déjà appairé + for (BluetoothDevice dev : devices) { + if (input.equals(dev.getAddress())) isPaired = 1; + } + + IntentFilter intFilter = new IntentFilter(); + intFilter.addAction(BluetoothDevice.ACTION_FOUND); + intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); + intFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + intFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); + registerReceiver(mReceiver, intFilter); + + // Si on ne le trouve pas, on active la recherche bluetooth + if (isPaired != 1) { + if (bluetooth.isDiscovering()) bluetooth.cancelDiscovery(); + if (!bluetooth.startDiscovery()) { + end("Error : Cannot start bluetooth discovery"); + return null; + } + }else{ + return connect(); + } + + } else { + end("Error : Your device hasn't Bluetooth"); + } + return null; + } + + private boolean paired(Set devices){ + for (BluetoothDevice dev : devices) { + if (input.equals(dev.getAddress())) { + if (!dev.createBond()) { + end("Error : Cannot pair device"); + return false; + } + while (isPaired == -1) {} + if (isPaired == 0) { + end("Error : Couldn't find device " + input); + return false; + } + device = dev; + } + } + return isPaired == 1; + } + + private BTKOM connect(){ + BTKOM btkom = null; + // Si on arrive ici, alors on est forcément apairé. Il faut alors connecter les appareils + Log.e("ok", "connecting"); + try { + BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(device.getUuids()[0].getUuid()); + try { + btkom = new BTKOM(socket); + addLog("Successfully connected to " + input); + end("Connected"); + }catch(IllegalArgumentException e){ + addLog(e.getMessage()); + } + } catch (IOException e){ + e.printStackTrace(); + } + return btkom; + } + + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + private Set devices; + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (BluetoothDevice.ACTION_FOUND.equals(action)) { + Log.e("ok","adding"); + devices.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)); + }else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { + devices = new HashSet(); + }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ + Log.e("ok", "ending"); + if(paired(devices)){ + connect(); + }else{ + end("Failed to find device"); + } + }else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { + if (intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1) == BOND_BONDED && intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1) == BOND_BONDING) { + isPaired = 1; + } else if (intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1) == BOND_NONE && intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1) == BOND_BONDING) { + isPaired = 0; + } + } + } + }; + }); + + BTKOM bluetooth = future.get(); + if(bluetooth != null){ + Log.i("log","Socket well established !"); + } + else { + Log.i("log", "Error creating socket"); + } + + } +} diff --git a/src/main/java/xavier/btkom/Message.java b/src/main/java/xavier/btkom/Message.java new file mode 100644 index 0000000..4e2dc87 --- /dev/null +++ b/src/main/java/xavier/btkom/Message.java @@ -0,0 +1,81 @@ +package xavier.btkom; + +import android.util.Log; + +import java.nio.ByteBuffer; + +/** + * Created by Xavier on 26/11/2016. + */ + +public class Message { + + private boolean isHeader = false; + private boolean isType = false; + private String type; + private String date; + private String message; + private String sender; + private int ID; + + Message(){ + + } + + Message(String message){ + this.message = message; + } + + Message(String message, String type){ + this(message); + if(this.type.length() == 3) + this.type = type; + else throw new IllegalArgumentException("Type too long"); + + } + + public boolean setTotalMessage(String message){ + if(message.length() < 48) return false; + this.date = message.substring(0, 19); + Log.i("log", "date : "+ this.date); + this.type = message.substring(19, 22); + Log.i("log", "type : "+ this.type); + this.ID = ByteBuffer.wrap(message.substring(22,26).getBytes()).getInt(); + Log.i("log", "ID : "+ this.ID); + this.sender = message.substring(26, message.indexOf('\0', 26)); + Log.i("log", "sender : "+ this.sender); + this.message =message.substring(48, message.length()); + Log.i("log", "message : "+ this.message); + return true; + } + + public void setID(int ID){this.ID = ID;} + + public int setSender(String sender){ + if(sender.length() > 10) return 1; + else this.sender = sender; + return 0; + } + + public void setMessage(String message){ + this.message = message; + } + + public String getType() { return this.type; } + + public int getLength(){ + return this.message.length(); + } + + public String getSender(){ + return this.sender; + } + + public int getID(){ + return this.ID; + } + + public String getMessage(){ return this.message; } + + +} diff --git a/src/main/res/layout/activity_main.xml b/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..ee99ac8 --- /dev/null +++ b/src/main/res/layout/activity_main.xml @@ -0,0 +1,47 @@ + + + + + + + +