Skip to content

Commit

Permalink
feat(java): fix read
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 c2536b9 commit 55b599b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Expand Up @@ -6,7 +6,11 @@
import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

public class BertyDevice {

Expand Down
Expand Up @@ -33,6 +33,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -241,12 +242,15 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
UUID charID = characteristic.getUuid();
Log.e(TAG, "READ");

if (charID.equals(MA_UUID)) {
Log.e(TAG, "READ MA");
mBluetoothGattServer.sendResponse(device, requestId, GATT_SUCCESS, offset, ma.getBytes(Charset.forName("UTF-8")));
} else if (charID.equals(PEER_ID_UUID)) {
Log.e(TAG, "READ PEER");
mBluetoothGattServer.sendResponse(device, requestId, GATT_SUCCESS, offset, peerID.getBytes(Charset.forName("UTF-8")));
} else {
Log.e(TAG, "READ UNKNOW");
mBluetoothGattServer.sendResponse(device, requestId, GATT_FAILURE, offset, null);
}
}
Expand Down Expand Up @@ -296,6 +300,7 @@ public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, Blue

@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
Log.e(TAG, "ON MTU CHANGED SERV");
Log.d(TAG, "new mtu is: " + mtu);
BertyDevice bertyDevice = getDeviceFromAddr(device.getAddress());
bertyDevice.mtu = mtu;
Expand Down Expand Up @@ -488,6 +493,34 @@ public String realTest() {
return null;
}

public @Nullable BertyDevice getDeviceFromMa(String ma) {
synchronized (bertyDevices) {
BertyDevice bDevice = null;
for (Map.Entry<String, BertyDevice> entry : bertyDevices.entrySet()) {
bDevice = entry.getValue();
if (bDevice.ma.equals(ma)) {
return bDevice;
}
}
}
return null;
}

public boolean dialPeer(String ma) {
// List<BluetoothDevice> connectedDevices = mBluetoothGattServer.getConnectedDevices();
BertyDevice bDevice = getDeviceFromMa(ma);
if (bDevice != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
bDevice.gatt.discoverServices();
}
return true;
}
return false;
// for (BluetoothDevice device : connectedDevices) {
//
// }
}

public void initGattCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mGattCallback = new BluetoothGattCallback() {
Expand Down Expand Up @@ -557,6 +590,7 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {

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

super.onServicesDiscovered(gatt, status);
}
Expand Down Expand Up @@ -604,6 +638,7 @@ public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {

@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
Log.e(TAG, "ON MTU CHANGED CLI");
BertyDevice bertyDevice = getDeviceFromAddr(gatt.getDevice().getAddress());
bertyDevice.mtu = mtu;
super.onMtuChanged(gatt, mtu, status);
Expand Down
1 change: 1 addition & 0 deletions client/react-native/gomobile/core/java.go
Expand Up @@ -38,6 +38,7 @@ func ConnClosed(bleUUID string) {
}

func BytesToConn(bleUUID string, b []byte) {
fmt.Printf("BYTES TO CONN FROM JAVA %+v\n", b)
ble.BytesToConn(bleUUID, b)
}

Expand Down
10 changes: 5 additions & 5 deletions core/network/ble/conn.go
Expand Up @@ -11,7 +11,6 @@ import (
yamux "github.com/whyrusleeping/yamux"
"go.uber.org/zap"
)
import "time"

type Conn struct {
tpt.Conn
Expand All @@ -34,15 +33,16 @@ type ConnForSmux struct {
var conns map[string]*Conn = make(map[string]*Conn)

func BytesToConn(bleUUID string, b []byte) {
go func(bleUUID string, b []byte) {
tmp := make([]byte, len(b))
copy(tmp, b)
go func(bleUUID string, tmp []byte) {
for {
if conn, ok := conns[bleUUID]; ok {
conn.incoming <- b
conn.incoming <- tmp
return
}
time.Sleep(1 * time.Second)
}
}(bleUUID, b)
}(bleUUID, tmp)
}

func ConnClosed(bleUUID string) {
Expand Down

0 comments on commit 55b599b

Please sign in to comment.