From 4c8de4d06fcb265e09d787821d74272c7f4aaa73 Mon Sep 17 00:00:00 2001 From: MaximBelov Date: Fri, 3 Sep 2021 15:28:50 +0300 Subject: [PATCH] feat(bluetooth-classic-serial): add plugin (#3729) --- .../bluetooth-classic-serial-port/index.ts | 338 ++++++++++++++++++ 1 file changed, 338 insertions(+) create mode 100644 src/@ionic-native/plugins/bluetooth-classic-serial-port/index.ts diff --git a/src/@ionic-native/plugins/bluetooth-classic-serial-port/index.ts b/src/@ionic-native/plugins/bluetooth-classic-serial-port/index.ts new file mode 100644 index 000000000..75099c9c1 --- /dev/null +++ b/src/@ionic-native/plugins/bluetooth-classic-serial-port/index.ts @@ -0,0 +1,338 @@ +import { Injectable } from '@angular/core'; +import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'; +import { Observable } from 'rxjs'; + +export interface BluetoothClassicSerialPortDevice { + id: string; + class?: string | number; + address?: string; + name?: string; + protocols?: string[]; // ios only + [p: string]: any; +} + +/** + * @name Bluetooth Classic Serial Port + * @description This plugin is written using the iOS Accessory Framework (MFi) to support Classic Bluetooth on iOS. + * @usage + * ```typescript + * import { BluetoothClassicSerialPort } from '@ionic-native/bluetooth-classic-serial-port/ngx'; + * + * constructor(private bluetoothClassicSerialPort: BluetoothClassicSerialPort) { } + * + * + * // Write a string + * this.bluetoothClassicSerialPort.write("00001101-0000-1000-8000-00805F9B34FB", "hello, world", success, failure); + * + * // Array of int or bytes + * this.bluetoothClassicSerialPort.write("00001101-0000-1000-8000-00805F9B34FB", [186, 220, 222], success, failure); + * + * // Typed Array + * var data = new Uint8Array(4); + * data[0] = 0x41; + * data[1] = 0x42; + * data[2] = 0x43; + * data[3] = 0x44; + * this.bluetoothClassicSerialPort.write(interfaceId, data, success, failure); + * + * // Array Buffer + * this.bluetoothClassicSerialPort.write(interfaceId, data.buffer, success, failure); + * ``` + * + * // iOS select accessory + * + * ```typescript + * async selectAccessory() { + * const deviceDiscovery = this.bluetoothClassicSerialPort.setDeviceDiscoveredListener().subscribe(async (connectionConfig) => { + * deviceDiscovery.unsubscribe(); + * }) + * + * await this.bluetoothClassicSerialPort.discoverUnpaired().catch(error => { + * deviceDiscovery.unsubscribe(); + * }) + * + * } + * ``` + * + */ +@Plugin({ + pluginName: 'BluetoothClassicSerialPort', + repo: 'https://github.com/MaximBelov/cordova-plugin-bluetooth-classic-serial-port', + plugin: 'cordova-plugin-bluetooth-classic-serial-port', + pluginRef: 'bluetoothClassicSerial', + platforms: ['Android', 'iOS', 'Browser'], +}) +@Injectable() +export class BluetoothClassicSerialPort extends IonicNativePlugin { + /** + * Connect to a Bluetooth device + * @param {string} deviceId Identifier of the remote device. + * @param {string} deviceId this is the MAC address. + * @param {string|string[]} interfaceId Identifier of the remote device + * @param {string|string[]} interfaceId This identifies the serial port to connect to. + * @returns {Observable} Subscribe to connect. + */ + @Cordova({ + platforms: ['Android', 'iOS'], + observable: true, + }) + connect(deviceId: string | number, interfaceId: string | string[]): Observable { + return; + } + + /** + * Connect to a Bluetooth device + * @deprecated + * @param {string} deviceId Identifier of the remote device. + * @param {number} deviceId this is the connection ID + * @param {string|string[]} interfaceArray Identifier of the remote device + * @param {string|string[]} interfaceArray this is the Protocol String + * @returns {Promise} + */ + @Cordova({ + platforms: ['iOS'], + methodName: 'connect', + }) + connectIos(deviceId: string | number, interfaceArray: string | string[]): Promise { + return; + } + + /** + * Connect insecurely to a Bluetooth device + * @param {string} deviceId Identifier of the remote device. For Android this is the MAC address + * @param {string | string[]} interfaceArray This identifies the serial port to connect to. For Android this is the SPP_UUID. + * @returns {Promise} Subscribe to connect. + */ + @Cordova({ + platforms: ['Android'], + observable: true, + }) + connectInsecure(deviceId: string, interfaceArray: string | string[]): Promise { + return; + } + + /** + * Disconnect from the connected device + * @param {string} interfaceId The interface to Disconnect + * @returns {Promise} + */ + @Cordova() + disconnect(interfaceId: string | string[]): Promise { + return; + } + + /** + * Disconnect from all the connected device + * @returns {Promise} + */ + @Cordova({ + methodName: 'connect', + }) + disconnectAll(): Promise { + return; + } + + /** + * Writes data to the serial port + * @param {string} interfaceId The interface to send the data to + * @param {ArrayBuffer | string | number[] | Uint8Array} data ArrayBuffer of data + * @returns {Promise} returns a promise when data has been written + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + write(interfaceId: string, data: ArrayBuffer | string | number[] | Uint8Array): Promise { + return; + } + + /** + * Gets the number of bytes of data available + * @param {string} interfaceId The interface to check + * @returns {Promise} returns a promise that contains the available bytes + */ + @Cordova({ + platforms: ['Android', 'Browser'], + }) + available(interfaceId: string): Promise { + return; + } + + /** + * Function read reads the data from the buffer. The data is passed to the success callback as a String. Calling read when no data is available will pass an empty String to the callback. + * @param {string} interfaceId The interface to read + * @returns {Promise} returns a promise with data from the buffer + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + read(interfaceId: string): Promise { + return; + } + + /** + * Reads data from the buffer until it reaches a delimiter + * @param {string} interfaceId The interface to read + * @param {string} delimiter string that you want to search until + * @returns {Observable} returns a promise + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + readUntil(interfaceId: string, delimiter: string): Observable { + return; + } + + /** + * Subscribe to be notified when data is received + * @param {string | string[]} interfaceId The interface to subscribe to + * @param {string} delimiter the string you want to watch for + * @returns {Observable} returns an observable. + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + observable: true, + }) + subscribe(interfaceId: string | string[], delimiter: string): Observable { + return; + } + + /** + * Unsubscribe from a subscription + * @param {string | string[]} interfaceId The interface to unsubscribe from + * @returns {Promise} returns an promise. + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + unsubscribe(interfaceId: string | string[]): Promise { + return; + } + + /** + * Subscribe to be notified when data is received + * @param {string | string[]} interfaceId The interface to subscribe to + * @returns {Observable} returns an observable + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + observable: true, + }) + subscribeRawData(interfaceId: string | string[]): Observable { + return; + } + + /** + * Unsubscribe from a subscription + * @param {string | string[]} interfaceId The interface to unsubscribe from + * @returns {Promise} returns an promise. + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + unsubscribeRawData(interfaceId: string | string[]): Promise { + return; + } + + /** + * Clears data in buffer + * @param {string} interfaceId The interface to clear data + * @returns {Promise} returns a promise when completed + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + clear(interfaceId: string): Promise<[]> { + return; + } + + /** + * Lists bonded devices + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + list(): Promise { + return; + } + + /** + * Reports the connection status + * @param {string} interfaceId The interface to check + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + isConnected(interfaceId: string): Promise { + return; + } + + /** + * Reports if bluetooth is enabled + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + isEnabled(): Promise { + return; + } + + /** + * Show the Bluetooth settings on the device + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android'], + }) + showBluetoothSettings(): Promise { + return; + } + + /** + * Enable Bluetooth on the device + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android', 'Browser'], + }) + enable(): Promise { + return; + } + + /** + * Discover unpaired devices + * @returns {Promise} returns a promise + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + }) + discoverUnpaired(): Promise { + return; + } + + /** + * Subscribe to be notified on Bluetooth device discovery. Discovery process must be initiated with the `discoverUnpaired` function. + * @returns {Observable} Returns an observable + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + observable: true, + clearFunction: 'clearDeviceDiscoveredListener', + }) + setDeviceDiscoveredListener(): Observable { + return; + } + + /** + * Clears notify callback function registered with setDeviceDiscoveredListener. + */ + @Cordova({ + platforms: ['Android', 'iOS', 'Browser'], + sync: true, + }) + clearDeviceDiscoveredListener() { + return; + } +}