Skip to content

Commit

Permalink
feat(ble): bind native ble code with Berty app
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Jan 16, 2019
1 parent 9182b6f commit b6f744d
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 134 deletions.
@@ -1,3 +1,3 @@
#Fri Nov 30 15:00:47 CET 2018
#Wed Jan 16 16:26:32 CET 2019
eclipse.preferences.version=1
connection.project.dir=
@@ -1,2 +1,2 @@
#Fri Nov 30 15:00:47 CET 2018
#Wed Jan 16 16:26:33 CET 2019
connection.project.dir=..
Expand Up @@ -6,11 +6,12 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import chat.berty.ble.Manager;
import core.Core;
import core.MobileNotification;
import core.NativeNotificationDriver;

import chat.berty.ble.BleManager;

public class CoreModule extends ReactContextBaseJavaModule {
private Logger logger = new Logger("chat.berty.io");
private String filesDir = "";
Expand All @@ -22,17 +23,10 @@ public CoreModule(ReactApplicationContext reactContext) {
this.filesDir = reactContext.getFilesDir().getAbsolutePath();
this.reactContext = reactContext;

Object o = actGetter(reactContext);

Manager.getInstance().setmReactContext(o, reactContext);
}
BleManager.setContext(reactContext.getApplicationContext());
BleManager.setActivity(reactContext.getCurrentActivity());

private Object actGetter(final ReactApplicationContext reactContext){
return new Manager.ActivityGetter() {
public Activity getCurrentActivity() {
return reactContext.getCurrentActivity();
}
};
this.notificationDriver.setNativeNotification(new Notification(reactContext, this.notificationDriver));
}

public String getName() {
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.lang.System;
import java.util.List;

import chat.berty.ble.Manager;
import chat.berty.core.CorePackage;

public class MainApplication extends Application implements ReactApplication {
Expand Down Expand Up @@ -77,7 +76,5 @@ public ReactNativeHost getReactNativeHost() {
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);

Manager.getInstance().setmContext(this.getApplicationContext());
}
}
@@ -1,3 +1,3 @@
#Fri Nov 30 15:00:47 CET 2018
#Wed Jan 16 16:26:34 CET 2019
eclipse.preferences.version=1
connection.project.dir=..
@@ -1,4 +1,4 @@
package tech.berty.bletesting;
package chat.berty.ble;

import android.os.Build;
import android.annotation.TargetApi;
Expand Down Expand Up @@ -82,4 +82,4 @@ public void onStartFailure(int errorCode) {

super.onStartFailure(errorCode);
}
}
}
@@ -1,4 +1,6 @@
package tech.berty.bletesting;
package chat.berty.ble;

import core.Core;

import android.os.Build;
import android.content.Context;
Expand Down Expand Up @@ -152,7 +154,7 @@ public void run() {
if (bertyHandshake()) {
Log.i(TAG, "asyncConnectionToDevice() succeeded with device: " + dDevice + ", MultiAddr: " + dMultiAddr + ", PeerID: " + dPeerID);
identified = true;
// TODO: core.addToPeerstore
Core.addToPeerStore(dPeerID, dMultiAddr);
} else {
Log.d(TAG, "asyncConnectionToDevice() Berty handshake failed with device: " + dDevice);
disconnectFromDevice();
Expand All @@ -167,7 +169,9 @@ public void run() {
} else {
if (identified) {
Log.e(TAG, "asyncConnectionToDevice() reconnection failed: connection lost with previously connected device: " + dDevice + ", MultiAddr: " + dMultiAddr + ", PeerID: " + dPeerID);
// TODO: core.connClosed
// TODO: Check with sfroment if it's ok to use connClosed that way
// TODO: Check with sfroment how libp2p handle a reconnection with a different mac address
Core.connClosed(dMultiAddr);
} else {
Log.e(TAG, "asyncConnectionToDevice() failed: can't connect GATT with device: " + dDevice);
}
Expand All @@ -182,7 +186,7 @@ public void run() {
}

// Disconnect device and remove it from index
private void disconnectFromDevice() {
void disconnectFromDevice() {
Log.d(TAG, "disconnectFromDevice() called for device: " + dDevice);

try {
Expand All @@ -201,7 +205,7 @@ private void setGatt() {
Log.d(TAG, "setGatt() called for device: " + dDevice);

if (dGatt == null) {
dGatt = dDevice.connectGatt(AppData.getCurrContext(), false, BleManager.getGattCallback());
dGatt = dDevice.connectGatt(BleManager.getContext(), false, BleManager.getGattCallback());
}
}

Expand Down Expand Up @@ -247,8 +251,7 @@ private boolean connectGatt() {
return false;
}

// TODO: set this as private
void disconnectGatt() {
private void disconnectGatt() {
Log.d(TAG, "disconnectGatt() called for device: " + dDevice);

if (dGatt != null) {
Expand All @@ -267,7 +270,7 @@ void setMtu(int mtu) {
int getGattClientState() {
Log.v(TAG, "getGattClientState() called for device: " + dDevice);

final Context context = AppData.getCurrContext();
final Context context = BleManager.getContext();
final BluetoothManager manager = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if (manager == null) {
Log.e(TAG, "Can't get BLE Manager");
Expand All @@ -280,7 +283,7 @@ int getGattClientState() {
int getGattServerState() {
Log.v(TAG, "getGattServerState() called for device: " + dDevice);

final Context context = AppData.getCurrContext();
final Context context = BleManager.getContext();
final BluetoothManager manager = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if (manager == null) {
Log.e(TAG, "Can't get BLE Manager");
Expand Down Expand Up @@ -496,4 +499,4 @@ boolean writeOnCharacteristic(byte[] payload, BluetoothGattCharacteristic charac
return false;
}
}
}
}

0 comments on commit b6f744d

Please sign in to comment.