Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #575

Merged
merged 3 commits into from
Oct 3, 2023
Merged

Next #575

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,6 @@ If the app needs to use Bluetooth while it is in the background, you also have t

On Android, no further steps are required to use the plugin (if you are using Capacitor 2, see [here](https://github.com/capacitor-community/bluetooth-le/blob/0.x/README.md#android)).

#### Check if location is enabled globally

Initialize requests the location permission but when it is not enabled globally it will not return any devices and throws no error. Just check if the location is enabled and open the settings when not.

``` typescript
async initialize() {
// Check if location is enabled
if (this.platform.is('android')) {
const isLocationEnabled = await BleClient.isLocationEnabled();
if (!isLocationEnabled) {
await BleClient.openLocationSettings();
}
}
await BleClient.initialize({androidNeverForLocation: true});
}
```

#### (Optional) Android 12 Bluetooth permissions

If your app targets Android 12 (API level 31) or higher and your app doesn't use Bluetooth scan results to derive physical location information, you can strongly assert that your app doesn't derive physical location. This allows the app to scan for Bluetooth devices without asking for location permissions. See the [Android documentation](https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#declare-android12-or-higher).
Expand Down Expand Up @@ -224,6 +207,8 @@ import { BleClient } from '@capacitor-community/bluetooth-le';
import { BluetoothLe } from '@capacitor-community/bluetooth-le';
```

### Heart rate monitor

Here is an example of how to use the plugin. It shows how to read the heart rate from a BLE heart rate monitor such as the Polar H10.

```typescript
Expand Down Expand Up @@ -296,7 +281,7 @@ function parseHeartRate(value: DataView): number {
}
```

An example of using the scanning API:
### Scanning API

```typescript
import { BleClient, numberToUUID } from '@capacitor-community/bluetooth-le';
Expand Down Expand Up @@ -326,6 +311,23 @@ export async function scan(): Promise<void> {
}
```

### Check if location is enabled

On Android, the `initialize` call requests the location permission. However, if location services are disable on the OS level, the app will not find any devices. You can check if the location is enabled and open the settings when not.

```typescript
async function initialize() {
// Check if location is enabled
if (this.platform.is('android')) {
const isLocationEnabled = await BleClient.isLocationEnabled();
if (!isLocationEnabled) {
await BleClient.openLocationSettings();
}
}
await BleClient.initialize();
}
```

## Example Applications

- [BLE Tester](https://github.com/sourcya/ble-tester) (Ionic/React)
Expand Down Expand Up @@ -625,15 +627,16 @@ Connect to a peripheral BLE device. For an example, see [usage](#usage).
### createBond(...)

```typescript
createBond(deviceId: string) => Promise<void>
createBond(deviceId: string, options?: TimeoutOptions | undefined) => Promise<void>
```

Create a bond with a peripheral BLE device.
Only available on **Android**. On iOS bonding is handled by the OS.

| Param | Type | Description |
| -------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| Param | Type | Description |
| -------------- | --------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| **`options`** | <code><a href="#timeoutoptions">TimeoutOptions</a></code> | Options for plugin call |

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ class BluetoothLe : Plugin() {
fun createBond(call: PluginCall) {
val device = getOrCreateDevice(call) ?: return
val timeout = call.getFloat("timeout", DEFAULT_TIMEOUT)!!.toLong()

device.createBond(timeout) { response ->
run {
if (response.success) {
Expand Down
10 changes: 0 additions & 10 deletions ios/PluginTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import Capacitor

class PluginTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testEcho() {
// This is an example of a functional test case for a plugin.
// Use XCTAssert and related functions to verify your tests produce the correct results.
Expand Down
Loading