Skip to content

Commit

Permalink
feat(java): write ble for java
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 55b599b commit c2cf5a5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Expand Up @@ -34,11 +34,17 @@ public class BertyDevice {

public CountDownLatch waitReady;

public Semaphore isWaiting;

public List<byte[]> toSend;

public BertyDevice(BluetoothDevice device, BluetoothGatt gatt, String address) {
this.gatt = gatt;
this.addr = address;
this.device = device;
this.isWaiting = new Semaphore(1);
waitReady = new CountDownLatch(2);
this.toSend = new ArrayList<>();
new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -58,4 +64,29 @@ public void isRdy() {
}
}
}

public void write(byte[] p) throws InterruptedException {
waitReady.await();
synchronized (toSend) {

int length = p.length;
int offset = 0;
do {
int chunckSize = length - offset > mtu ? mtu : length - offset;
byte[] chunck = Arrays.copyOfRange(p, offset, chunckSize);
offset += chunckSize;
toSend.add(chunck);
} while (offset < length);


while (!toSend.isEmpty()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
writerCharacteristic.setValue(toSend.get(0));
while (!gatt.writeCharacteristic(writerCharacteristic));
isWaiting.acquire();
}
toSend.remove(0);
}
}
}
}
Expand Up @@ -259,10 +259,16 @@ public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, i
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
UUID charID = characteristic.getUuid();
BertyDevice bDevice = getDeviceFromAddr(device.getAddress());
Log.e(TAG, "WRITE");

if (charID.equals(ACCEPT_UUID)) {
mBluetoothGattServer.sendResponse(device, requestId, GATT_SUCCESS, offset, null);
} else if (charID.equals(WRITER_UUID)) {
// Log.e(TAG, "READER CALLED " + Arrays.toString(value));
try {
bDevice.waitReady.await();
} catch (Exception e) {
Log.e(TAG, "FAIL AWAIT " + e.getMessage());
}
Core.bytesToConn(bDevice.ma, value);
mBluetoothGattServer.sendResponse(device, requestId, GATT_SUCCESS, offset, null);
} else if (charID.equals(CLOSER_UUID)) {
Expand Down Expand Up @@ -590,7 +596,7 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {

gatt.readCharacteristic(bDevice.maCharacteristic);
gatt.readCharacteristic(bDevice.peerIDCharacteristic);
while (!gatt.requestMtu(111000));
while (!gatt.requestMtu(512));

super.onServicesDiscovered(gatt, status);
}
Expand All @@ -605,6 +611,15 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// Log.e(TAG, "charact writed");
if (status == GATT_SUCCESS) {
BertyDevice bDevice = getDeviceFromAddr(gatt.getDevice().getAddress());
bDevice.isWaiting.release();


} else {
Log.e(TAG, "Error writing gatt " + status);
}
super.onCharacteristicWrite(gatt, characteristic, status);
}

Expand Down Expand Up @@ -705,6 +720,23 @@ public void handleConnectionStateChange(BertyDevice device, int status, int newS
}
}

public boolean write(byte[] p, String ma) {
BertyDevice bDevice = getDeviceFromMa(ma);
if (bDevice == null) {
Log.e(TAG, "WRITING PAT3");
return false;
}

try {
bDevice.write(p);
} catch (Exception e) {
Log.e(TAG, "WRITING PAT2 " + e.getMessage());
return false;
}

return true;
}

public class PopulateCharacteristic implements Callable<BluetoothGattCharacteristic> {
private UUID uuid;
private BertyDevice device;
Expand Down
2 changes: 2 additions & 0 deletions client/react-native/gomobile/core/java.go
Expand Up @@ -15,6 +15,8 @@ func initBleFunc() {
ble.SetPeerID = Manager.GetInstance().SetPeerID
ble.StartScanning = Manager.GetInstance().StartScanning
ble.StartAdvertising = Manager.GetInstance().StartAdvertising
ble.Write = Manager.GetInstance().Write
ble.DialPeer = Manager.GetInstance().DialPeer
}

func JavaExportTestFunc() {
Expand Down

0 comments on commit c2cf5a5

Please sign in to comment.