UNPKG

@nativescript-community/nordic-dfu

Version:

A NativeScript plugin for performing Nordic Bluetooth device firmware updates.

157 lines 7.11 kB
import { SDK_VERSION } from '@nativescript/core/utils'; import { DfuState, DFUInitiatorCommon } from './common'; import { Utils } from '@nativescript/core'; import { DfuServiceController } from './serviceController'; // Keep strong references of the initiators while running const executingInitiators = new Map(); var DfuProgressListener = /** @class */ (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) { if (executingInitiators.has(owner.peripheralUUID)) { executingInitiators.delete(owner.peripheralUUID); } owner._notifyDfuStateChanged(DfuState.DFU_COMPLETED); } setTimeout(function () { // if this activity is still open and upload process was completed, cancel the notification 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) { if (executingInitiators.has(owner.peripheralUUID)) { executingInitiators.delete(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) { if (executingInitiators.has(owner.peripheralUUID)) { executingInitiators.delete(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); } 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; } 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(filePath) { const appContext = Utils.android.getApplicationContext(); this.mNative.setZip(android.net.Uri.parse(filePath)); if (SDK_VERSION >= 26) { no.nordicsemi.android.dfu.DfuServiceInitiator.createDfuNotificationChannel(appContext); } const nativeController = this.mNative.start(appContext, com.nativescript.dfu.DfuService.class); executingInitiators.set(this.peripheralUUID, this); return new DfuServiceController(nativeController); } } export { DfuState }; //# sourceMappingURL=index.android.js.map