Skip to content

Commit

Permalink
fix: added empty object to shutdown method (#50)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan <[email protected]>
  • Loading branch information
Jan authored Jun 30, 2023
1 parent 855ca45 commit 95f5369
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BleDidcommModule(private val context: ReactApplicationContext) :

@ReactMethod
fun shutdownCentral(
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
promise: Promise
) {
try {
Expand All @@ -67,6 +68,7 @@ class BleDidcommModule(private val context: ReactApplicationContext) :

@ReactMethod
fun shutdownPeripheral(
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
promise: Promise
) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ class PeripheralManager(
}

fun shutdownPeripheral() {
this.gattServer.connectedDevices.clear()
this.gattServer.close()
val connectedDevices = this.bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)
for (device in connectedDevices) {
this.gattServer.cancelConnection(device)
}

try {
this.stopAdvertising()
} catch (e: Exception) {
Expand Down Expand Up @@ -101,6 +104,7 @@ class PeripheralManager(
fun stopAdvertising() {
val advertiser = bluetoothAdapter.bluetoothLeAdvertiser
advertiser.stopAdvertising(advertiseCallback)
this.gattServer.close()
advertiseCallback = null
}

Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 79b9603a04b5cd9c3421fcf11b93d98c75a337c5

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
31 changes: 24 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ const requestPermissions = async () => {
}

export default function App() {
const central = new Central()
const peripheral = new Peripheral()

const [isCentral, setIsCentral] = React.useState<boolean>(false)
const [isPeripheral, setIsPeripheral] = React.useState<boolean>(false)
const [peripheralId, setPeripheralId] = React.useState<string>()
const [connected, setConnected] = React.useState<boolean>(false)
const [central, setCentral] = React.useState<Central>(new Central())
const [peripheral, setPeripheral] = React.useState<Peripheral>(
new Peripheral()
)
const [isConnected, setIsConnected] = React.useState<boolean>(false)

React.useEffect(() => {
const onDiscoverPeripheralListener = central.registerOnDiscoveredListener(
Expand All @@ -51,7 +50,7 @@ export default function App() {
const onConnectedPeripheralListener = central.registerOnConnectedListener(
({ identifier }: { identifier: string }) => {
console.log(`Connected to: ${identifier}`)
setConnected(true)
setIsConnected(true)
}
)

Expand Down Expand Up @@ -118,6 +117,15 @@ export default function App() {
)}
{isCentral && (
<>
<Button
title="shutdown"
onPress={async () => {
await central.shutdown()
setIsConnected(false)
setIsCentral(false)
setPeripheralId(undefined)
}}
/>
<Button
title="set services"
onPress={async () => {
Expand All @@ -134,13 +142,14 @@ export default function App() {
await central.scan()
}}
/>

{peripheralId && (
<Button
title="connect"
onPress={async () => await central.connect(peripheralId)}
/>
)}
{connected && (
{isConnected && (
<Button
title="write"
onPress={async () => await central.sendMessage(msg)}
Expand All @@ -150,6 +159,14 @@ export default function App() {
)}
{isPeripheral && (
<>
<Button
title="shutdown"
onPress={async () => {
await peripheral.shutdown()
setIsConnected(false)
setIsPeripheral(false)
}}
/>
<Button
title="set services"
onPress={async () => {
Expand Down
2 changes: 2 additions & 0 deletions ios/BleDidcomm.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ @interface RCT_EXTERN_MODULE(BleDidcomm, RCTEventEmitter)
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(shutdownCentral
:(NSDictionary *)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

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

RCT_EXTERN_METHOD(shutdownPeripheral
:(NSDictionary *)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

Expand Down
2 changes: 2 additions & 0 deletions ios/BleDidcomm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BleDidcomm: React.RCTEventEmitter {
}

@objc func shutdownPeripheral(
_: [String: String],
resolve: RCTPromiseResolveBlock,
reject _: RCTPromiseRejectBlock
) {
Expand Down Expand Up @@ -79,6 +80,7 @@ class BleDidcomm: React.RCTEventEmitter {
}

@objc func shutdownCentral(
_: [String: String],
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
Expand Down
2 changes: 1 addition & 1 deletion src/central.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Central implements Ble {

public async shutdown() {
try {
await sdk.shutdownCentral()
await sdk.shutdownCentral({})
} catch (e) {
throw new Error('Failed to shutdown central: ' + e)
}
Expand Down
2 changes: 1 addition & 1 deletion src/peripheral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Peripheral implements Ble {

public async shutdown() {
try {
await sdk.shutdownPeripheral()
await sdk.shutdownPeripheral({})
} catch (e) {
throw new Error('Failed to shutdown peripheral: ' + e)
}
Expand Down
6 changes: 4 additions & 2 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const BleDidcomm = NativeModules.BleDidcomm
}
)

// Functions without any params needs an empty object to work, might be fixable by tweaking the Objective-C interface
// For now we just add an empty object to functions without any params.
type Sdk = {
startPeripheral({}: Record<string, never>): Promise<void>
startCentral({}: Record<string, never>): Promise<void>
Expand All @@ -32,8 +34,8 @@ type Sdk = {
characteristicUUID: string,
notifyCharacteristicUUID: string
): Promise<void>
shutdownCentral(): Promise<void>
shutdownPeripheral(): Promise<void>
shutdownCentral({}: Record<string, never>): Promise<void>
shutdownPeripheral({}: Record<string, never>): Promise<void>
scan({}: Record<never, never>): Promise<void>
stopScan(): void
advertise({}: Record<never, never>): Promise<void>
Expand Down

0 comments on commit 95f5369

Please sign in to comment.