Commit f8cbe015 authored by y0no's avatar y0no
Browse files

Add signature to deviceInfo

parent f41d1ef0
......@@ -72,7 +72,6 @@ public class BtScanner {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice oBtDevice = result.getDevice();
Log.d(TAG, "Data: " + result.getScanRecord().toString());
addOrUpdateDevice(
oBtDevice,
result.getScanRecord().getBytes(),
......
......@@ -2,8 +2,6 @@ package fr.y0no.newbtle;
import android.bluetooth.BluetoothDevice;
import fr.y0no.newbtle.utils.ADParser;
/**
* Created by y0no on 07/07/16.
*/
......@@ -21,7 +19,7 @@ public class DeviceInfo {
m_lFirstSeen = m_lLastSeen = firstSeen;
m_iRssi = Rssi;
ADParser.parse(m_bData);
m_oSignature = FingerprintMgr.getOrCreateSignature(this);
}
public String getDeviceName() {
......@@ -59,4 +57,12 @@ public class DeviceInfo {
public BluetoothDevice getM_oDevice() { return m_oDevice; }
public void setM_oDevice(BluetoothDevice m_oDevice) { this.m_oDevice = m_oDevice; }
public FingerprintMgr.DeviceSignature getM_oSignature() {
return m_oSignature;
}
public void setM_oSignature(FingerprintMgr.DeviceSignature m_oSignature) {
this.m_oSignature = m_oSignature;
}
}
package fr.y0no.newbtle;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import fr.y0no.newbtle.utils.ADParser;
import fr.y0no.newbtle.utils.Conversion;
/**
* Created by y0no on 08/07/16.
*/
public class FingerprintMgr {
private static String TAG = "FingerprintMGR";
public enum DEVICE_TYPE {
UNKNOWN,
......@@ -24,11 +26,12 @@ public class FingerprintMgr {
WRISTBAND,
HEARTBEAT,
SMARTWATCH,
SOUND
SOUND,
LAPTOP
};
public class DeviceSignature {
public static class DeviceSignature {
private DEVICE_TYPE mType;
private String mName;
......@@ -53,35 +56,43 @@ public class FingerprintMgr {
m_oSignatures.put(name, signature);
}
public static DeviceSignature getSignature(DeviceInfo device) {
public static DeviceSignature getOrCreateSignature(DeviceInfo device) {
return m_oSignatures.get(device.getDeviceName());
}
private byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len/2];
/* Try a quick fingerprinting */
for (Map.Entry<String, DeviceSignature> entry : m_oSignatures.entrySet()) {
String signature = entry.getKey();
DeviceSignature model = entry.getValue();
for(int i = 0; i < len; i+=2){
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
if (compareSignature(ADParser.toRecords(device.getM_bData()),
ADParser.toRecords(signature))) {
Log.d(TAG, "Signature already known : " + model.getName());
device.setM_oSignature(model);
return model;
}
}
return data;
}
String signatureString = ADParser.toSignature(device.getM_bData());
DeviceSignature signature = new DeviceSignature(DEVICE_TYPE.UNKNOWN, device.getDeviceName());
m_oSignatures.put(signatureString, signature);
private String convertStringToHex(byte[] buf)
{
final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
Log.d(TAG, "New signature : " + signature.getName());
Log.d(TAG, m_oSignatures.toString());
return signature;
}
private boolean compareSignature(ArrayList<ADParser.Record> records, String signature) {
return false;
private static boolean compareSignature(ArrayList<ADParser.Record> records,
ArrayList<ADParser.Record> signature) {
boolean type_found = false;
for (ADParser.Record record: signature) {
for (ADParser.Record devRecord: records) {
if (record.getType() == devRecord.getType()) {
type_found = true;
}
}
}
Log.d(TAG, "Type found: " + type_found);
return type_found;
}
}
package fr.y0no.newbtle.utils;
import android.text.TextUtils;
import android.util.Log;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -9,26 +11,28 @@ import java.util.Arrays;
* Created by y0no on 08/07/16.
*/
public class ADParser {
private static String TAG = "ADParser";
public static class Record {
private byte mType;
private byte[] mData;
private byte m_bType;
private byte[] m_bData;
public Record(byte type, byte[] data) {
mType = type;
mData = data;
m_bType = type;
m_bData = data;
}
public byte getType(){
return mType;
return m_bType;
}
public byte[] getData() {
return mData;
return m_bData;
}
}
public static ArrayList<Record> parse(byte[] recordData) {
public static ArrayList<Record> toRecords(byte[] recordData) {
final ArrayList<Record> records = new ArrayList<Record>();
int i = 0;
byte type, length;
......@@ -40,11 +44,32 @@ public class ADParser {
type = recordData[i+1];
buffer = Arrays.copyOfRange(recordData, i+2, i+2+length-1);
records.add(new ADParser.Record(type, buffer));
Log.i("POF", String.format("Record %02X %02X", length, type));
Log.i("POF", String.format("Record %d %s", type, Conversion.bytesToHexString(buffer)));
i+= (length+1);
}
/* Return records. */
return records;
}
public static ArrayList<Record> toRecords(String signature) {
ArrayList<Record> records = new ArrayList<>();
String[] patterns = signature.split(",");
for (String pattern: patterns) {
int type = Integer.parseInt(pattern.substring(0,2), 16);
byte[] data = Conversion.hexStringToBytes(pattern.substring(3));
records.add(new Record((byte)type, data));
}
return records;
}
public static String toSignature(byte[] recordData) {
ArrayList<Record> records = toRecords(recordData);
ArrayList<String> sigData = new ArrayList<>();
for (ADParser.Record record: records) {
sigData.add(Conversion.bytesToHexString(new byte[]{record.getType()}) + ":" + Conversion.bytesToHexString(record.getData()));
}
return TextUtils.join(",", sigData);
}
}
package fr.y0no.newbtle.utils;
/**
* Created by y0no on 08/07/16.
*/
public class Conversion {
public static String bytesToHexString(byte[] bytes){
StringBuilder sb = new StringBuilder();
for(byte b : bytes){
sb.append(String.format("%02x", b&0xff));
}
return sb.toString();
}
public static byte[] hexStringToBytes(String s) {
int len = s.length();
byte[] data = new byte[len/2];
for(int i = 0; i < len; i+=2){
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment