Skip to content

Commit

Permalink
feat: check if ble is enabled (#78)
Browse files Browse the repository at this point in the history
* feat(ios): is ble enabled check

Signed-off-by: Berend Sliedrecht <[email protected]>

* feat(android): check if ble is enabled

Signed-off-by: Berend Sliedrecht <[email protected]>

---------

Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Aug 11, 2023
1 parent 32cfa04 commit e704368
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.reactnativebledidcomm

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import androidx.annotation.RequiresPermission
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
Expand All @@ -16,11 +19,24 @@ class BleDidcommModule(private val context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
private var centralManager: CentralManager? = null
private var peripheralManager: PeripheralManager? = null
private val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter

override fun getName(): String {
return Constants.TAG
}

@ReactMethod
fun isBleEnabled(
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
promise: Promise,
) {
try {
promise.resolve(bluetoothAdapter.state == BluetoothAdapter.STATE_ON)
} catch (e: Exception) {
promise.reject("error", e)
}
}

@ReactMethod
fun startCentral(
@Suppress("UNUSED_PARAMETER") options: ReadableMap,
Expand Down
5 changes: 5 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'react-native'
import { Central } from './Central'
import { Peripheral } from './Peripheral'
import { isBleEnabled } from '@animo-id/react-native-ble-didcomm'

export const Spacer = () => <View style={{ height: 20, width: 20 }} />

Expand Down Expand Up @@ -37,6 +38,10 @@ export default function App() {
{isCentral ? 'central' : isPeripheral ? 'peripheral' : 'none'}
</Text>
<Spacer />
<Button
title="is ble enabled"
onPress={async () => console.log(await isBleEnabled())}
/>
{Platform.OS === 'android' && (
<>
<Button
Expand Down
5 changes: 5 additions & 0 deletions ios/BleDidcomm.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

@interface RCT_EXTERN_MODULE(BleDidcomm, RCTEventEmitter)

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

RCT_EXTERN_METHOD(startCentral
:(NSDictionary *)options
resolve:(RCTPromiseResolveBlock)resolve
Expand Down
10 changes: 10 additions & 0 deletions ios/BleDidcomm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ class BleDidcomm: React.RCTEventEmitter {
var peripheralManager: PeripheralManager?
var centralManager: CentralManager?

@objc func isBleEnabled(
_: [String: String],
resolve: RCTPromiseResolveBlock,
reject _: RCTPromiseRejectBlock
) {
let cm = CBCentralManager(delegate: nil, queue: .main)
Thread.sleep(forTimeInterval: 0.05)
resolve(cm.state == .poweredOn)
}

@objc func startPeripheral(
_: [String: String],
resolve: RCTPromiseResolveBlock,
Expand Down
11 changes: 11 additions & 0 deletions src/bleStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { sdk } from './register'

export const isBleEnabled = async () => {
try {
return await sdk.isBleEnabled({})
} catch (e) {
throw new Error(
`An error occured while trying to check the ble state: ${e}`
)
}
}
10 changes: 5 additions & 5 deletions src/central/central.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export class Central implements Ble {
try {
await sdk.write(message)
} catch (e) {
throw new Error(`An error occurred while trying to write message` + e)
throw new Error(`An error occurred while trying to write message: ${e}`)
}
}

public async start() {
try {
await sdk.startCentral({})
} catch (e) {
throw new Error('An error occurred during startup: ' + e)
throw new Error(`An error occurred during startup: ${e}`)
}
}

Expand All @@ -29,7 +29,7 @@ export class Central implements Ble {
options.indicationUUID
)
} catch (e) {
throw new Error('An error occurred during startup: ' + e)
throw new Error(`An error occurred during startup: ${e}`)
}
}

Expand All @@ -54,7 +54,7 @@ export class Central implements Ble {
try {
await sdk.scan({})
} catch (e) {
throw new Error('An error occurred while scanning for devices: ' + e)
throw new Error(`An error occurred while scanning for devices: ${e}`)
}
}

Expand All @@ -63,7 +63,7 @@ export class Central implements Ble {
await sdk.connect(peripheralId)
} catch (e) {
throw new Error(
`An error occurred while trying to connect to ${peripheralId}: ` + e
`An error occurred while trying to connect to '${peripheralId}': ${e}`
)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './constants'
export * from './central'
export * from './peripheral'
export * from './ble'
export * from './bleStatus'
1 change: 1 addition & 0 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 = {
isBleEnabled({}: Record<string, never>): Promise<boolean>
startPeripheral({}: Record<string, never>): Promise<void>
startCentral({}: Record<string, never>): Promise<void>
setCentralService(
Expand Down

0 comments on commit e704368

Please sign in to comment.