react-native-ble-nitro
Version:
High-performance React Native BLE library built on Nitro Modules - drop-in replacement for react-native-ble-plx
193 lines (167 loc) • 7.04 kB
text/typescript
import {
BleErrorCode,
BleATTErrorCode,
BleIOSErrorCode,
BleAndroidErrorCode
} from '../specs/types';
import type {
NativeBleError,
BleErrorCodeMessageMapping
} from '../specs/types';
/**
* Default error messages for BLE error codes
* Maintains compatibility with react-native-ble-plx error messages
*/
const BleErrorCodeMessage: BleErrorCodeMessageMapping = {
[]: 'Unknown error occurred',
[]: 'BLE Manager was destroyed',
[]: 'Operation was cancelled',
[]: 'Operation timed out',
[]: 'Operation could not be started',
[]: 'Invalid identifiers provided',
[]: 'Bluetooth is not supported on this device',
[]: 'App is not authorized to use Bluetooth',
[]: 'Bluetooth is powered off',
[]: 'Bluetooth is in unknown state',
[]: 'Bluetooth is resetting',
[]: 'Bluetooth state change failed',
[]: 'Device connection failed',
[]: 'Device was disconnected',
[]: 'Failed to read RSSI',
[]: 'Device is already connected',
[]: 'Device not found',
[]: 'Device is not connected',
[]: 'Failed to change MTU',
[]: 'Services discovery failed',
[]: 'Included services discovery failed',
[]: 'Service not found',
[]: 'Services not discovered',
[]: 'Characteristics discovery failed',
[]: 'Characteristic write failed',
[]: 'Characteristic read failed',
[]: 'Failed to change characteristic notification state',
[]: 'Characteristic not found',
[]: 'Characteristics not discovered',
[]: 'Invalid characteristic data format',
[]: 'Descriptors discovery failed',
[]: 'Descriptor write failed',
[]: 'Descriptor read failed',
[]: 'Descriptor not found',
[]: 'Descriptors not discovered',
[]: 'Invalid descriptor data format',
[]: 'Descriptor write not allowed',
[]: 'Failed to start scan',
[]: 'Location services are disabled'
};
/**
* BleError class that maintains 100% compatibility with react-native-ble-plx
* Contains additional properties for platform-independent error handling
*/
export class BleError extends Error {
/**
* Platform independent error code
*/
public readonly errorCode: BleErrorCode;
/**
* Platform independent error code related to ATT errors
*/
public readonly attErrorCode: BleATTErrorCode | null;
/**
* iOS specific error code (if not an ATT error)
*/
public readonly iosErrorCode: BleIOSErrorCode | null;
/**
* Android specific error code (if not an ATT error)
*/
public readonly androidErrorCode: BleAndroidErrorCode | null;
/**
* Platform specific error message
*/
public readonly reason: string | null;
/**
* Device ID associated with error (if applicable)
*/
public readonly deviceID?: string;
/**
* Service UUID associated with error (if applicable)
*/
public readonly serviceUUID?: string;
/**
* Characteristic UUID associated with error (if applicable)
*/
public readonly characteristicUUID?: string;
/**
* Descriptor UUID associated with error (if applicable)
*/
public readonly descriptorUUID?: string;
/**
* Internal error message for debugging
*/
public readonly internalMessage?: string;
constructor(
nativeBleError: NativeBleError | string,
errorMessageMapping: BleErrorCodeMessageMapping = BleErrorCodeMessage
) {
if (typeof nativeBleError === 'string') {
// Simple string error case
super(nativeBleError);
this.errorCode = BleErrorCode.UnknownError;
this.attErrorCode = null;
this.iosErrorCode = null;
this.androidErrorCode = null;
this.reason = nativeBleError;
return;
}
// Native BLE error case
const errorMessage = errorMessageMapping[nativeBleError.errorCode] || 'Unknown BLE error';
super(errorMessage);
this.errorCode = nativeBleError.errorCode;
this.attErrorCode = nativeBleError.attErrorCode;
this.iosErrorCode = nativeBleError.iosErrorCode;
this.androidErrorCode = nativeBleError.androidErrorCode;
this.reason = nativeBleError.reason;
if (nativeBleError.deviceID !== undefined) this.deviceID = nativeBleError.deviceID;
if (nativeBleError.serviceUUID !== undefined) this.serviceUUID = nativeBleError.serviceUUID;
if (nativeBleError.characteristicUUID !== undefined) this.characteristicUUID = nativeBleError.characteristicUUID;
if (nativeBleError.descriptorUUID !== undefined) this.descriptorUUID = nativeBleError.descriptorUUID;
if (nativeBleError.internalMessage !== undefined) this.internalMessage = nativeBleError.internalMessage;
// Set proper prototype chain
Object.setPrototypeOf(this, BleError.prototype);
this.name = 'BleError';
}
/**
* Returns a string representation of the error with all relevant information
*/
public toString(): string {
const parts = [
`BleError: ${this.message}`,
`Error code: ${this.errorCode}`
];
if (this.attErrorCode !== null) {
parts.push(`ATT error code: ${this.attErrorCode}`);
}
if (this.iosErrorCode !== null) {
parts.push(`iOS error code: ${this.iosErrorCode}`);
}
if (this.androidErrorCode !== null) {
parts.push(`Android error code: ${this.androidErrorCode}`);
}
if (this.reason) {
parts.push(`Reason: ${this.reason}`);
}
if (this.deviceID) {
parts.push(`Device ID: ${this.deviceID}`);
}
if (this.serviceUUID) {
parts.push(`Service UUID: ${this.serviceUUID}`);
}
if (this.characteristicUUID) {
parts.push(`Characteristic UUID: ${this.characteristicUUID}`);
}
if (this.descriptorUUID) {
parts.push(`Descriptor UUID: ${this.descriptorUUID}`);
}
return parts.join(', ');
}
}
export { BleErrorCodeMessage };