Skip to content

Commit 86b1c2c

Browse files
authored
Merge pull request #6 from Salt-PepperEngineering/update-xcode
Update Xcode & iOSDFULibrary & Add old PR & README
2 parents c72823d + 4ffc30e commit 86b1c2c

File tree

10 files changed

+910
-1011
lines changed

10 files changed

+910
-1011
lines changed

README.md

Lines changed: 27 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ nrf52 chip from Nordic Semiconductor. It works for both iOS and Android.
55

66
For more info about the DFU process, see: [Resources](#resources)
77

8-
## Installation
8+
This is a fork from the main library!
9+
If need the main documentation you can find it [here](https://github.com/Pilloxa/react-native-nordic-dfu).
10+
This fork contains the latest verisons of `iOSDFULibrary` & `Android-BLE-Library`.
11+
12+
### Installation
913

1014
Install and link the NPM package per usual with
1115

@@ -25,251 +29,58 @@ For React Native below 60.0 version
2529
react-native link react-native-nordic-dfu
2630
```
2731

28-
### Minimum requirements
29-
30-
This project has been verified to work with the following dependencies, though other versions may work as well.
31-
32-
| Dependency | Version |
33-
| ------------ | ------- |
34-
| React Native | 0.59.4 |
35-
| XCode | 10.2 |
36-
| Swift | 5.0 |
37-
| CocoaPods | 1.6.1 |
38-
| Gradle | 5.3.1 |
39-
40-
### iOS
41-
42-
The iOS version of this library has native dependencies that need to be installed via `CocoaPods`, which is currently the only supported method for installing this library. (PR's for alternative installation methods are welcome!)
43-
44-
Previous versions supported manual linking, but this was prone to errors every time a new version of XCode and/or Swift was released, which is why this support was dropped. If you've previously installed this library manually, you'll want to remove the old installation and replace it with CocoaPods.
32+
### Project Setup
4533

46-
#### CocoaPods
34+
Unfortunately, the ios project is written in Objective-C so you will need to use `use_frameworks! :linkage => :static`.
35+
Note: We are considering rewriting the ios module on Swift, but it depends very much on how much free time we have and how much we needed right now.
4736

48-
On your project directory;
49-
50-
```bash
51-
cd ios && pod install
52-
```
53-
54-
If your React Native version below 0.60 or any problem occures on pod command, you can try these steps;
55-
56-
Add the following to your `Podfile`
37+
`Podfile`:
5738

5839
```ruby
5940
target "YourApp" do
6041

6142
...
6243
pod "react-native-nordic-dfu", path: "../node_modules/react-native-nordic-dfu"
6344
...
45+
use_frameworks! :linkage => :static
46+
...
6447

6548
end
6649
```
6750

68-
and in the same folder as the Podfile run
51+
`AppDelegate.mm`:
6952

70-
```bash
71-
pod install
7253
```
73-
74-
Since there's native Swift dependencies you need to set which Swift version your project complies with. If you haven't already done this, open up your project with XCode and add a User-Defined setting under Build Settings: `SWIFT_VERSION = <your-swift-version>`.
75-
76-
If your React Native version is higher than 0.60, probably it's already there.
77-
78-
#### Bluetooth integration
79-
80-
This library needs access to an instance of `CBCentralManager`, which you most likely will have instantiated already if you're using Bluetooth for other purposes than DFU in your project.
81-
82-
To integrate with your existing Bluetooth setup, call `[RNNordicDfu setCentralManagerGetter:<...>]` with a block argument that returns your `CBCentralManager` instance.
83-
84-
If you want control over the `CBCentralManager` instance after the DFU process is done you might need to provide the `onDFUComplete` and `onDFUError` callbacks to transfer back delegate control.
85-
86-
Example code;
87-
88-
```swift
89-
...
9054
...
9155
#import "RNNordicDfu.h"
92-
#import "BleManager.h"
56+
...
9357
9458
@implementation AppDelegate
9559
96-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
60+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
61+
{
9762
...
98-
...
99-
100-
[RNNordicDfu setCentralManagerGetter:^() {
101-
return [BleManager getCentralManager];
102-
}];
103-
104-
// Reset manager delegate since the Nordic DFU lib "steals" control over it
105-
[RNNordicDfu setOnDFUComplete:^() {
106-
NSLog(@"onDFUComplete");
107-
CBCentralManager * manager = [BleManager getCentralManager];
108-
manager.delegate = [BleManager getInstance];
109-
}];
11063
111-
[RNNordicDfu setOnDFUError:^() {
112-
NSLog(@"onDFUError");
113-
CBCentralManager * manager = [BleManager getCentralManager];
114-
manager.delegate = [BleManager getInstance];
115-
}];
64+
[RNNordicDfu setCentralManagerGetter:^() {
65+
return [[CBCentralManager alloc] initWithDelegate:nil queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)];
66+
}];
67+
68+
// Reset manager delegate since the Nordic DFU lib "steals" control over it
69+
[RNNordicDfu setOnDFUComplete:^() {
70+
NSLog(@"onDFUComplete");
71+
}];
72+
[RNNordicDfu setOnDFUError:^() {
73+
NSLog(@"onDFUError");
74+
}];
75+
...
11676
117-
return YES;
11877
}
119-
120-
```
121-
122-
You can find them aslo in example project.
123-
124-
On iOS side this library requires to BleManager module which that [react-native-ble-manager](https://github.com/innoveit/react-native-ble-manager) provides.
125-
126-
It required because;
127-
128-
- You need `BleManager.h` module on AppDelegate file for integration.
129-
- You should call `BleManager.start()` (for once) before the trigger a DFU process on iOS or you will get error like [this issue](https://github.com/Pilloxa/react-native-nordic-dfu/issues/82).
130-
131-
### Android
132-
133-
Android requires that you have `FOREGROUND_SERVICE` permissions.
134-
You will need the following in your AndroidManifest.xml
135-
136-
```
137-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
138-
```
139-
140-
## API
141-
142-
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
143-
144-
### startDFU
145-
146-
Starts the DFU process
147-
148-
Observe: The peripheral must have been discovered by the native BLE side so that the
149-
bluetooth stack knows about it. This library will not do a scan but only
150-
the actual connect and then the transfer. See the example project to see how it can be
151-
done in React Native.
152-
153-
**Parameters**
154-
155-
- `obj` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
156-
- `obj.deviceAddress` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The `identifier`\* of the device that should be updated
157-
- `obj.deviceName` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name of the device in the update notification (optional, default `null`)
158-
- `obj.filePath` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The file system path to the zip-file used for updating
159-
- `obj.alternativeAdvertisingNameEnabled` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Send unique name to device before it is switched into bootloader mode (iOS only) - defaults to `true`
160-
161-
\* `identifier` — MAC address (Android) / UUID (iOS)
162-
163-
**Examples**
164-
165-
```javascript
166-
import { NordicDFU, DFUEmitter } from "react-native-nordic-dfu";
167-
168-
NordicDFU.startDFU({
169-
deviceAddress: "C3:53:C0:39:2F:99",
170-
deviceName: "Pilloxa Pillbox",
171-
filePath: "/data/user/0/com.nordicdfuexample/files/RNFetchBlobTmp4of.zip",
172-
})
173-
.then((res) => console.log("Transfer done:", res))
174-
.catch(console.log);
175-
```
176-
177-
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** A promise that resolves or rejects with the `deviceAddress` in the return value
178-
179-
### DFUEmitter
180-
181-
Event emitter for DFU state and progress events
182-
183-
**Examples**
184-
185-
```javascript
186-
import { NordicDFU, DFUEmitter } from "react-native-nordic-dfu";
187-
188-
DFUEmitter.addListener(
189-
"DFUProgress",
190-
({ percent, currentPart, partsTotal, avgSpeed, speed }) => {
191-
console.log("DFU progress: " + percent + "%");
192-
}
193-
);
194-
195-
DFUEmitter.addListener("DFUStateChanged", ({ state }) => {
196-
console.log("DFU State:", state);
197-
});
198-
```
199-
200-
## Selecting firmware file from local storage
201-
202-
If your user will select the firmware file from local storage you should keep on mind some issues;
203-
204-
You can use [react-native-document-picker](https://github.com/Elyx0/react-native-document-picker) library for file selecting process.
205-
206-
### On iOS
207-
208-
You should select file type as `public.archive` or you will get null type error as like [this issue](https://github.com/Pilloxa/react-native-nordic-dfu/issues/100)
209-
210-
```js
211-
DocumentPicker.pick({ type: "public.archive" });
212-
```
213-
214-
If your device getting disconnect after enable DFU, you should set `false` value to `alternativeAdvertisingNameEnabled` prop while starting DFU.
215-
216-
```js
217-
NordicDFU.startDFU({
218-
deviceAddress: "XXXXXXXX-XXXX-XXXX-XXXX-XX",
219-
filePath: firmwareFile.uri,
220-
alternativeAdvertisingNameEnabled: false,
221-
});
222-
```
223-
224-
### On Android
225-
226-
Some Android versions directly selecting file may can cause errors. If you get any file error you should copy it to your local storage. Like cache directory.
227-
228-
You can use [react-native-fs](https://github.com/itinance/react-native-fs) for copying file.
229-
230-
```js
231-
const firmwareFile = await DocumentPicker.pick({ type: DocumentPicker.types.zip })
232-
const destination = RNFS.CachesDirectoryPath + "/firmwareFile.zip");
233-
234-
await RNFS.copyFile(formatFile.uri, destination);
235-
236-
NordicDFU.startDFU({ deviceAddress: "XX:XX:XX:XX:XX:XX", filePath: destination })
237-
```
238-
239-
If you getting disconnect error sometimes while starting DFU process, you should connect the device before start it.
240-
241-
## Example project
242-
243-
Navigate to `example/` and run
244-
245-
```bash
246-
npm install
24778
```
24879

249-
Run the iOS project with
250-
251-
```bash
252-
react-native run-ios
253-
```
254-
255-
and the Android project with
256-
257-
```bash
258-
react-native run-android
259-
```
260-
261-
## Development
262-
263-
PR's are always welcome!
264-
26580
## Resources
26681

26782
- [DFU Introduction](http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v11.0.0/examples_ble_dfu.html?cp=6_0_0_4_3_1 "BLE Bootloader/DFU")
26883
- [Secure DFU Introduction](http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v12.0.0/ble_sdk_app_dfu_bootloader.html?cp=4_0_0_4_3_1 "BLE Secure DFU Bootloader")
26984
- [How to create init packet](https://github.com/NordicSemiconductor/Android-nRF-Connect/tree/master/init%20packet%20handling "Init packet handling")
27085
- [nRF51 Development Kit (DK)](http://www.nordicsemi.com/eng/Products/nRF51-DK "nRF51 DK") (compatible with Arduino Uno Revision 3)
27186
- [nRF52 Development Kit (DK)](http://www.nordicsemi.com/eng/Products/Bluetooth-Smart-Bluetooth-low-energy/nRF52-DK "nRF52 DK") (compatible with Arduino Uno Revision 3)
272-
273-
## Sponsored by
274-
275-
[![pilloxa](https://pilloxa.com/images/pilloxa-round-logo.svg)](https://pilloxa.com)

android/src/main/java/com/pilloxa/dfu/RNNordicDfuModule.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,36 @@ public RNNordicDfuModule(ReactApplicationContext reactContext) {
3131
}
3232

3333
@ReactMethod
34-
public void startDFU(String address, String name, String filePath, ReadableMap options, Promise promise) {
34+
public void startDFU(String address, String name, String filePath, int packetReceiptNotificationParameter, ReadableMap options, Promise promise) {
3535
mPromise = promise;
3636
final DfuServiceInitiator starter = new DfuServiceInitiator(address)
3737
.setKeepBond(false);
38+
39+
if (options.hasKey("retries")) {
40+
int retries = options.getInt("retries");
41+
starter.setNumberOfRetries(retries);
42+
}
43+
44+
if (options.hasKey("maxMtu")) {
45+
int mtu = options.getInt("maxMtu");
46+
starter.setMtu(mtu);
47+
}
3848
if (name != null) {
3949
starter.setDeviceName(name);
4050
}
41-
starter.setPacketsReceiptNotificationsValue(1);
51+
// mimic behavior of iOSDFULibrary when packetReceiptNotificationParameter is set to `0` - see: https://github.com/NordicSemiconductor/IOS-Pods-DFU-Library/blob/master/iOSDFULibrary/Classes/Implementation/DFUServiceInitiator.swift#L115
52+
if (packetReceiptNotificationParameter > 0) {
53+
starter.setPacketsReceiptNotificationsValue(packetReceiptNotificationParameter);
54+
} else {
55+
starter.setPacketsReceiptNotificationsValue(1);
56+
}
4257
starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true);
43-
starter.setZip(filePath);
58+
if (filePath.endsWith(".bin") || filePath.endsWith(".hex")) {
59+
starter.setBinOrHex(DfuBaseService.TYPE_APPLICATION, filePath).setInitFile(null, null);
60+
} else {
61+
starter.setZip(filePath);
62+
}
63+
4464
final DfuServiceController controller = starter.start(this.reactContext, DfuService.class);
4565
}
4666

@@ -49,6 +69,10 @@ public String getName() {
4969
return name;
5070
}
5171

72+
@ReactMethod
73+
public void removeListeners(int count) {
74+
// Keep: Required for RN built in Event Emitter Calls.
75+
}
5276

5377
private void sendEvent(String eventName, @Nullable WritableMap params) {
5478
getReactApplicationContext()

index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
declare module 'react-native-nordic-dfu' {
1+
declare module "react-native-nordic-dfu" {
22
export class NordicDFU {
33
static startDFU({
44
deviceAddress,
55
deviceName,
66
filePath,
77
alternativeAdvertisingNameEnabled,
8+
packetReceiptNotificationParameter,
89
retries,
9-
maxMtu
10+
maxMtu,
1011
}: {
1112
deviceAddress: string;
1213
deviceName?: string;
1314
filePath: string | null;
1415
alternativeAdvertisingNameEnabled?: boolean;
16+
packetReceiptNotificationParameter?: number;
1517
retries?: number;
1618
maxMtu?: number;
1719
}): Promise<string>;
@@ -28,10 +30,10 @@ declare module 'react-native-nordic-dfu' {
2830

2931
export class DFUEmitter {
3032
static addListener(
31-
name: 'DFUProgress' | 'DFUStateChanged',
33+
name: "DFUProgress" | "DFUStateChanged",
3234
handler: (update: IDfuUpdate) => void
3335
): void;
3436

35-
static removeAllListeners(name: 'DFUProgress' | 'DFUStateChanged'): void;
37+
static removeAllListeners(name: "DFUProgress" | "DFUStateChanged"): void;
3638
}
3739
}

0 commit comments

Comments
 (0)