nativescript-bluetooth
Version:
Connect to and interact with Bluetooth LE peripherals
150 lines (149 loc) • 5.78 kB
TypeScript
/// <reference types="node" />
import { AdvertismentData, BluetoothCommon, ConnectOptions, ConnectionState, DisconnectOptions, DiscoverCharacteristicsOptions, DiscoverOptions, DiscoverServicesOptions, MtuOptions, Peripheral, ReadOptions, ReadResult, Service, StartNotifyingOptions, StartScanningOptions, StopNotifyingOptions, WriteOptions } from './bluetooth.common';
import PQueue from 'p-queue';
export declare function getBluetoothInstance(): Bluetooth;
export { AdvertismentData, Peripheral, ReadResult, Service };
export declare enum ScanMode {
LOW_LATENCY = 0,
BALANCED = 1,
LOW_POWER = 2,
OPPORTUNISTIC = 3
}
export declare enum MatchMode {
AGGRESSIVE = 0,
STICKY = 1
}
export declare enum MatchNum {
MAX_ADVERTISEMENT = 0,
FEW_ADVERTISEMENT = 1,
ONE_ADVERTISEMENT = 2
}
export declare enum CallbackType {
ALL_MATCHES = 0,
FIRST_MATCH = 1,
MATCH_LOST = 2
}
export declare enum Phy {
LE_1M = 0,
LE_CODED = 1,
LE_ALL_SUPPORTED = 2
}
export declare function uuidToString(uuid: any): any;
export declare function arrayToNativeByteArray(val: any): any;
export declare function stringToUint8Array(value: any, encoding?: string): Uint8Array;
export declare function valueToByteArray(value: any, encoding?: string): any;
export declare function byteArrayToBuffer(value: any): ArrayBuffer;
export declare function printValueToString(value: number[]): any[];
export declare function stringToUuid(uuidStr: any): java.util.UUID;
export declare type BluetoothGattCallback = new (owner: WeakRef<Bluetooth>) => android.bluetooth.BluetoothGattCallback;
export declare type SubBluetoothGattCallback = Partial<android.bluetooth.BluetoothGattCallback>;
export interface BluetoothGattCallbackWithSubCallback extends android.bluetooth.BluetoothGattCallback {
addSubDelegate(delegate: SubBluetoothGattCallback): any;
removeSubDelegate(delegate: SubBluetoothGattCallback): any;
}
export declare type DisconnectListener = () => void;
export declare class Bluetooth extends BluetoothCommon {
private _adapter;
get adapter(): globalAndroid.bluetooth.BluetoothAdapter;
private _bluetoothManager;
get bluetoothManager(): globalAndroid.bluetooth.BluetoothManager;
_bluetoothGattCallback: BluetoothGattCallbackWithSubCallback;
get bluetoothGattCallback(): BluetoothGattCallbackWithSubCallback;
private scanCallback;
private LeScanCallback;
gattQueue: PQueue;
static readonly android: {
ScanMode: typeof ScanMode;
MatchMode: typeof MatchMode;
MatchNum: typeof MatchNum;
CallbackType: typeof CallbackType;
};
/**
* Connections are stored as key-val pairs of UUID-Connection.
* So something like this:
* [{
* 34343-2434-5454: {
* state: 'connected',
* discoveredState: '',
* operationConnect: someCallbackFunction
* },
* 1323213-21321323: {
* ..
* }
* }, ..]
*/
connections: {
[k: string]: {
state: ConnectionState;
onConnected?: (e: {
UUID: string;
name: string;
state: string;
services?: Service[];
advertismentData: AdvertismentData;
}) => void;
onDisconnected?: (e: {
UUID: string;
name: string;
}) => void;
device?: android.bluetooth.BluetoothGatt;
onNotifyCallbacks?: {
[k: string]: (result: ReadResult) => void;
};
advertismentData?: AdvertismentData;
};
};
constructor();
clear(): void;
broadcastRegistered: boolean;
registerBroadcast(): void;
unregisterBroadcast(): void;
onListenerAdded(eventName: string, count: number): void;
onListenerRemoved(eventName: string, count: number): void;
stop(): void;
locationPermissionGranted(): boolean;
hasLocationPermission(): Promise<boolean>;
requestCoarseLocationPermission(callback?: () => void): Promise<boolean>;
getAndroidLocationManager(): android.location.LocationManager;
isGPSEnabled(): any;
enableGPS(): Promise<void>;
enable(): Promise<unknown>;
isBluetoothEnabled(): Promise<boolean>;
isConnected(args: any): Promise<boolean>;
openBluetoothSettings(): Promise<unknown>;
scanningReferTimer: {
timer?: NodeJS.Timeout;
resolve?: Function;
};
private stopCurrentScan;
clearAdvertismentCache(): void;
startScanning(args: StartScanningOptions): Promise<unknown>;
stopScanning(): void;
connect(args: ConnectOptions): Promise<any>;
disconnect(args: DisconnectOptions): Promise<void>;
private addToGatQueue;
private addToQueue;
read(args: ReadOptions): Promise<any>;
requestMtu(args: MtuOptions): Promise<any>;
write(args: WriteOptions): Promise<any>;
writeWithoutResponse(args: WriteOptions): Promise<any>;
startNotifying(args: StartNotifyingOptions): Promise<any>;
stopNotifying(args: StopNotifyingOptions): Promise<any>;
discoverServices(args: DiscoverServicesOptions): Promise<{
services: Service[];
}>;
discoverCharacteristics(args: DiscoverCharacteristicsOptions): Promise<void>;
discoverAll(args: DiscoverOptions): Promise<{
services: Service[];
}>;
private disconnectListeners;
addDisconnectListener(delegate: DisconnectListener): void;
removeDisconnectListener(delegate: DisconnectListener): void;
gattDisconnect(gatt: android.bluetooth.BluetoothGatt): void;
private _findNotifyCharacteristic;
private _findCharacteristicOfType;
private _getWrapper;
private _isEnabled;
private _getContext;
private _getActivity;
}