@nativescript-community/nordic-dfu
Version:
A NativeScript plugin for performing Nordic Bluetooth device firmware updates.
201 lines • 8.75 kB
JavaScript
import { File, getFileAccess, knownFolders, Utils } from '@nativescript/core';
import { DfuState, DFUInitiatorCommon, _addExecutingInitiator, _removeExecutingInitiator, DFUControllerInternal } from './common';
import { DFUController } from '../DFUController';
const DFU_TEMP_FILE_NAME = 'dfu-temp.zip';
var DfuProgressListener = (function (_super) {
__extends(DfuProgressListener, _super);
function DfuProgressListener(owner) {
var _this = _super.call(this) || this;
_this.mOwner = new WeakRef(owner);
return global.__native(_this);
}
DfuProgressListener.prototype.onDeviceConnecting = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._notifyDfuStateChanged(DfuState.CONNECTING);
}
};
DfuProgressListener.prototype.onDfuProcessStarting = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._notifyDfuStateChanged(DfuState.DFU_PROCESS_STARTING);
}
};
DfuProgressListener.prototype.onEnablingDfuMode = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._notifyDfuStateChanged(DfuState.ENABLING_DFU_MODE);
}
};
DfuProgressListener.prototype.onFirmwareValidating = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._notifyDfuStateChanged(DfuState.FIRMWARE_VALIDATING);
}
};
DfuProgressListener.prototype.onDeviceDisconnecting = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner._notifyDfuStateChanged(DfuState.DEVICE_DISCONNECTING);
}
};
DfuProgressListener.prototype.onDfuCompleted = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
_removeExecutingInitiator(owner.peripheralUUID);
owner._notifyDfuStateChanged(DfuState.DFU_COMPLETED);
}
setTimeout(function () {
var manager = Utils.android.getApplicationContext().getSystemService(android.content.Context.NOTIFICATION_SERVICE);
manager.cancel(com.nativescript.dfu.DfuService.NOTIFICATION_ID);
}, 200);
};
DfuProgressListener.prototype.onDfuAborted = function (_deviceAddress) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
_removeExecutingInitiator(owner.peripheralUUID);
owner._notifyDfuStateChanged(DfuState.DFU_ABORTED);
}
};
DfuProgressListener.prototype.onProgressChanged = function (_deviceAddress, percent, speed, avgSpeed, currentPart, totalParts) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner.notify({
eventName: DFUInitiator.dfuProgressEvent,
object: owner,
percent: percent,
speed: speed,
avgSpeed: avgSpeed,
currentPart: currentPart,
totalParts: totalParts,
});
}
};
DfuProgressListener.prototype.onError = function (_deviceAddress, _error, _errorType, message) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
_removeExecutingInitiator(owner.peripheralUUID);
owner._notifyDfuStateChanged(DfuState.DFU_FAILED, message);
}
};
return DfuProgressListener;
}(no.nordicsemi.android.dfu.DfuProgressListenerAdapter));
export class DFUInitiator extends DFUInitiatorCommon {
constructor(peripheralUUID) {
super(peripheralUUID);
this.mNative = new no.nordicsemi.android.dfu.DfuServiceInitiator(peripheralUUID).setKeepBond(false);
this.mProgressListener = new DfuProgressListener(this);
no.nordicsemi.android.dfu.DfuServiceListenerHelper.registerProgressListener(Utils.android.getApplicationContext(), this.mProgressListener);
}
setIOSConnectionTimeout(val) {
// Not implemented on android
return this;
}
setAndroidDeviceName(name) {
this.mNative.setDeviceName(name);
return this;
}
setAndroidDisableNotification(val) {
this.mNative.setDisableNotification(val);
return this;
}
setAndroidForeground(val) {
this.mNative.setForeground(val);
return this;
}
setAndroidKeepBond(val) {
this.mNative.setKeepBond(val);
return this;
}
setAndroidScanTimeout(val) {
this.mNative.setScanTimeout(val);
return this;
}
setForceDfu(val) {
this.mNative.setForceDfu(val);
return this;
}
setForceScanningForNewAddressInLegacyDfu(val) {
this.mNative.setForceScanningForNewAddressInLegacyDfu(val);
return this;
}
setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(val) {
this.mNative.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(val);
return this;
}
setPacketReceiptNotificationParameter(val) {
this.mNative.setPacketsReceiptNotificationsEnabled(val > 0);
this.mNative.setPacketsReceiptNotificationsValue(val);
return this;
}
disableResume() {
this.mNative.disableResume();
return this;
}
start(zipFile) {
const appContext = Utils.android.getApplicationContext();
const cleanUpCallback = this._createDFUFileURI(zipFile, appContext, (uri) => {
this.mNative.setZip(uri);
});
if (Utils.SDK_VERSION >= 26) {
no.nordicsemi.android.dfu.DfuServiceInitiator.createDfuNotificationChannel(appContext);
}
const nativeController = this.mNative.start(appContext, com.nativescript.dfu.DfuService.class);
const serviceController = Reflect.construct(DFUControllerInternal, [nativeController], DFUController);
_addExecutingInitiator(this.peripheralUUID, {
object: this,
serviceController,
cleanUpCallback,
});
return serviceController;
}
_createDFUFileURI(zipFile, context, callback) {
if (!zipFile || !context || !callback) {
throw new Error('_createDFUFileURI: Missing DFU parameters for generating file URI');
}
let cleanUpCallback;
if (typeof zipFile === 'string') {
callback(android.net.Uri.parse(zipFile));
cleanUpCallback = () => { };
}
else if (zipFile instanceof ArrayBuffer) {
const cacheFolder = knownFolders.temp();
const filePath = cacheFolder.path + '/' + DFU_TEMP_FILE_NAME;
const errorCallback = (err) => console.error(err);
if (File.exists) {
const file = File.fromPath(filePath);
file.removeSync(errorCallback);
}
const tmpFile = new java.io.File(filePath);
const fos = new java.io.FileOutputStream(tmpFile);
const channel = fos.getChannel();
try {
channel.write(zipFile);
}
catch (err) {
console.error(err);
}
finally {
channel.close();
fos.close();
}
callback(androidx.core.content.FileProvider.getUriForFile(context, context.getPackageName() + '.dfufileprovider', tmpFile));
cleanUpCallback = () => getFileAccess().deleteFile(filePath, errorCallback);
}
else {
throw new Error(`Incorrect zip file given: ${zipFile}`);
}
return cleanUpCallback;
}
}
export { DfuState };
//# sourceMappingURL=index.android.js.map