161847f8eSopenharmony_ci/* 261847f8eSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 461847f8eSopenharmony_ci * you may not use this file except in compliance with the License. 561847f8eSopenharmony_ci * You may obtain a copy of the License at 661847f8eSopenharmony_ci * 761847f8eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 861847f8eSopenharmony_ci * 961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and 1361847f8eSopenharmony_ci * limitations under the License. 1461847f8eSopenharmony_ci */ 1561847f8eSopenharmony_ci 1661847f8eSopenharmony_ci/** 1761847f8eSopenharmony_ci * @file 1861847f8eSopenharmony_ci * @kit ConnectivityKit 1961847f8eSopenharmony_ci */ 2061847f8eSopenharmony_ci 2161847f8eSopenharmony_ciimport type { AsyncCallback, Callback } from './@ohos.base'; 2261847f8eSopenharmony_ci 2361847f8eSopenharmony_ci/** 2461847f8eSopenharmony_ci * Provides methods to operate or manage bluetooth socket connection. 2561847f8eSopenharmony_ci * 2661847f8eSopenharmony_ci * @namespace socket 2761847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 2861847f8eSopenharmony_ci * @since 10 2961847f8eSopenharmony_ci */ 3061847f8eSopenharmony_cideclare namespace socket { 3161847f8eSopenharmony_ci /** 3261847f8eSopenharmony_ci * Creates a Bluetooth server listening socket. 3361847f8eSopenharmony_ci * 3461847f8eSopenharmony_ci * @permission ohos.permission.ACCESS_BLUETOOTH 3561847f8eSopenharmony_ci * @param { string } name - Indicates the service name. 3661847f8eSopenharmony_ci * @param { SppOptions } options - Indicates the listen parameters. 3761847f8eSopenharmony_ci * @param { AsyncCallback<number> } callback - Callback used to return a server socket ID. 3861847f8eSopenharmony_ci * @throws { BusinessError } 201 - Permission denied. 3961847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 4061847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 3. Parameter verification failed. 4161847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 4261847f8eSopenharmony_ci * @throws { BusinessError } 2900001 - Service stopped. 4361847f8eSopenharmony_ci * @throws { BusinessError } 2900003 - Bluetooth disabled. 4461847f8eSopenharmony_ci * @throws { BusinessError } 2900004 - Profile not supported. 4561847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 4661847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 4761847f8eSopenharmony_ci * @since 10 4861847f8eSopenharmony_ci */ 4961847f8eSopenharmony_ci function sppListen(name: string, options: SppOptions, callback: AsyncCallback<number>): void; 5061847f8eSopenharmony_ci 5161847f8eSopenharmony_ci /** 5261847f8eSopenharmony_ci * Waits for a remote device to connect. 5361847f8eSopenharmony_ci * 5461847f8eSopenharmony_ci * @param { number } serverSocket - Indicates the server socket ID, returned by {@link sppListen}. 5561847f8eSopenharmony_ci * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 5661847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 5761847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 3. Parameter verification failed. 5861847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 5961847f8eSopenharmony_ci * @throws { BusinessError } 2900001 - Service stopped. 6061847f8eSopenharmony_ci * @throws { BusinessError } 2900003 - Bluetooth disabled. 6161847f8eSopenharmony_ci * @throws { BusinessError } 2900004 - Profile not supported. 6261847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 6361847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 6461847f8eSopenharmony_ci * @since 10 6561847f8eSopenharmony_ci */ 6661847f8eSopenharmony_ci function sppAccept(serverSocket: number, callback: AsyncCallback<number>): void; 6761847f8eSopenharmony_ci 6861847f8eSopenharmony_ci /** 6961847f8eSopenharmony_ci * Connects to a remote device over the socket. 7061847f8eSopenharmony_ci * 7161847f8eSopenharmony_ci * @permission ohos.permission.ACCESS_BLUETOOTH 7261847f8eSopenharmony_ci * @param { string } deviceId - Indicates device ID. For example, "11:22:33:AA:BB:FF". 7361847f8eSopenharmony_ci * @param { SppOptions } options - Indicates the connect parameters {@link SppOptions}. 7461847f8eSopenharmony_ci * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 7561847f8eSopenharmony_ci * @throws { BusinessError } 201 - Permission denied. 7661847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 7761847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 3. Parameter verification failed. 7861847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 7961847f8eSopenharmony_ci * @throws { BusinessError } 2900001 - Service stopped. 8061847f8eSopenharmony_ci * @throws { BusinessError } 2900003 - Bluetooth disabled. 8161847f8eSopenharmony_ci * @throws { BusinessError } 2900004 - Profile not supported. 8261847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 8361847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 8461847f8eSopenharmony_ci * @since 10 8561847f8eSopenharmony_ci */ 8661847f8eSopenharmony_ci function sppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void; 8761847f8eSopenharmony_ci 8861847f8eSopenharmony_ci /** 8961847f8eSopenharmony_ci * Disables an spp server socket and releases related resources. 9061847f8eSopenharmony_ci * 9161847f8eSopenharmony_ci * @param { number } socket - Indicates the server socket ID, returned by {@link sppListen}. 9261847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 9361847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 9461847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 9561847f8eSopenharmony_ci * @throws { BusinessError } 2900001 - Service stopped. 9661847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 9761847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 9861847f8eSopenharmony_ci * @since 10 9961847f8eSopenharmony_ci */ 10061847f8eSopenharmony_ci function sppCloseServerSocket(socket: number): void; 10161847f8eSopenharmony_ci 10261847f8eSopenharmony_ci /** 10361847f8eSopenharmony_ci * Disables an spp client socket and releases related resources. 10461847f8eSopenharmony_ci * 10561847f8eSopenharmony_ci * @param { number } socket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 10661847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 10761847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 10861847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 10961847f8eSopenharmony_ci * @throws { BusinessError } 2900001 - Service stopped. 11061847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 11161847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 11261847f8eSopenharmony_ci * @since 10 11361847f8eSopenharmony_ci */ 11461847f8eSopenharmony_ci function sppCloseClientSocket(socket: number): void; 11561847f8eSopenharmony_ci 11661847f8eSopenharmony_ci /** 11761847f8eSopenharmony_ci * Write data through the socket. 11861847f8eSopenharmony_ci * 11961847f8eSopenharmony_ci * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 12061847f8eSopenharmony_ci * @param { ArrayBuffer } data - Indicates the data to write. 12161847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 12261847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 12361847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 12461847f8eSopenharmony_ci * @throws { BusinessError } 2901054 - IO error. 12561847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 12661847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 12761847f8eSopenharmony_ci * @since 10 12861847f8eSopenharmony_ci */ 12961847f8eSopenharmony_ci function sppWrite(clientSocket: number, data: ArrayBuffer): void; 13061847f8eSopenharmony_ci 13161847f8eSopenharmony_ci /** 13261847f8eSopenharmony_ci * Subscribe the event reported when data is read from the socket. 13361847f8eSopenharmony_ci * 13461847f8eSopenharmony_ci * @param { 'sppRead' } type - Type of the spp read event to listen for. 13561847f8eSopenharmony_ci * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 13661847f8eSopenharmony_ci * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 13761847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 13861847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 13961847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 14061847f8eSopenharmony_ci * @throws { BusinessError } 2901054 - IO error. 14161847f8eSopenharmony_ci * @throws { BusinessError } 2900099 - Operation failed. 14261847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 14361847f8eSopenharmony_ci * @since 10 14461847f8eSopenharmony_ci */ 14561847f8eSopenharmony_ci function on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void; 14661847f8eSopenharmony_ci 14761847f8eSopenharmony_ci /** 14861847f8eSopenharmony_ci * Unsubscribe the event reported when data is read from the socket. 14961847f8eSopenharmony_ci * 15061847f8eSopenharmony_ci * @param { 'sppRead' } type - Type of the spp read event to listen for. 15161847f8eSopenharmony_ci * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 15261847f8eSopenharmony_ci * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 15361847f8eSopenharmony_ci * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 15461847f8eSopenharmony_ci * <br>2. Incorrect parameter types. 15561847f8eSopenharmony_ci * @throws { BusinessError } 801 - Capability not supported. 15661847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 15761847f8eSopenharmony_ci * @since 10 15861847f8eSopenharmony_ci */ 15961847f8eSopenharmony_ci function off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void; 16061847f8eSopenharmony_ci 16161847f8eSopenharmony_ci /** 16261847f8eSopenharmony_ci * Describes the spp parameters. 16361847f8eSopenharmony_ci * 16461847f8eSopenharmony_ci * @typedef SppOptions 16561847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 16661847f8eSopenharmony_ci * @since 10 16761847f8eSopenharmony_ci */ 16861847f8eSopenharmony_ci interface SppOptions { 16961847f8eSopenharmony_ci /** 17061847f8eSopenharmony_ci * Indicates the UUID in the SDP record. 17161847f8eSopenharmony_ci * 17261847f8eSopenharmony_ci * @type { string } 17361847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 17461847f8eSopenharmony_ci * @since 10 17561847f8eSopenharmony_ci */ 17661847f8eSopenharmony_ci uuid: string; 17761847f8eSopenharmony_ci /** 17861847f8eSopenharmony_ci * Indicates secure channel or not 17961847f8eSopenharmony_ci * 18061847f8eSopenharmony_ci * @type { boolean } 18161847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 18261847f8eSopenharmony_ci * @since 10 18361847f8eSopenharmony_ci */ 18461847f8eSopenharmony_ci secure: boolean; 18561847f8eSopenharmony_ci /** 18661847f8eSopenharmony_ci * Spp link type 18761847f8eSopenharmony_ci * 18861847f8eSopenharmony_ci * @type { SppType } 18961847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 19061847f8eSopenharmony_ci * @since 10 19161847f8eSopenharmony_ci */ 19261847f8eSopenharmony_ci type: SppType; 19361847f8eSopenharmony_ci } 19461847f8eSopenharmony_ci 19561847f8eSopenharmony_ci /** 19661847f8eSopenharmony_ci * The enum of SPP type. 19761847f8eSopenharmony_ci * 19861847f8eSopenharmony_ci * @enum { number } 19961847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 20061847f8eSopenharmony_ci * @since 10 20161847f8eSopenharmony_ci */ 20261847f8eSopenharmony_ci enum SppType { 20361847f8eSopenharmony_ci /** 20461847f8eSopenharmony_ci * RFCOMM 20561847f8eSopenharmony_ci * 20661847f8eSopenharmony_ci * @syscap SystemCapability.Communication.Bluetooth.Core 20761847f8eSopenharmony_ci * @since 10 20861847f8eSopenharmony_ci */ 20961847f8eSopenharmony_ci SPP_RFCOMM 21061847f8eSopenharmony_ci } 21161847f8eSopenharmony_ci} 21261847f8eSopenharmony_ci 21361847f8eSopenharmony_ciexport default socket;