@sfourdrinier/react-native-ble-plx
Version:
React Native Bluetooth Low Energy library
519 lines (511 loc) • 23.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BleIOSErrorCode = exports.BleErrorCodeMessage = exports.BleErrorCode = exports.BleError = exports.BleAndroidErrorCode = exports.BleATTErrorCode = void 0;
exports.parseBleError = parseBleError;
var _Utils = require("./Utils");
/**
* BleError is an error class which is guaranteed to be thrown by all functions of this
* library. It contains additional properties which help to identify problems in
* platform independent way.
*/
class BleError extends Error {
/**
* Platform independent error code. Possible values are defined in {@link BleErrorCode}.
*/
/**
* Platform independent error code related to ATT errors.
*/
/**
* iOS specific error code (if not an ATT error).
*/
/**
* Android specific error code (if not an ATT error).
*/
/**
* Platform specific error message.
*/
constructor(nativeBleError, errorMessageMapping) {
super();
this.message = errorMessageMapping[BleErrorCode.UnknownError];
if (typeof nativeBleError === 'string') {
this.errorCode = BleErrorCode.UnknownError;
this.attErrorCode = null;
this.iosErrorCode = null;
this.androidErrorCode = null;
this.reason = nativeBleError;
} else {
const message = errorMessageMapping[nativeBleError.errorCode];
if (message) {
this.message = (0, _Utils.fillStringWithArguments)(message, nativeBleError);
}
this.errorCode = nativeBleError.errorCode;
this.attErrorCode = nativeBleError.attErrorCode;
this.iosErrorCode = nativeBleError.iosErrorCode;
this.androidErrorCode = nativeBleError.androidErrorCode;
this.reason = nativeBleError.reason;
}
this.name = 'BleError';
}
}
exports.BleError = BleError;
function parseBleError(errorMessage, errorMessageMapping) {
let bleError;
const errorMapping = errorMessageMapping ? errorMessageMapping : BleErrorCodeMessage;
try {
const nativeBleError = JSON.parse(errorMessage);
bleError = new BleError(nativeBleError, errorMapping);
} catch (_parseError) {
bleError = new BleError(errorMessage, errorMapping);
}
return bleError;
}
/**
* Platform independent error code map adjusted to this library's use cases.
*/
let BleErrorCode = exports.BleErrorCode = /*#__PURE__*/function (BleErrorCode) {
// Implementation specific errors ------------------------------------------------------------------------------------
/**
* This error can be thrown when unexpected error occurred and in most cases it is related to implementation bug.
* Original message is available in {@link #bleerrorreason|reason} property.
*/
BleErrorCode[BleErrorCode["UnknownError"] = 0] = "UnknownError";
/**
* Current promise failed to finish due to BleManager shutdown. It means that user called
* {@link #blemanagerdestroy|manager.destroy()} function before all operations completed.
*/
BleErrorCode[BleErrorCode["BluetoothManagerDestroyed"] = 1] = "BluetoothManagerDestroyed";
/**
* Promise was explicitly cancelled by a user with {@link #blemanagercanceltransaction|manager.cancelTransaction()}
* function call.
*/
BleErrorCode[BleErrorCode["OperationCancelled"] = 2] = "OperationCancelled";
/**
* Operation timed out and was cancelled.
*/
BleErrorCode[BleErrorCode["OperationTimedOut"] = 3] = "OperationTimedOut";
/**
* Native module couldn't start operation due to internal state, which doesn't allow to do that.
*/
BleErrorCode[BleErrorCode["OperationStartFailed"] = 4] = "OperationStartFailed";
/**
* Invalid UUIDs or IDs were passed to API call.
*/
BleErrorCode[BleErrorCode["InvalidIdentifiers"] = 5] = "InvalidIdentifiers";
// Bluetooth global states -------------------------------------------------------------------------------------------
/**
* Bluetooth is not supported for this particular device. It may happen on iOS simulator for example.
*/
BleErrorCode[BleErrorCode["BluetoothUnsupported"] = 100] = "BluetoothUnsupported";
/**
* There are no granted permissions which allow to use BLE functionality. On Android it may require
* android.permission.ACCESS_COARSE_LOCATION permission or android.permission.ACCESS_FINE_LOCATION permission.
*/
BleErrorCode[BleErrorCode["BluetoothUnauthorized"] = 101] = "BluetoothUnauthorized";
/**
* BLE is powered off on device. All previous operations and their state is invalidated.
*/
BleErrorCode[BleErrorCode["BluetoothPoweredOff"] = 102] = "BluetoothPoweredOff";
/**
* BLE stack is in unspecified state.
*/
BleErrorCode[BleErrorCode["BluetoothInUnknownState"] = 103] = "BluetoothInUnknownState";
/**
* BLE stack is resetting.
*/
BleErrorCode[BleErrorCode["BluetoothResetting"] = 104] = "BluetoothResetting";
/**
* BLE state change failed.
*/
BleErrorCode[BleErrorCode["BluetoothStateChangeFailed"] = 105] = "BluetoothStateChangeFailed";
// Peripheral errors. ------------------------------------------------------------------------------------------------
/**
* Couldn't connect to specified device.
*/
BleErrorCode[BleErrorCode["DeviceConnectionFailed"] = 200] = "DeviceConnectionFailed";
/**
* Device was disconnected.
*/
BleErrorCode[BleErrorCode["DeviceDisconnected"] = 201] = "DeviceDisconnected";
/**
* Couldn't read RSSI from device.
*/
BleErrorCode[BleErrorCode["DeviceRSSIReadFailed"] = 202] = "DeviceRSSIReadFailed";
/**
* Device is already connected. It is not allowed to connect twice to the same device.
*/
BleErrorCode[BleErrorCode["DeviceAlreadyConnected"] = 203] = "DeviceAlreadyConnected";
/**
* Couldn't find device with specified ID.
*/
BleErrorCode[BleErrorCode["DeviceNotFound"] = 204] = "DeviceNotFound";
/**
* Operation failed because device has to be connected to perform it.
*/
BleErrorCode[BleErrorCode["DeviceNotConnected"] = 205] = "DeviceNotConnected";
/**
* Device could not change MTU value.
*/
BleErrorCode[BleErrorCode["DeviceMTUChangeFailed"] = 206] = "DeviceMTUChangeFailed";
// Services ----------------------------------------------------------------------------------------------------------
/**
* Couldn't discover services for specified device.
*/
BleErrorCode[BleErrorCode["ServicesDiscoveryFailed"] = 300] = "ServicesDiscoveryFailed";
/**
* Couldn't discover included services for specified service.
*/
BleErrorCode[BleErrorCode["IncludedServicesDiscoveryFailed"] = 301] = "IncludedServicesDiscoveryFailed";
/**
* Service with specified ID or UUID couldn't be found. User may need to call
* {@link #blemanagerdiscoverallservicesandcharacteristicsfordevice|manager.discoverAllServicesAndCharacteristicsForDevice}
* to cache them.
*/
BleErrorCode[BleErrorCode["ServiceNotFound"] = 302] = "ServiceNotFound";
/**
* Services were not discovered. User may need to call
* {@link #blemanagerdiscoverallservicesandcharacteristicsfordevice|manager.discoverAllServicesAndCharacteristicsForDevice}
* to cache them.
*/
BleErrorCode[BleErrorCode["ServicesNotDiscovered"] = 303] = "ServicesNotDiscovered";
// Characteristics ---------------------------------------------------------------------------------------------------
/**
* Couldn't discover characteristics for specified service.
*/
BleErrorCode[BleErrorCode["CharacteristicsDiscoveryFailed"] = 400] = "CharacteristicsDiscoveryFailed";
/**
* Couldn't write to specified characteristic. Make sure that
* {@link #characteristiciswritablewithresponse|characteristic.isWritableWithResponse}
* or {@link #characteristiciswritablewithoutresponse|characteristic.isWritableWithoutResponse} is set to true.
*/
BleErrorCode[BleErrorCode["CharacteristicWriteFailed"] = 401] = "CharacteristicWriteFailed";
/**
* Couldn't read from specified characteristic. Make sure that
* {@link #characteristicisreadable|characteristic.isReadable} is set to true.
*/
BleErrorCode[BleErrorCode["CharacteristicReadFailed"] = 402] = "CharacteristicReadFailed";
/**
* Couldn't set notification or indication for specified characteristic. Make sure that
* {@link #characteristicisnotifiable|characteristic.isNotifiable} or
* {@link #characteristicisindicatable|characteristic.isIndicatable} is set to true.
*/
BleErrorCode[BleErrorCode["CharacteristicNotifyChangeFailed"] = 403] = "CharacteristicNotifyChangeFailed";
/**
* Characteristic with specified ID or UUID couldn't be found. User may need to call
* {@link #blemanagerdiscoverallservicesandcharacteristicsfordevice|manager.discoverAllServicesAndCharacteristicsForDevice}
* to cache them.
*/
BleErrorCode[BleErrorCode["CharacteristicNotFound"] = 404] = "CharacteristicNotFound";
/**
* Characteristic were not discovered for specified service. User may need to call
* {@link #blemanagerdiscoverallservicesandcharacteristicsfordevice|manager.discoverAllServicesAndCharacteristicsForDevice}
* to cache them.
*/
BleErrorCode[BleErrorCode["CharacteristicsNotDiscovered"] = 405] = "CharacteristicsNotDiscovered";
/**
* Invalid Base64 format was passed to characteristic API function call.
*/
BleErrorCode[BleErrorCode["CharacteristicInvalidDataFormat"] = 406] = "CharacteristicInvalidDataFormat";
// Characteristics ---------------------------------------------------------------------------------------------------
/**
* Couldn't discover descriptor for specified characteristic.
*/
BleErrorCode[BleErrorCode["DescriptorsDiscoveryFailed"] = 500] = "DescriptorsDiscoveryFailed";
/**
* Couldn't write to specified descriptor.
*/
BleErrorCode[BleErrorCode["DescriptorWriteFailed"] = 501] = "DescriptorWriteFailed";
/**
* Couldn't read from specified descriptor.
*/
BleErrorCode[BleErrorCode["DescriptorReadFailed"] = 502] = "DescriptorReadFailed";
/**
* Couldn't find specified descriptor.
*/
BleErrorCode[BleErrorCode["DescriptorNotFound"] = 503] = "DescriptorNotFound";
/**
* Descriptors are not discovered for specified characteristic.
*/
BleErrorCode[BleErrorCode["DescriptorsNotDiscovered"] = 504] = "DescriptorsNotDiscovered";
/**
* Invalid Base64 format was passed to descriptor API function call.
*/
BleErrorCode[BleErrorCode["DescriptorInvalidDataFormat"] = 505] = "DescriptorInvalidDataFormat";
/**
* Issued a write to a descriptor, which is handled by OS.
*/
BleErrorCode[BleErrorCode["DescriptorWriteNotAllowed"] = 506] = "DescriptorWriteNotAllowed";
// Scanning errors ---------------------------------------------------------------------------------------------------
/**
* Cannot start scanning operation.
*/
BleErrorCode[BleErrorCode["ScanStartFailed"] = 600] = "ScanStartFailed";
/**
* Location services are disabled.
*/
BleErrorCode[BleErrorCode["LocationServicesDisabled"] = 601] = "LocationServicesDisabled";
return BleErrorCode;
}({});
/**
* Mapping of error codes to error messages
* @name BleErrorCodeMessage
*/
const BleErrorCodeMessage = exports.BleErrorCodeMessage = {
// Implementation specific errors
[]: 'Unknown error occurred. This is probably a bug! Check reason property.',
[]: 'BleManager was destroyed',
[]: 'Operation was cancelled',
[]: 'Operation timed out',
[]: 'Operation was rejected',
[]: 'Invalid UUIDs or IDs were passed: {internalMessage}',
// Bluetooth global states
[]: 'BluetoothLE is unsupported on this device',
[]: 'Device is not authorized to use BluetoothLE',
[]: 'BluetoothLE is powered off',
[]: 'BluetoothLE is in unknown state',
[]: 'BluetoothLE is resetting',
[]: 'Bluetooth state change failed',
// Peripheral errors.
[]: 'Device {deviceID} connection failed',
[]: 'Device {deviceID} was disconnected',
[]: 'RSSI read failed for device {deviceID}',
[]: 'Device {deviceID} is already connected',
[]: 'Device {deviceID} not found',
[]: 'Device {deviceID} is not connected',
[]: 'Device {deviceID} could not change MTU size',
// Services
[]: 'Services discovery failed for device {deviceID}',
[]: 'Included services discovery failed for device {deviceID} and service: {serviceUUID}',
[]: 'Service {serviceUUID} for device {deviceID} not found',
[]: 'Services not discovered for device {deviceID}',
// Characteristics
[]: 'Characteristic discovery failed for device {deviceID} and service {serviceUUID}',
[]: 'Characteristic {characteristicUUID} write failed for device {deviceID} and service {serviceUUID}',
[]: 'Characteristic {characteristicUUID} read failed for device {deviceID} and service {serviceUUID}',
[]: 'Characteristic {characteristicUUID} notify change failed for device {deviceID} and service {serviceUUID}',
[]: 'Characteristic {characteristicUUID} not found',
[]: 'Characteristics not discovered for device {deviceID} and service {serviceUUID}',
[]: 'Cannot write to characteristic {characteristicUUID} with invalid data format: {internalMessage}',
// Descriptors
[]: 'Descriptor {descriptorUUID} discovery failed for device {deviceID}, service {serviceUUID} and characteristic {characteristicUUID}',
[]: 'Descriptor {descriptorUUID} write failed for device {deviceID}, service {serviceUUID} and characteristic {characteristicUUID}',
[]: 'Descriptor {descriptorUUID} read failed for device {deviceID}, service {serviceUUID} and characteristic {characteristicUUID}',
[]: 'Descriptor {descriptorUUID} not found',
[]: 'Descriptors not discovered for device {deviceID}, service {serviceUUID} and characteristic {characteristicUUID}',
[]: 'Cannot write to descriptor {descriptorUUID} with invalid data format: {internalMessage}',
[]: "Cannot write to descriptor {descriptorUUID}. It's not allowed by iOS and therefore forbidden on Android as well.",
// Scanning
[]: 'Cannot start scanning operation',
[]: 'Location services are disabled'
};
/**
* Error codes for ATT errors.
* @name BleATTErrorCode
*/
let BleATTErrorCode = exports.BleATTErrorCode = /*#__PURE__*/function (BleATTErrorCode) {
/**
* The ATT command or request successfully completed.
*/
BleATTErrorCode[BleATTErrorCode["Success"] = 0] = "Success";
/**
* The attribute handle is invalid on this device.
*/
BleATTErrorCode[BleATTErrorCode["InvalidHandle"] = 1] = "InvalidHandle";
/**
* The attribute’s value cannot be read.
*/
BleATTErrorCode[BleATTErrorCode["ReadNotPermitted"] = 2] = "ReadNotPermitted";
/**
* The attribute’s value cannot be written.
*/
BleATTErrorCode[BleATTErrorCode["WriteNotPermitted"] = 3] = "WriteNotPermitted";
/**
* The attribute Protocol Data Unit (PDU) or “message” is invalid.
*/
BleATTErrorCode[BleATTErrorCode["InvalidPdu"] = 4] = "InvalidPdu";
/**
* The attribute requires authentication before its value can be read or written.
*/
BleATTErrorCode[BleATTErrorCode["InsufficientAuthentication"] = 5] = "InsufficientAuthentication";
/**
* The attribute server does not support the request received by the client.
*/
BleATTErrorCode[BleATTErrorCode["RequestNotSupported"] = 6] = "RequestNotSupported";
/**
* The specified offset value was past the end of the attribute’s value.
*/
BleATTErrorCode[BleATTErrorCode["InvalidOffset"] = 7] = "InvalidOffset";
/**
* The attribute requires authorization before its value can be read or written.
*/
BleATTErrorCode[BleATTErrorCode["InsufficientAuthorization"] = 8] = "InsufficientAuthorization";
/**
* The prepare queue is full, because too many prepare write requests have been queued.
*/
BleATTErrorCode[BleATTErrorCode["PrepareQueueFull"] = 9] = "PrepareQueueFull";
/**
* The attribute is not found within the specified attribute handle range.
*/
BleATTErrorCode[BleATTErrorCode["AttributeNotFound"] = 10] = "AttributeNotFound";
/**
* The attribute cannot be read or written using the ATT read blob request.
*/
BleATTErrorCode[BleATTErrorCode["AttributeNotLong"] = 11] = "AttributeNotLong";
/**
* The encryption key size used for encrypting this link is insufficient.
*/
BleATTErrorCode[BleATTErrorCode["InsufficientEncryptionKeySize"] = 12] = "InsufficientEncryptionKeySize";
/**
* The length of the attribute’s value is invalid for the intended operation.
*/
BleATTErrorCode[BleATTErrorCode["InvalidAttributeValueLength"] = 13] = "InvalidAttributeValueLength";
/**
* The ATT request has encountered an unlikely error and therefore could not be completed.
*/
BleATTErrorCode[BleATTErrorCode["UnlikelyError"] = 14] = "UnlikelyError";
/**
* The attribute requires encryption before its value can be read or written.
*/
BleATTErrorCode[BleATTErrorCode["InsufficientEncryption"] = 15] = "InsufficientEncryption";
/**
* The attribute type is not a supported grouping attribute as defined by a higher-layer specification.
*/
BleATTErrorCode[BleATTErrorCode["UnsupportedGroupType"] = 16] = "UnsupportedGroupType";
/**
* Resources are insufficient to complete the ATT request.
*/
BleATTErrorCode[BleATTErrorCode["InsufficientResources"] = 17] = "InsufficientResources"; // Values 0x012 – 0x7F are reserved for future use.
return BleATTErrorCode;
}({});
/**
* iOS specific error codes.
* @name BleIOSErrorCode
*/
let BleIOSErrorCode = exports.BleIOSErrorCode = /*#__PURE__*/function (BleIOSErrorCode) {
/**
* An unknown error occurred.
*/
BleIOSErrorCode[BleIOSErrorCode["Unknown"] = 0] = "Unknown";
/**
* The specified parameters are invalid.
*/
BleIOSErrorCode[BleIOSErrorCode["InvalidParameters"] = 1] = "InvalidParameters";
/**
* The specified attribute handle is invalid.
*/
BleIOSErrorCode[BleIOSErrorCode["InvalidHandle"] = 2] = "InvalidHandle";
/**
* The device is not currently connected.
*/
BleIOSErrorCode[BleIOSErrorCode["NotConnected"] = 3] = "NotConnected";
/**
* The device has run out of space to complete the intended operation.
*/
BleIOSErrorCode[BleIOSErrorCode["OutOfSpace"] = 4] = "OutOfSpace";
/**
* The operation is canceled.
*/
BleIOSErrorCode[BleIOSErrorCode["OperationCancelled"] = 5] = "OperationCancelled";
/**
* The connection timed out.
*/
BleIOSErrorCode[BleIOSErrorCode["ConnectionTimeout"] = 6] = "ConnectionTimeout";
/**
* The peripheral disconnected.
*/
BleIOSErrorCode[BleIOSErrorCode["PeripheralDisconnected"] = 7] = "PeripheralDisconnected";
/**
* The specified UUID is not permitted.
*/
BleIOSErrorCode[BleIOSErrorCode["UuidNotAllowed"] = 8] = "UuidNotAllowed";
/**
* The peripheral is already advertising.
*/
BleIOSErrorCode[BleIOSErrorCode["AlreadyAdvertising"] = 9] = "AlreadyAdvertising";
/**
* The connection failed.
*/
BleIOSErrorCode[BleIOSErrorCode["ConnectionFailed"] = 10] = "ConnectionFailed";
/**
* The device already has the maximum number of connections.
*/
BleIOSErrorCode[BleIOSErrorCode["ConnectionLimitReached"] = 11] = "ConnectionLimitReached";
/**
* Unknown device.
*/
BleIOSErrorCode[BleIOSErrorCode["UnknownDevice"] = 12] = "UnknownDevice";
return BleIOSErrorCode;
}({});
/**
* Android specific error codes.
* @name BleAndroidErrorCode
*/
let BleAndroidErrorCode = exports.BleAndroidErrorCode = /*#__PURE__*/function (BleAndroidErrorCode) {
/**
* The device has insufficient resources to complete the intended operation.
*/
BleAndroidErrorCode[BleAndroidErrorCode["NoResources"] = 128] = "NoResources";
/**
* Internal error occurred which may happen due to implementation error in BLE stack.
*/
BleAndroidErrorCode[BleAndroidErrorCode["InternalError"] = 129] = "InternalError";
/**
* BLE stack implementation entered illegal state and operation couldn't complete.
*/
BleAndroidErrorCode[BleAndroidErrorCode["WrongState"] = 130] = "WrongState";
/**
* BLE stack didn't allocate sufficient memory buffer for internal caches.
*/
BleAndroidErrorCode[BleAndroidErrorCode["DbFull"] = 131] = "DbFull";
/**
* Maximum number of pending operations was exceeded.
*/
BleAndroidErrorCode[BleAndroidErrorCode["Busy"] = 132] = "Busy";
/**
* Generic BLE stack error.
*/
BleAndroidErrorCode[BleAndroidErrorCode["Error"] = 133] = "Error";
/**
* Command is already queued up in GATT.
*/
BleAndroidErrorCode[BleAndroidErrorCode["CmdStarted"] = 134] = "CmdStarted";
/**
* Illegal parameter was passed to internal BLE stack function.
*/
BleAndroidErrorCode[BleAndroidErrorCode["IllegalParameter"] = 135] = "IllegalParameter";
/**
* Operation is pending.
*/
BleAndroidErrorCode[BleAndroidErrorCode["Pending"] = 136] = "Pending";
/**
* Authorization failed before performing read or write operation.
*/
BleAndroidErrorCode[BleAndroidErrorCode["AuthFail"] = 137] = "AuthFail";
/**
* More cache entries were loaded then expected.
*/
BleAndroidErrorCode[BleAndroidErrorCode["More"] = 138] = "More";
/**
* Invalid configuration
*/
BleAndroidErrorCode[BleAndroidErrorCode["InvalidCfg"] = 139] = "InvalidCfg";
/**
* GATT service already started.
*/
BleAndroidErrorCode[BleAndroidErrorCode["ServiceStarted"] = 140] = "ServiceStarted";
/**
* GATT link is encrypted but prone to man in the middle attacks.
*/
BleAndroidErrorCode[BleAndroidErrorCode["EncrypedNoMitm"] = 141] = "EncrypedNoMitm";
/**
* GATT link is not encrypted.
*/
BleAndroidErrorCode[BleAndroidErrorCode["NotEncrypted"] = 142] = "NotEncrypted";
/**
* ATT command was sent but channel is congested.
*/
BleAndroidErrorCode[BleAndroidErrorCode["Congested"] = 143] = "Congested";
return BleAndroidErrorCode;
}({});
//# sourceMappingURL=BleError.js.map