diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml deleted file mode 100644 index 5f747fe..0000000 --- a/.github/workflows/cr.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Code Review - -permissions: - contents: read - pull-requests: write - -on: - workflow_dispatch: - pull_request: - types: [opened, reopened, synchronize] - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: anc95/ChatGPT-CodeReview@main - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - # Optional - OPENAI_API_ENDPOINT: https://api.openai.com/v1 - MODEL: gpt-3.5-turbo-1106 # https://platform.openai.com/docs/models - PROMPT: "Please check if there are any bugs, confusions, or irregularities in the following code diff: " - top_p: 1 # https://platform.openai.com/docs/api-reference/chat/create#chat/create-top_p - temperature: 1 # https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature - max_tokens: 4096 - MAX_PATCH_LENGTH: 10000 # if the patch/diff length is large than MAX_PATCH_LENGTH, will be ignored and won't review. By default, with no MAX_PATCH_LENGTH set, there is also no limit for the patch/diff length. \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 72809ed..7d0ef55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ -## 0.8.2 +## 0.8.3 +* Rename onPairStateChange -> onPairingStateChange +* Improve readme -* Improve readme. +## 0.8.2 +* Improve readme ## 0.8.1 - -* Update supported platforms. +* Update supported platforms ## 0.8.0 - -* Initial release. +* Initial release diff --git a/README.md b/README.md index 36b54ac..2a8840d 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ # Universal BLE +[![universal_ble version](https://img.shields.io/pub/v/universal_ble?label=universal_ble)](https://pub.dev/packages/universal_ble) + A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE) plugin for Flutter ## Features -- [Scanning for BLE Peripherals](#scanning-for-ble-peripherals) -- [Connecting to BLE Peripheral](#connecting-to-ble-peripheral) -- [Discovering Services of BLE Peripheral](#discovering-services-of-ble-peripheral) -- [Transferring Data between BLE Central & Peripheral](#transferring-data-between-ble-central--peripheral) -- [Pairing BLE Peripheral](#pairing-ble-central--peripheral) -- [Receiving BLE Availability Changes](#receiving-ble-availability-changes) +- [Scanning](#scanning) +- [Connecting](#connecting) +- [Discovering Services](#discovering-services) +- [Reading & Writing data](#reading--writing-data) +- [Pairing](#pairing) +- [Bluetooth Availability](#bluetooth-availability) ### API Support Matrix @@ -23,14 +25,25 @@ A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE | writeValue | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | | setNotifiable | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | | pair/unPair | ✔️ | ❌ | ❌ | ✔️ | ✔️ | ❌ | -| onPairStateChange | ✔️ | ❌ | ❌ | ✔️ | ✔️ | ❌ | +| onPairingStateChange | ✔️ | ❌ | ❌ | ✔️ | ✔️ | ❌ | | enableBluetooth | ✔️ | ❌ | ❌ | ✔️ | ✔️ | ❌ | | onAvailabilityChange | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | -| requestMtu | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | 🚧 | +| requestMtu | ✔️ | ✔️ | ✔️ | ✔️ | 🚧 | ❌ | ## Getting Started -### Scanning for BLE Peripherals +Add universal_ble in your pubspec.yaml: +```yaml +dependencies: + universal_ble: +``` + +and import it wherever you want to use it: +```dart +import 'package:universal_ble/universal_ble.dart'; +``` + +### Scanning ```dart // Set a scan result handler @@ -41,33 +54,26 @@ UniversalBle.onScanResult = (scanResult) { // Perform a scan UniversalBle.startScan(); -// On web, you can add filters and specify optional services to discover after connection. The parameter is ignored on other platforms. -UniversalBle.startScan( - webRequestOptions: WebRequestOptionsBuilder.acceptAllDevices( - optionalServices: ["SERVICE_UUID"], - ), -); - // Stop scanning UniversalBle.stopScan(); ``` -Already connected devices won't show up as scan results. -You can list the already connected devices using `getConnectedDevices()`. You still need to explicitly connect before using them. +Already connected devices, either through previous sessions or connected through system settings, won't show up as scan results. +You can list those devices using `getConnectedDevices()`. You still need to explicitly connect before using them. ```dart // You can set `withServices` to narrow down the results await UniversalBle.getConnectedDevices(withServices: []); ``` -### Connecting to BLE Peripheral +### Connecting ```dart -// Connect to a peripheral using the `deviceId` of the scanResult received from `UniversalBle.onScanResult` +// Connect to a device using the `deviceId` of the scanResult received from `UniversalBle.onScanResult` String deviceId = scanResult.deviceId; UniversalBle.connect(deviceId); -// Disconnect from a peripheral +// Disconnect from a device UniversalBle.disconnect(deviceId); // Get notified for connection state changes @@ -76,14 +82,14 @@ UniversalBle.onConnectionChanged = (String deviceId, BleConnectionState state) { } ``` -### Discovering Services of BLE Peripheral +### Discovering Services ```dart -// Discover services of a specific `deviceId` +// Discover services of a specific device UniversalBle.discoverServices(deviceId); ``` -### Transferring Data between BLE Central & Peripheral +### Reading & Writing data ```dart // Read data from a characteristic @@ -104,37 +110,37 @@ UniversalBle.onValueChanged = (String deviceId, String characteristicId, Uint8Li UniversalBle.setNotifiable(deviceId, serviceId, characteristicId, BleInputProperty.disabled); ``` -### Pairing BLE Central & Peripheral +### Pairing ```dart // Pair UniversalBle.pair(deviceId); // Get the pairing result -UniversalBle.onPairStateChange = (String deviceId, bool isPaired, String? error) { +UniversalBle.onPairingStateChange = (String deviceId, bool isPaired, String? error) { // Handle Pairing state change } // Unpair UniversalBle.unPair(deviceId); -// Check current pairing status +// Check current pairing state bool isPaired = UniversalBle.isPaired(deviceId); ``` -### Enable Bluetooth Programmatically +### Bluetooth Availability ```dart -UniversalBle.enableBluetooth(); -``` +// Get current Bluetooth availability state +AvailabilityState availabilityState = UniversalBle.getBluetoothAvailabilityState(); // e.g. poweredOff or poweredOn, -### Receiving BLE Availability Changes - -```dart +// Receive Bluetooth availability changes UniversalBle.onAvailabilityChange = (state) { - // Handle BLE availability states - // e.g. poweredOff or poweredOn, + // Handle the new Bluetooth availability state }; + +// Enable Bluetooth programmatically +UniversalBle.enableBluetooth(); ``` ## Platform-Specific Setup @@ -164,6 +170,18 @@ Add `NSBluetoothPeripheralUsageDescription` and `NSBluetoothAlwaysUsageDescripti Add the `Bluetooth` capability to the macOS app from Xcode. +### Web + +On web, you have to add filters and specify optional services when scanning for devices. The parameter is ignored on other platforms. + +```dart +UniversalBle.startScan( + webRequestOptions: WebRequestOptionsBuilder.acceptAllDevices( + optionalServices: ["SERVICE_UUID"], + ), +); +``` + ## Customizing Platform Implementation of UniversalBle ```dart diff --git a/example/lib/peripheral_detail_page.dart b/example/lib/peripheral_detail_page.dart index c258749..ed720e9 100644 --- a/example/lib/peripheral_detail_page.dart +++ b/example/lib/peripheral_detail_page.dart @@ -39,9 +39,9 @@ class _PeripheralDetailPageState extends State { super.initState(); UniversalBle.onConnectionChanged = _handleConnectionChange; UniversalBle.onValueChanged = _handleValueChange; - UniversalBle.onPairStateChange = + UniversalBle.onPairingStateChange = (String deviceId, bool isPaired, String? error) { - print('OnPairStateChange $deviceId, $isPaired'); + print('OnPairingStateChange $deviceId, $isPaired'); setState(() { if (error != null) { results.add("PairStateChangeError (Paired: $isPaired): $error "); diff --git a/example/pubspec.lock b/example/pubspec.lock index a3068f8..d80ddb8 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -378,7 +378,7 @@ packages: path: ".." relative: true source: path - version: "0.0.1" + version: "0.8.3" vector_math: dependency: transitive description: diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index 67d7a81..fa3da86 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -154,8 +154,8 @@ class UniversalBle { _platform.onValueChanged = onValueChanged; /// To get pair state changes, - static set onPairStateChange(OnPairStateChange pairStateChange) => - _platform.onPairStateChange = pairStateChange; + static set onPairingStateChange(OnPairingStateChange pairingStateChange) => + _platform.onPairingStateChange = pairingStateChange; static UniversalBlePlatform _defaultPlatform() { if (kIsWeb) return UniversalBleWeb.instance; @@ -175,5 +175,5 @@ typedef OnScanResult = void Function(BleScanResult scanResult); typedef OnAvailabilityChange = void Function(AvailabilityState state); -typedef OnPairStateChange = void Function( +typedef OnPairingStateChange = void Function( String deviceId, bool isPaired, String? error); diff --git a/lib/src/universal_ble_linux/universal_ble_linux.dart b/lib/src/universal_ble_linux/universal_ble_linux.dart index 71dc363..a908ba8 100644 --- a/lib/src/universal_ble_linux/universal_ble_linux.dart +++ b/lib/src/universal_ble_linux/universal_ble_linux.dart @@ -314,7 +314,7 @@ class UniversalBleLinux extends UniversalBlePlatform { onScanResult?.call(device.toBleScanResult()); break; case BluezProperty.paired: - onPairStateChange?.call(device.address, device.paired, null); + onPairingStateChange?.call(device.address, device.paired, null); break; case BluezProperty.legacyPairing: case BluezProperty.servicesResolved: diff --git a/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart b/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart index 9343d68..7601709 100644 --- a/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart +++ b/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart @@ -122,7 +122,7 @@ class UniversalBlePigeonChannel extends UniversalBlePlatform { (String deviceId, String characteristicId, Uint8List value) => onValueChanged?.call(deviceId, characteristicId, value), pairStateChange: (String deviceId, bool isPaired, String? error) => - onPairStateChange?.call(deviceId, isPaired, error), + onPairingStateChange?.call(deviceId, isPaired, error), )); } } @@ -149,7 +149,7 @@ class _UniversalBleCallbackHandler extends UniversalBleCallbackChannel { OnScanResult scanResult; OnConnectionChanged connectionChanged; OnValueChanged valueChanged; - OnPairStateChange pairStateChange; + OnPairingStateChange pairStateChange; _UniversalBleCallbackHandler({ required this.availabilityChange, diff --git a/lib/src/universal_ble_platform_interface.dart b/lib/src/universal_ble_platform_interface.dart index e8542fc..ee5c051 100644 --- a/lib/src/universal_ble_platform_interface.dart +++ b/lib/src/universal_ble_platform_interface.dart @@ -48,5 +48,5 @@ abstract class UniversalBlePlatform { OnScanResult? onScanResult; OnConnectionChanged? onConnectionChanged; OnValueChanged? onValueChanged; - OnPairStateChange? onPairStateChange; + OnPairingStateChange? onPairingStateChange; } diff --git a/pubspec.yaml b/pubspec.yaml index 2d7a0c9..4bf416b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: universal_ble description: A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE) plugin for Flutter -version: 0.8.2 +version: 0.8.3 homepage: https://navideck.com repository: https://github.com/Navideck/universal_ble issue_tracker: https://github.com/Navideck/universal_ble/issues