Skip to content

Commit

Permalink
feat(java): impl svcSema
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 bf3c080 commit b4d41b7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
@@ -1,8 +1,10 @@
package chat.berty.ble;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.util.Log;

Expand All @@ -13,6 +15,7 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class BertyDevice {

public static String TAG = "chat.berty.ble.BertyDevice";
Expand All @@ -30,6 +33,7 @@ public class BertyDevice {
public Semaphore svcSema;
public Semaphore isWaiting;
public List<byte[]> toSend;
protected BluetoothGattService svc;
protected BluetoothGattCharacteristic acceptCharacteristic;
protected BluetoothGattCharacteristic maCharacteristic;
protected BluetoothGattCharacteristic peerIDCharacteristic;
Expand All @@ -42,7 +46,7 @@ public BertyDevice(BluetoothDevice device, BluetoothGatt gatt, String address) {
this.addr = address;
this.device = device;
this.isWaiting = new Semaphore(1);
this.svcSema = new Semaphore(1);
this.svcSema = new Semaphore(0);
this.latchRdy = new CountDownLatch(2);
this.latchConn = new CountDownLatch(2);
this.latchChar = new CountDownLatch(6);
Expand Down Expand Up @@ -73,13 +77,35 @@ public void run() {
}

public void waitConn() {
Log.e(TAG, "waitConn()");
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("waitConn");
Thread.currentThread().setName("WaitConn");
try {
latchConn.await();
Log.e(TAG, "BOTH CONN RDY");
while (!gatt.discoverServices()){
Log.e(TAG, "Waiting service discover");
Thread.sleep(1000);
}
waitService();
} catch (Exception e) {
Log.e(TAG, "Error waiting/writing " + e.getMessage());
}

}
}).start();
}

public void waitService() {
Log.e(TAG, "waitService()");
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("WaitService");
try {
svcSema.acquire();
Log.e(TAG, "Need to launch char disco");
} catch (Exception e) {
Log.e(TAG, "Error waiting/writing " + e.getMessage());
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.os.Build;
import android.util.Log;
Expand Down Expand Up @@ -131,14 +132,13 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.e(TAG, "onServicesDiscovered()");
// Log.e(TAG, "START DISCO CHAR3");
// for (BluetoothGattService svc : gatt.getServices()) {
// Log.e(TAG, "START DISCO CHAR2 " + svc.getUuid());
// if (svc.getUuid().equals(SERVICE_UUID)) {
// Log.e(TAG, "START DISCO CHAR1");
// populateCharacteristic(gatt);
// }
// }
BertyDevice bDevice = BertyUtils.getDeviceFromAddr(gatt.getDevice().getAddress());
for (BluetoothGattService svc : gatt.getServices()) {
if (svc.getUuid().equals(BertyUtils.SERVICE_UUID)) {
bDevice.svc = svc;
bDevice.svcSema.release();
}
}
super.onServicesDiscovered(gatt, status);
}

Expand Down

0 comments on commit b4d41b7

Please sign in to comment.