diff --git a/.gitignore b/.gitignore index 278df9205..8a6a42cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,4 @@ buck-out/ # Bundle artifact *.jsbundle /lib -/test_project \ No newline at end of file +/test_project diff --git a/CHANGELOG.md b/CHANGELOG.md index 69a49e794..efc08127a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +## [3.4.0] - 2024-12-20 + +### Changed + +- internal `_manager` property isn't enumerable anymore. This change will hide it from the `console.log`, `JSON.stringify` and other similar methods. +- `BleManager` is now a singleton. It will be created only once and reused across the app. This change will allow users to declare instance in React tree (hooks and components). This change should not affect the existing codebase, where `BleManager` is created once and used across the app. + +### Fixed + +- Timeout parameter in connect method on Android causing the connection to be closed after the timeout period even if connection was established. +- Missing `serviceUUIDs` data after `discoverAllServicesAndCharacteristics` method call + ## [3.2.1] - 2024-07-9 ### Changed diff --git a/android/src/main/java/com/bleplx/adapter/Device.java b/android/src/main/java/com/bleplx/adapter/Device.java index 178709e03..253042be2 100755 --- a/android/src/main/java/com/bleplx/adapter/Device.java +++ b/android/src/main/java/com/bleplx/adapter/Device.java @@ -3,6 +3,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -61,6 +62,20 @@ public List getServices() { return services; } + @Nullable + public List getServicesUUIDs() { + if (services == null) { + return null; + } + + List servicesUUIDs = new ArrayList<>(); + for (Service service : services) { + servicesUUIDs.add(service.getUuid()); + } + + return servicesUUIDs; + } + public void setServices(@Nullable List services) { this.services = services; } diff --git a/android/src/main/java/com/bleplx/converter/DeviceToJsObjectConverter.java b/android/src/main/java/com/bleplx/converter/DeviceToJsObjectConverter.java index 735be0ec0..1c5acdb61 100644 --- a/android/src/main/java/com/bleplx/converter/DeviceToJsObjectConverter.java +++ b/android/src/main/java/com/bleplx/converter/DeviceToJsObjectConverter.java @@ -1,9 +1,16 @@ package com.bleplx.converter; +import android.util.Log; + import com.bleplx.adapter.Device; +import com.bleplx.utils.ReadableArrayConverter; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.WritableMap; +import java.util.List; +import java.util.UUID; + public class DeviceToJsObjectConverter extends JSObjectConverter { private interface Metadata { @@ -39,10 +46,15 @@ public WritableMap toJSObject(Device value) { result.putNull(Metadata.MTU); } + if(value.getServices() != null) { + result.putArray(Metadata.SERVICE_UUIDS, ReadableArrayConverter.toReadableArray(value.getServicesUUIDs())); + } else { + result.putNull(Metadata.SERVICE_UUIDS); + } + // Advertisement data is not set result.putNull(Metadata.MANUFACTURER_DATA); result.putNull(Metadata.SERVICE_DATA); - result.putNull(Metadata.SERVICE_UUIDS); result.putNull(Metadata.LOCAL_NAME); result.putNull(Metadata.TX_POWER_LEVEL); result.putNull(Metadata.SOLICITED_SERVICE_UUIDS); diff --git a/android/src/main/java/com/bleplx/utils/ReadableArrayConverter.java b/android/src/main/java/com/bleplx/utils/ReadableArrayConverter.java index b6772414b..2aa5987df 100644 --- a/android/src/main/java/com/bleplx/utils/ReadableArrayConverter.java +++ b/android/src/main/java/com/bleplx/utils/ReadableArrayConverter.java @@ -1,6 +1,11 @@ package com.bleplx.utils; +import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.WritableArray; + +import java.util.List; +import java.util.UUID; public class ReadableArrayConverter { public static String[] toStringArray(ReadableArray readableArray) { @@ -10,4 +15,14 @@ public static String[] toStringArray(ReadableArray readableArray) { } return stringArray; } + + public static ReadableArray toReadableArray(List uuids) { + WritableArray array = Arguments.createArray(); + + for (UUID uuid : uuids) { + array.pushString(uuid.toString()); + } + + return array; + } } diff --git a/docs/GETTING_STARTED.md b/docs/GETTING_STARTED.md index 63c0f5538..2d5e4277d 100644 --- a/docs/GETTING_STARTED.md +++ b/docs/GETTING_STARTED.md @@ -2,8 +2,11 @@ react-native-ble-plx -This guide is an introduction to BLE stack and APIs exported by this library. All examples -will be based on CC2541 SensorTag. +This guide is an introduction to BLE stack and APIs exported by this library. For further information you can refer to + +- tutorials and API reference available in this documentation, +- [GitHub wiki](https://github.com/dotintent/react-native-ble-plx/wiki), +- example app available in the repository. ### Install and prepare package @@ -15,7 +18,7 @@ In the case of react native CLI you need to configure two environments: ### Creating BLE Manager -First step is to create BleManager instance which is an entry point to all available APIs. It should be declared **OUTSIDE the life cycle of React**. Make sure to create it after application started its execution. We can keep it as a static reference by either creating our own abstraction (ex.1) or by simply creating a new instance (ex.2). +First step is to create BleManager instance which is an entry point to all available APIs. Make sure to create it after application started its execution. We can keep it as a static reference by either creating our own abstraction (ex.1) or by simply creating a new instance (ex.2). #### Ex.1 @@ -42,9 +45,7 @@ import { BleManager } from 'react-native-ble-plx' export const manager = new BleManager() ``` -Only _one_ instance of BleManager is allowed. When you don't need any BLE functionality you can destroy created instance by calling `manager.destroy()` function. You can then recreate `BleManager` later on. - -Note that you may experience undefined behavior when calling a function on one `BleManager` and continuing with another instance. A frequently made error is to create a new instance of the manager for every re-render of a React Native Component. +When you don't need any BLE functionality you can destroy created instance by calling `manager.destroy()` function. You can then recreate `BleManager` later on. ### Ask for permissions diff --git a/docs/index.html b/docs/index.html index 5135673af..fd09509e7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -2,7 +2,7 @@ - react-native-ble-plx 3.2.1 | Documentation + react-native-ble-plx 3.3.0 | Documentation @@ -15,7 +15,7 @@

react-native-ble-plx

-
3.2.1
+
3.3.0

react-native-ble-plx

-

This guide is an introduction to BLE stack and APIs exported by this library. All examples -will be based on CC2541 SensorTag.

+

This guide is an introduction to BLE stack and APIs exported by this library. For further information you can refer to

+
    +
  • tutorials and API reference available in this documentation,
  • +
  • GitHub wiki,
  • +
  • example app available in the repository.
  • +

Install and prepare package

In the case of Expo, you will need to prepare a plugin config, detailed information can be found here: https://github.com/dotintent/react-native-ble-plx?tab=readme-ov-file#expo-sdk-43 In the case of react native CLI you need to configure two environments:

@@ -1985,13 +1989,13 @@

Install and prepare package

  • Android
  • Creating BLE Manager

    -

    First step is to create BleManager instance which is an entry point to all available APIs. It should be declared OUTSIDE the life cycle of React. Make sure to create it after application started its execution. We can keep it as a static reference by either creating our own abstraction (ex.1) or by simply creating a new instance (ex.2).

    +

    First step is to create BleManager instance which is an entry point to all available APIs. Make sure to create it after application started its execution. We can keep it as a static reference by either creating our own abstraction (ex.1) or by simply creating a new instance (ex.2).

    Ex.1

    import { BleManager } from 'react-native-ble-plx'
     
     // create your own singleton class
     class BLEServiceInstance {
    -  manager: BleManage
    +  manager: BleManager
     
       constructor() {
         this.manager = new BleManager()
    @@ -2003,8 +2007,7 @@ 

    Ex.2

    import { BleManager } from 'react-native-ble-plx'
     
     export const manager = new BleManager()
    -

    Only one instance of BleManager is allowed. When you don't need any BLE functionality you can destroy created instance by calling manager.destroy() function. You can then recreate BleManager later on.

    -

    Note that you may experience undefined behavior when calling a function on one BleManager and continuing with another instance. A frequently made error is to create a new instance of the manager for every re-render of a React Native Component.

    +

    When you don't need any BLE functionality you can destroy created instance by calling manager.destroy() function. You can then recreate BleManager later on.

    Ask for permissions

    Check if you requested following permissions