Skip to content

Commit

Permalink
fix(ble): fix listener.Close on 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 29, 2018
1 parent b7e106c commit 0f781a8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
Expand Up @@ -172,6 +172,15 @@ public void initScannerAndAdvertiser() {
}
}

public void closeScannerAndAdvertiser() {
stopScanning();
stopAdvertising();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothGattServer.close();
}
}

public static Manager getInstance() {
if (instance == null) {
synchronized (Manager.class) {
Expand Down Expand Up @@ -495,6 +504,12 @@ public void startAdvertising() {
}
}

public void stopAdvertising() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeAdvertiser.stopAdvertising(mAdvertisingCallback);
}
}

public void startScanning() {
ScanSettings settings = createScanSetting();
ScanFilter filter = makeFilter();
Expand All @@ -504,6 +519,12 @@ public void startScanning() {
}
}

public void stopScanning() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeScanner.stopScan(mScanCallback);
}
}

protected BluetoothGattCallback mGattCallback;

public @Nullable BertyDevice getDeviceFromAddr(String addr) {
Expand Down
1 change: 1 addition & 0 deletions client/react-native/gomobile/core/java.go
Expand Up @@ -17,6 +17,7 @@ func initBleFunc() {
ble.Write = Manager.GetInstance().Write
ble.DialPeer = Manager.GetInstance().DialPeer
ble.InitScannerAndAdvertiser = Manager.GetInstance().InitScannerAndAdvertiser
ble.CloseScannerAndAdvertiser = Manager.GetInstance().CloseScannerAndAdvertiser
}

func JavaToGo() string {
Expand Down
4 changes: 4 additions & 0 deletions core/network/ble/darwin.go
Expand Up @@ -153,3 +153,7 @@ func AddToPeerStoreC(peerID *C.char, rAddr *C.char) {
goRAddr := C.GoString(rAddr)
AddToPeerStore(goPeerID, goRAddr)
}

func (b *Listener) closeNative() {

}
5 changes: 5 additions & 0 deletions core/network/ble/java.go
Expand Up @@ -18,6 +18,7 @@ var StartAdvertising func() = nil
var Write func(p []byte, ma string) bool = nil
var DialPeer func(ma string) bool = nil
var InitScannerAndAdvertiser func() = nil
var CloseScannerAndAdvertiser func() = nil

func (t *Transport) Dial(ctx context.Context, rAddr ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
// if int(C.isDiscovering()) != 1 {
Expand Down Expand Up @@ -105,3 +106,7 @@ func (b *Conn) IsClosed() bool {

return b.closed
}

func (b *Listener) closeNative() {
CloseScannerAndAdvertiser()
}
1 change: 1 addition & 0 deletions core/network/ble/listener.go
Expand Up @@ -88,6 +88,7 @@ func (b *Listener) Close() error {
return fmt.Errorf("ble listerner already closed")
}
default:
b.closeNative()
close(b.closer)
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions core/network/ble/os_other.go
Expand Up @@ -55,3 +55,7 @@ func (t *Transport) Proxy() bool {
func (t *Transport) String() string {
return "ble"
}

func (b *Listener) closeNative() {

}

0 comments on commit 0f781a8

Please sign in to comment.