new structure

This commit is contained in:
Xavier 2017-02-20 15:41:25 +01:00
parent 1f31a6037e
commit c6aabde0b2
6 changed files with 0 additions and 548 deletions

View File

@ -1,42 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xavier.btkom">
<permission android:name="android.permission.BLUETOOTH"/>
<permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<permission android:name="android.permission.RECEIVE_SMS"/>
<permission android:name="android.permission.READ_SMS"/>
<permission android:name="android.permission.READ_CONTACTS"/>
<permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>

View File

@ -1,140 +0,0 @@
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<Message> 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<Message>();
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<String> packets = new ArrayList<String>();
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<number; i++){
Log.i("log", "size : " + ByteBuffer.wrap(buffer, i*SIZE_MESSAGE+40, 4).getInt());
str += new String(Arrays.copyOfRange(buffer, i*SIZE_MESSAGE+SIZE_HEADER, i*SIZE_MESSAGE+SIZE_HEADER+ByteBuffer.wrap(buffer, i*SIZE_MESSAGE+40, 4).getInt()-1));
}
}else{
str = new String(buffer, 0, bytes);
}
msg = new Message();
msg.setTotalMessage(str);
//messages.add(msg);
id = msg.getID();
if(id != caltoID + 1){
Log.i("log", "id : wrong id number "+id);
}else{
treatMessage(msg);
}
available = 0;
}else if(input.available() != available){
available = input.available();
Thread.currentThread().sleep(100);
}
}catch (IOException | InterruptedException e) {
}
}
}
});
listening.start();
}
private void treatMessage(Message msg){
switch(msg.getType()){
case "SYN":
}
}
private int connect() throws IOException{
String message = "YYYY+MM+DD+HH+MM+SS"+"SYN"+Integer.toHexString(this.androidID)+"telephone\0"+"000000000101";
output.write(message.getBytes());
return 0;
}
int setSender(String senderName){
if(senderName.length() <= 10){
this.senderName = senderName;
return 1;
}
return 0;
}
int sendMessage(Message message){
return 1;
}
void receptMessage(){
}
Message getLastMessage(){
if(messages.size() == 0) return null;
return messages.get(messages.size()-1);
}
Message getMessage(int id){
for(Message message : messages){
if(message.getID() == id) return message;
}
return null;
}
}

View File

@ -1,35 +0,0 @@
package xavier.btkom;
/**
* Created by Xavier on 19/02/2017.
*/
public class Constants {
// GLOBAL TRANSFER INFORMATIONS
public final int SIZE_TX = 1000;
public final int SIZE_RX = 255;
public final int SIZE_HEADER = 46;
public final int SIZE_DATA_TX = (SIZE_RX - SIZE_HEADER);
// TYPES
public final int RQT = 1;
public final int ASW = 2;
public final int ACK = 4;
public final int HED = 8;
public final int FUL = 16;
public final int ERR = 32;
public final int END = 64;
public final int SYN = 128;
// POSITIONS IN HEADER
public final int TIME_POS = 0;
public final int TYPE_POS = TIME_POS + 16;
public final int ID_POS = TYPE_POS + 1;
public final int SENDER_POS = ID_POS + 4;
public final int MESSAGE_LENGTH_POS = SENDER_POS + 10;
public final int PACKET_LENGTH_POS = MESSAGE_LENGTH_POS + 4;
public final int PART_POS = PACKET_LENGTH_POS + 4;
public final int NUMBER_PART_POS = PART_POS + 2;
}

View File

@ -1,203 +0,0 @@
package xavier.btkom;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.ParcelUuid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static android.bluetooth.BluetoothDevice.BOND_BONDED;
import static android.bluetooth.BluetoothDevice.BOND_BONDING;
import static android.bluetooth.BluetoothDevice.BOND_NONE;
import static android.widget.Toast.LENGTH_SHORT;
public class MainActivity extends AppCompatActivity {
private BTKOM btkom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void addLog(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView text = (TextView)findViewById(R.id.logs);
text.setText(text.getText() + "\n" + str);
}
});
}
protected void end(final String str) {
Log.i("log", "end: "+str);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.connect).setClickable(true);
}
public void connect(View view) throws ExecutionException, InterruptedException {
findViewById(R.id.connect).setClickable(false);
TextView text = (TextView) findViewById(R.id.input);
final String input = text.getText().toString();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<BTKOM> future = executor.submit(new Callable<BTKOM>(){
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<BluetoothDevice> 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<BluetoothDevice> 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<BluetoothDevice> 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<BluetoothDevice>();
}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");
}
}
}

View File

@ -1,81 +0,0 @@
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; }
}

View File

@ -1,47 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="xavier.btkom.MainActivity"
android:gravity="center_horizontal">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<EditText
android:layout_width="500px"
android:layout_height="130px"
android:hint="Bluetooth MAC"
android:layout_marginRight="50px"
android:id="@+id/input"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect to calc"
android:onClick="connect"
android:id="@+id/connect"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logs"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textIsSelectable="true"
android:id="@+id/logs"/>
</LinearLayout>