Skip to content

Commit

Permalink
feat(java): impl latchChar
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 29, 2018
1 parent b4d41b7 commit 24570dc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 101 deletions.
Expand Up @@ -6,12 +6,18 @@
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -105,7 +111,8 @@ public void run() {
Thread.currentThread().setName("WaitService");
try {
svcSema.acquire();
Log.e(TAG, "Need to launch char disco");
waitChar();
populateCharacteristic();
} catch (Exception e) {
Log.e(TAG, "Error waiting/writing " + e.getMessage());
}
Expand All @@ -114,6 +121,67 @@ public void run() {
}).start();
}

public void waitChar() {
Log.e(TAG, "waitChar()");
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("WaitChar");
try {
latchChar.await();
Log.e(TAG, "Need to launch read char");
} catch (Exception e) {
Log.e(TAG, "Error waiting/writing " + e.getMessage());
}

}
}).start();
}

public void populateCharacteristic() {
Log.e(TAG, "populateCharacteristic()");
ExecutorService es = Executors.newFixedThreadPool(6);
List<PopulateCharacteristic> todo = new ArrayList<>(6);

todo.add(new PopulateCharacteristic(BertyUtils.MA_UUID));
todo.add(new PopulateCharacteristic(BertyUtils.PEER_ID_UUID));
todo.add(new PopulateCharacteristic(BertyUtils.CLOSER_UUID));
todo.add(new PopulateCharacteristic(BertyUtils.WRITER_UUID));
todo.add(new PopulateCharacteristic(BertyUtils.IS_READY_UUID));
todo.add(new PopulateCharacteristic(BertyUtils.ACCEPT_UUID));

try {
List<Future<BluetoothGattCharacteristic>> answers = es.invokeAll(todo);
for (Future<BluetoothGattCharacteristic> future : answers) {
BluetoothGattCharacteristic c = future.get();

if (c != null && c.getUuid().equals(BertyUtils.MA_UUID)) {
maCharacteristic = c;
latchChar.countDown();
} else if (c != null && c.getUuid().equals(BertyUtils.PEER_ID_UUID)) {
peerIDCharacteristic = c;
latchChar.countDown();
} else if (c != null && c.getUuid().equals(BertyUtils.CLOSER_UUID)) {
closerCharacteristic = c;
latchChar.countDown();
} else if (c != null && c.getUuid().equals(BertyUtils.WRITER_UUID)) {
writerCharacteristic = c;
latchChar.countDown();
} else if (c != null && c.getUuid().equals(BertyUtils.IS_READY_UUID)) {
isRdyCharacteristic = c;
latchChar.countDown();
} else if (c != null && c.getUuid().equals(BertyUtils.ACCEPT_UUID)) {
acceptCharacteristic = c;
latchChar.countDown();
} else {
Log.e(TAG, "Retrieved an unknown characteristic");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void writeRdy() {
new Thread(new Runnable() {
@Override
Expand Down Expand Up @@ -159,4 +227,21 @@ public void write(byte[] p) throws InterruptedException {
}
}
}

private class PopulateCharacteristic implements Callable<BluetoothGattCharacteristic> {
private UUID uuid;

public PopulateCharacteristic(UUID charactUUID) {
uuid = charactUUID;
}

public @Nullable
BluetoothGattCharacteristic call() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return gatt.getService(BertyUtils.SERVICE_UUID).getCharacteristic(uuid);
}

return null;
}
}
}
Expand Up @@ -264,88 +264,6 @@ public boolean dialPeer(String ma) {
return false;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void populateCharacteristic(BluetoothGatt gatt) {
BertyDevice bDevice = BertyUtils.getDeviceFromAddr(gatt.getDevice().getAddress());
ExecutorService es = Executors.newFixedThreadPool(6);
List<PopulateCharacteristic> todo = new ArrayList<>(6);

todo.add(new PopulateCharacteristic(MA_UUID, bDevice));
todo.add(new PopulateCharacteristic(PEER_ID_UUID, bDevice));
todo.add(new PopulateCharacteristic(CLOSER_UUID, bDevice));
todo.add(new PopulateCharacteristic(WRITER_UUID, bDevice));
todo.add(new PopulateCharacteristic(IS_READY_UUID, bDevice));
todo.add(new PopulateCharacteristic(ACCEPT_UUID, bDevice));

Log.e(TAG, "START DISCO CHAR");

try {
List<Future<BluetoothGattCharacteristic>> answers = es.invokeAll(todo);
for (Future<BluetoothGattCharacteristic> future : answers) {
BluetoothGattCharacteristic c = future.get();

if (c != null && c.getUuid().equals(MA_UUID)) {
bDevice.maCharacteristic = c;
bDevice.latchChar.countDown();
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("ReadMaWaiter");
while (!gatt.readCharacteristic(bDevice.maCharacteristic)) {
Log.e(TAG, "Gonna wait");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.e(TAG, "TEST " + bDevice.maCharacteristic.getValue());
}
}).start();
} else if (c != null && c.getUuid().equals(PEER_ID_UUID)) {
bDevice.peerIDCharacteristic = c;
bDevice.latchChar.countDown();
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("ReadPeerWaiter");
while (!gatt.readCharacteristic(bDevice.peerIDCharacteristic)) {
Log.e(TAG, "Gonna wait");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.e(TAG, "TEST " + bDevice.peerIDCharacteristic.getValue());
}
}).start();
} else if (c != null && c.getUuid().equals(CLOSER_UUID)) {
bDevice.closerCharacteristic = c;
bDevice.latchChar.countDown();
} else if (c != null && c.getUuid().equals(WRITER_UUID)) {
bDevice.writerCharacteristic = c;
bDevice.latchChar.countDown();
} else if (c != null && c.getUuid().equals(IS_READY_UUID)) {
bDevice.isRdyCharacteristic = c;
bDevice.latchChar.countDown();
} else if (c != null && c.getUuid().equals(ACCEPT_UUID)) {
bDevice.acceptCharacteristic = c;
bDevice.latchChar.countDown();
} else {
Log.e(TAG, "UNKNOW CHARACT");
}

Log.e(TAG, "UUID " + c.getUuid());
}
} catch (Exception e) {
e.printStackTrace();
}

while (!gatt.requestMtu(512)) {
/** intentionally empty */
}
}

public void handleMaRead(BertyDevice device, BluetoothGattCharacteristic characteristic) {
String newMa = null;
Expand Down Expand Up @@ -409,24 +327,6 @@ public boolean write(byte[] p, String ma) {
return true;
}

public class PopulateCharacteristic implements Callable<BluetoothGattCharacteristic> {
private UUID uuid;
private BertyDevice device;

public PopulateCharacteristic(UUID charactUUID, BertyDevice bDevice) {
uuid = charactUUID;
device = bDevice;
}

public @Nullable BluetoothGattCharacteristic call() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return device.gatt.getService(SERVICE_UUID).getCharacteristic(uuid);
}

return null;
}
}

@Override
protected void finalize() throws Throwable {
try {
Expand Down

0 comments on commit 24570dc

Please sign in to comment.