Skip to content

Commit 95f5369

Browse files
author
Jan
authored
fix: added empty object to shutdown method (#50)
Signed-off-by: Jan <[email protected]>
1 parent 855ca45 commit 95f5369

File tree

9 files changed

+43
-14
lines changed

9 files changed

+43
-14
lines changed

android/src/main/java/com/reactnativebledidcomm/BleDidcommModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BleDidcommModule(private val context: ReactApplicationContext) :
5454

5555
@ReactMethod
5656
fun shutdownCentral(
57+
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
5758
promise: Promise
5859
) {
5960
try {
@@ -67,6 +68,7 @@ class BleDidcommModule(private val context: ReactApplicationContext) :
6768

6869
@ReactMethod
6970
fun shutdownPeripheral(
71+
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
7072
promise: Promise
7173
) {
7274
try {

android/src/main/java/com/reactnativebledidcomm/peripheral/PeripheralManager.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ class PeripheralManager(
6363
}
6464

6565
fun shutdownPeripheral() {
66-
this.gattServer.connectedDevices.clear()
67-
this.gattServer.close()
66+
val connectedDevices = this.bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)
67+
for (device in connectedDevices) {
68+
this.gattServer.cancelConnection(device)
69+
}
70+
6871
try {
6972
this.stopAdvertising()
7073
} catch (e: Exception) {
@@ -101,6 +104,7 @@ class PeripheralManager(
101104
fun stopAdvertising() {
102105
val advertiser = bluetoothAdapter.bluetoothLeAdvertiser
103106
advertiser.stopAdvertising(advertiseCallback)
107+
this.gattServer.close()
104108
advertiseCallback = null
105109
}
106110

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,4 @@ SPEC CHECKSUMS:
466466

467467
PODFILE CHECKSUM: 79b9603a04b5cd9c3421fcf11b93d98c75a337c5
468468

469-
COCOAPODS: 1.11.3
469+
COCOAPODS: 1.12.1

example/src/App.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ const requestPermissions = async () => {
3131
}
3232

3333
export default function App() {
34+
const central = new Central()
35+
const peripheral = new Peripheral()
36+
3437
const [isCentral, setIsCentral] = React.useState<boolean>(false)
3538
const [isPeripheral, setIsPeripheral] = React.useState<boolean>(false)
3639
const [peripheralId, setPeripheralId] = React.useState<string>()
37-
const [connected, setConnected] = React.useState<boolean>(false)
38-
const [central, setCentral] = React.useState<Central>(new Central())
39-
const [peripheral, setPeripheral] = React.useState<Peripheral>(
40-
new Peripheral()
41-
)
40+
const [isConnected, setIsConnected] = React.useState<boolean>(false)
4241

4342
React.useEffect(() => {
4443
const onDiscoverPeripheralListener = central.registerOnDiscoveredListener(
@@ -51,7 +50,7 @@ export default function App() {
5150
const onConnectedPeripheralListener = central.registerOnConnectedListener(
5251
({ identifier }: { identifier: string }) => {
5352
console.log(`Connected to: ${identifier}`)
54-
setConnected(true)
53+
setIsConnected(true)
5554
}
5655
)
5756

@@ -118,6 +117,15 @@ export default function App() {
118117
)}
119118
{isCentral && (
120119
<>
120+
<Button
121+
title="shutdown"
122+
onPress={async () => {
123+
await central.shutdown()
124+
setIsConnected(false)
125+
setIsCentral(false)
126+
setPeripheralId(undefined)
127+
}}
128+
/>
121129
<Button
122130
title="set services"
123131
onPress={async () => {
@@ -134,13 +142,14 @@ export default function App() {
134142
await central.scan()
135143
}}
136144
/>
145+
137146
{peripheralId && (
138147
<Button
139148
title="connect"
140149
onPress={async () => await central.connect(peripheralId)}
141150
/>
142151
)}
143-
{connected && (
152+
{isConnected && (
144153
<Button
145154
title="write"
146155
onPress={async () => await central.sendMessage(msg)}
@@ -150,6 +159,14 @@ export default function App() {
150159
)}
151160
{isPeripheral && (
152161
<>
162+
<Button
163+
title="shutdown"
164+
onPress={async () => {
165+
await peripheral.shutdown()
166+
setIsConnected(false)
167+
setIsPeripheral(false)
168+
}}
169+
/>
153170
<Button
154171
title="set services"
155172
onPress={async () => {

ios/BleDidcomm.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ @interface RCT_EXTERN_MODULE(BleDidcomm, RCTEventEmitter)
1111
reject:(RCTPromiseRejectBlock)reject)
1212

1313
RCT_EXTERN_METHOD(shutdownCentral
14+
:(NSDictionary *)options
1415
resolve:(RCTPromiseResolveBlock)resolve
1516
reject:(RCTPromiseRejectBlock)reject)
1617

@@ -20,6 +21,7 @@ @interface RCT_EXTERN_MODULE(BleDidcomm, RCTEventEmitter)
2021
reject:(RCTPromiseRejectBlock)reject)
2122

2223
RCT_EXTERN_METHOD(shutdownPeripheral
24+
:(NSDictionary *)options
2325
resolve:(RCTPromiseResolveBlock)resolve
2426
reject:(RCTPromiseRejectBlock)reject)
2527

ios/BleDidcomm.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class BleDidcomm: React.RCTEventEmitter {
1818
}
1919

2020
@objc func shutdownPeripheral(
21+
_: [String: String],
2122
resolve: RCTPromiseResolveBlock,
2223
reject _: RCTPromiseRejectBlock
2324
) {
@@ -79,6 +80,7 @@ class BleDidcomm: React.RCTEventEmitter {
7980
}
8081

8182
@objc func shutdownCentral(
83+
_: [String: String],
8284
resolve: RCTPromiseResolveBlock,
8385
reject: RCTPromiseRejectBlock
8486
) {

src/central.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Central implements Ble {
4545

4646
public async shutdown() {
4747
try {
48-
await sdk.shutdownCentral()
48+
await sdk.shutdownCentral({})
4949
} catch (e) {
5050
throw new Error('Failed to shutdown central: ' + e)
5151
}

src/peripheral.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Peripheral implements Ble {
4545

4646
public async shutdown() {
4747
try {
48-
await sdk.shutdownPeripheral()
48+
await sdk.shutdownPeripheral({})
4949
} catch (e) {
5050
throw new Error('Failed to shutdown peripheral: ' + e)
5151
}

src/register.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const BleDidcomm = NativeModules.BleDidcomm
1818
}
1919
)
2020

21+
// Functions without any params needs an empty object to work, might be fixable by tweaking the Objective-C interface
22+
// For now we just add an empty object to functions without any params.
2123
type Sdk = {
2224
startPeripheral({}: Record<string, never>): Promise<void>
2325
startCentral({}: Record<string, never>): Promise<void>
@@ -32,8 +34,8 @@ type Sdk = {
3234
characteristicUUID: string,
3335
notifyCharacteristicUUID: string
3436
): Promise<void>
35-
shutdownCentral(): Promise<void>
36-
shutdownPeripheral(): Promise<void>
37+
shutdownCentral({}: Record<string, never>): Promise<void>
38+
shutdownPeripheral({}: Record<string, never>): Promise<void>
3739
scan({}: Record<never, never>): Promise<void>
3840
stopScan(): void
3941
advertise({}: Record<never, never>): Promise<void>

0 commit comments

Comments
 (0)