Skip to content

Commit

Permalink
feat(java): read ma and peer charact
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Nov 9, 2018
1 parent 11a7db6 commit 8dba2b3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
@@ -1,15 +1,21 @@
package chat.berty.ble;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;

public class BertyDevice {

public String addr;
public String peerID;
public String ma;
public long mtu;

public BluetoothGatt gatt;

public BluetoothDevice device;

public BertyDevice(BluetoothDevice rDevice, String address) {
public BertyDevice(BluetoothDevice rDevice, BluetoothGatt rGatt, String address) {
gatt = rGatt;
addr = address;
device = rDevice;
}
Expand Down
Expand Up @@ -37,6 +37,7 @@

import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -46,6 +47,8 @@

import static android.bluetooth.BluetoothGattCharacteristic.*;
import static android.bluetooth.BluetoothGattService.SERVICE_TYPE_PRIMARY;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
import static android.content.Context.BLUETOOTH_SERVICE;

public class Manager {
Expand Down Expand Up @@ -203,7 +206,9 @@ public void initGattServerCallBack() {
mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {

BertyDevice bDevice = getDeviceFromAddr(device.getAddress());
handleConnectionStateChange(bDevice, status, newState);
Log.e(TAG, "new coob " + device.getAddress());
}

@Override
Expand Down Expand Up @@ -310,10 +315,9 @@ public void parseResult(ScanResult result) {
String addr = device.getAddress();
synchronized (bertyDevices) {
if (!bertyDevices.containsKey(addr)) {
BertyDevice bDevice = new BertyDevice(device, addr);
BluetoothGatt gatt = device.connectGatt(mContext, false, mGattCallback);
BertyDevice bDevice = new BertyDevice(device, gatt, addr);
bertyDevices.put(addr, bDevice);
device.connectGatt(mContext, false, mGattCallback);
Log.e(TAG, "connecting to " + addr);
}
}
}
Expand Down Expand Up @@ -408,6 +412,16 @@ public String realTest() {

protected BluetoothGattCallback mGattCallback;

public @Nullable BertyDevice getDeviceFromAddr(String addr) {
synchronized (bertyDevices) {
if (bertyDevices.containsKey(addr)) {
return bertyDevices.get(addr);
}
}

return null;
}

public void initGattCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mGattCallback = new BluetoothGattCallback() {
Expand All @@ -423,16 +437,25 @@ public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
BertyDevice bDevice = getDeviceFromAddr(gatt.getDevice().getAddress());
handleConnectionStateChange(bDevice, status, newState);
super.onConnectionStateChange(gatt, status, newState);
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
BluetoothGattService svc = gatt.getService(SERVICE_UUID);
gatt.readCharacteristic(svc.getCharacteristic(MA_UUID));
gatt.readCharacteristic(svc.getCharacteristic(PEER_ID_UUID));

super.onServicesDiscovered(gatt, status);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.e(TAG, "charact discovered " + new String(characteristic.getValue(), Charset.forName("UTF-8")));
BertyDevice bDevice = getDeviceFromAddr(gatt.getDevice().getAddress());
handleReadCharact(bDevice, characteristic);
super.onCharacteristicRead(gatt, characteristic, status);
}

Expand All @@ -443,6 +466,9 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "charact changed " + new String(characteristic.getValue(), Charset.forName("UTF-8")));
BertyDevice bDevice = getDeviceFromAddr(gatt.getDevice().getAddress());
handleReadCharact(bDevice, characteristic);
super.onCharacteristicChanged(gatt, characteristic);
}

Expand Down Expand Up @@ -473,4 +499,44 @@ public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
};
}
}

public void handleMaRead(BertyDevice device, BluetoothGattCharacteristic characteristic) {
String newMa = new String(characteristic.getValue(), Charset.forName("UTF-8"));
if (device.ma == null || device.ma == "" || !device.ma.equals(newMa)) {
device.ma = newMa;
if (device.peerID == null || device.peerID == "" || !device.ma.equals(newMa)) {
device.gatt.readCharacteristic(device.gatt.getService(SERVICE_UUID).getCharacteristic(PEER_ID_UUID));
}
}
}

public void handlePeerIDRead(BertyDevice device, BluetoothGattCharacteristic characteristic) {
String newPeerID = new String(characteristic.getValue(), Charset.forName("UTF-8"));
if (device.peerID == null || device.peerID == "" || !device.peerID.equals(newPeerID)) {
device.peerID = newPeerID;
if (device.ma == null || device.ma == "" || !device.peerID.equals(newPeerID)) {
device.gatt.readCharacteristic(device.gatt.getService(SERVICE_UUID).getCharacteristic(MA_UUID));
}
}
}

public void handleReadCharact(BertyDevice device, BluetoothGattCharacteristic characteristic) {
UUID charID = characteristic.getUuid();
if (charID.equals(MA_UUID)) {
handleMaRead(device, characteristic);
} else if (charID.equals(PEER_ID_UUID)) {
handlePeerIDRead(device, characteristic);
}
}

public void handleConnectionStateChange(BertyDevice device, int status, int newState) {
if (newState == STATE_CONNECTED) {
Log.e(TAG, "new newly connected device " + device.gatt);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
device.gatt.discoverServices();
}
} else if (newState == STATE_DISCONNECTED) {
Log.e(TAG, "disconnected device " + device.addr);
}
}
}

0 comments on commit 8dba2b3

Please sign in to comment.