1/* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * @file 18 * @kit ConnectivityKit 19 */ 20 21import type { AsyncCallback, Callback } from './@ohos.base'; 22 23/** 24 * Provides methods to operate or manage bluetooth socket connection. 25 * 26 * @namespace socket 27 * @syscap SystemCapability.Communication.Bluetooth.Core 28 * @since 10 29 */ 30declare namespace socket { 31 /** 32 * Creates a Bluetooth server listening socket. 33 * 34 * @permission ohos.permission.ACCESS_BLUETOOTH 35 * @param { string } name - Indicates the service name. 36 * @param { SppOptions } options - Indicates the listen parameters. 37 * @param { AsyncCallback<number> } callback - Callback used to return a server socket ID. 38 * @throws { BusinessError } 201 - Permission denied. 39 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 40 * <br>2. Incorrect parameter types. 3. Parameter verification failed. 41 * @throws { BusinessError } 801 - Capability not supported. 42 * @throws { BusinessError } 2900001 - Service stopped. 43 * @throws { BusinessError } 2900003 - Bluetooth disabled. 44 * @throws { BusinessError } 2900004 - Profile not supported. 45 * @throws { BusinessError } 2900099 - Operation failed. 46 * @syscap SystemCapability.Communication.Bluetooth.Core 47 * @since 10 48 */ 49 function sppListen(name: string, options: SppOptions, callback: AsyncCallback<number>): void; 50 51 /** 52 * Get l2cap socket psm. 53 * 54 * @param { number } serverSocket - Indicates the server socket ID, returned by {@link sppListen}. 55 * @returns { number } Returns the l2cap socket psm 56 * @syscap SystemCapability.Communication.Bluetooth.Core 57 * @since 20 58 */ 59 function getL2capPsm(serverSocket: number): number; 60 /** 61 * Waits for a remote device to connect. 62 * 63 * @param { number } serverSocket - Indicates the server socket ID, returned by {@link sppListen}. 64 * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 65 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 66 * <br>2. Incorrect parameter types. 3. Parameter verification failed. 67 * @throws { BusinessError } 801 - Capability not supported. 68 * @throws { BusinessError } 2900001 - Service stopped. 69 * @throws { BusinessError } 2900003 - Bluetooth disabled. 70 * @throws { BusinessError } 2900004 - Profile not supported. 71 * @throws { BusinessError } 2900099 - Operation failed. 72 * @syscap SystemCapability.Communication.Bluetooth.Core 73 * @since 10 74 */ 75 function sppAccept(serverSocket: number, callback: AsyncCallback<number>): void; 76 77 /** 78 * Connects to a remote device over the socket. 79 * 80 * @permission ohos.permission.ACCESS_BLUETOOTH 81 * @param { string } deviceId - Indicates device ID. For example, "11:22:33:AA:BB:FF". 82 * @param { SppOptions } options - Indicates the connect parameters {@link SppOptions}. 83 * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 84 * @throws { BusinessError } 201 - Permission denied. 85 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 86 * <br>2. Incorrect parameter types. 3. Parameter verification failed. 87 * @throws { BusinessError } 801 - Capability not supported. 88 * @throws { BusinessError } 2900001 - Service stopped. 89 * @throws { BusinessError } 2900003 - Bluetooth disabled. 90 * @throws { BusinessError } 2900004 - Profile not supported. 91 * @throws { BusinessError } 2900099 - Operation failed. 92 * @syscap SystemCapability.Communication.Bluetooth.Core 93 * @since 10 94 */ 95 function sppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void; 96 97 /** 98 * Obtain the device id in the client socket. 99 * 100 * @param { number } clientSocket - Indicates client socket. 101 * @returns { string } Returns the connected device id 102 * @throws { BusinessError } 401 - Parameter error. Possible causes: 103 * 1. Mandatory parameters are left unspecified; 104 * 2. Incorrect parameter types; 105 * 3. Parameter verification failed. 106 * @syscap SystemCapability.Communication.Bluetooth.Core 107 * @since 17 108 */ 109 function getDeviceId(clientSocket: number): string; 110 111 /** 112 * Disables an spp server socket and releases related resources. 113 * 114 * @param { number } socket - Indicates the server socket ID, returned by {@link sppListen}. 115 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 116 * <br>2. Incorrect parameter types. 117 * @throws { BusinessError } 801 - Capability not supported. 118 * @throws { BusinessError } 2900001 - Service stopped. 119 * @throws { BusinessError } 2900099 - Operation failed. 120 * @syscap SystemCapability.Communication.Bluetooth.Core 121 * @since 10 122 */ 123 function sppCloseServerSocket(socket: number): void; 124 125 /** 126 * Disables an spp client socket and releases related resources. 127 * 128 * @param { number } socket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 129 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 130 * <br>2. Incorrect parameter types. 131 * @throws { BusinessError } 801 - Capability not supported. 132 * @throws { BusinessError } 2900001 - Service stopped. 133 * @throws { BusinessError } 2900099 - Operation failed. 134 * @syscap SystemCapability.Communication.Bluetooth.Core 135 * @since 10 136 */ 137 function sppCloseClientSocket(socket: number): void; 138 139 /** 140 * Write data through the socket. 141 * 142 * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 143 * @param { ArrayBuffer } data - Indicates the data to write. 144 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 145 * <br>2. Incorrect parameter types. 146 * @throws { BusinessError } 801 - Capability not supported. 147 * @throws { BusinessError } 2901054 - IO error. 148 * @throws { BusinessError } 2900099 - Operation failed. 149 * @syscap SystemCapability.Communication.Bluetooth.Core 150 * @since 10 151 */ 152 function sppWrite(clientSocket: number, data: ArrayBuffer): void; 153 154 /** 155 * Subscribe the event reported when data is read from the socket. 156 * 157 * @param { 'sppRead' } type - Type of the spp read event to listen for. 158 * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 159 * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 160 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 161 * <br>2. Incorrect parameter types. 162 * @throws { BusinessError } 801 - Capability not supported. 163 * @throws { BusinessError } 2901054 - IO error. 164 * @throws { BusinessError } 2900099 - Operation failed. 165 * @syscap SystemCapability.Communication.Bluetooth.Core 166 * @since 10 167 */ 168 function on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void; 169 170 /** 171 * Unsubscribe the event reported when data is read from the socket. 172 * 173 * @param { 'sppRead' } type - Type of the spp read event to listen for. 174 * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 175 * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 176 * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 177 * <br>2. Incorrect parameter types. 178 * @throws { BusinessError } 801 - Capability not supported. 179 * @syscap SystemCapability.Communication.Bluetooth.Core 180 * @since 10 181 */ 182 function off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void; 183 184 /** 185 * Asynchronous interface for writing data to the socket. 186 * 187 * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 188 * @param { ArrayBuffer } data - Indicates the data to write. 189 * @returns { Promise<void> } Returns the promise object. 190 * @throws { BusinessError } 801 - Capability not supported. 191 * @throws { BusinessError } 2901054 - IO error. 192 * @throws { BusinessError } 2900099 - Operation failed. 193 * @syscap SystemCapability.Communication.Bluetooth.Core 194 * @since 18 195 */ 196 function sppWriteAsync(clientSocket: number, data: ArrayBuffer): Promise<void>; 197 198 /** 199 * Asynchronous interface for reading data from the socket. 200 * 201 * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 202 * @returns { Promise<ArrayBuffer> } Returns the promise object, used to get the spp read data. 203 * @throws { BusinessError } 801 - Capability not supported. 204 * @throws { BusinessError } 2901054 - IO error. 205 * @throws { BusinessError } 2900099 - Operation failed. 206 * @syscap SystemCapability.Communication.Bluetooth.Core 207 * @since 18 208 */ 209 function sppReadAsync(clientSocket: number): Promise<ArrayBuffer>; 210 211 /** 212 * Describes the spp parameters. 213 * 214 * @typedef SppOptions 215 * @syscap SystemCapability.Communication.Bluetooth.Core 216 * @since 10 217 */ 218 interface SppOptions { 219 /** 220 * Indicates the UUID in the SDP record. 221 * 222 * @type { string } 223 * @syscap SystemCapability.Communication.Bluetooth.Core 224 * @since 10 225 */ 226 uuid: string; 227 /** 228 * Indicates secure channel or not 229 * 230 * @type { boolean } 231 * @syscap SystemCapability.Communication.Bluetooth.Core 232 * @since 10 233 */ 234 secure: boolean; 235 /** 236 * Spp link type 237 * 238 * @type { SppType } 239 * @syscap SystemCapability.Communication.Bluetooth.Core 240 * @since 10 241 */ 242 type: SppType; 243 /** 244 * l2cap protocol service multiplexer 245 * 246 * @type { ?number } 247 * @syscap SystemCapability.Communication.Bluetooth.Core 248 * @since 20 249 */ 250 psm?: number; 251 } 252 253 /** 254 * The enum of SPP type. 255 * 256 * @enum { number } 257 * @syscap SystemCapability.Communication.Bluetooth.Core 258 * @since 10 259 */ 260 enum SppType { 261 /** 262 * RFCOMM 263 * 264 * @syscap SystemCapability.Communication.Bluetooth.Core 265 * @since 10 266 */ 267 SPP_RFCOMM = 0, 268 /** 269 * L2CAP of the BR type 270 * 271 * @syscap SystemCapability.Communication.Bluetooth.Core 272 * @since 20 273 */ 274 SPP_L2CAP = 1, 275 /** 276 * L2CAP of the BLE type 277 * 278 * @syscap SystemCapability.Communication.Bluetooth.Core 279 * @since 20 280 */ 281 SPP_L2CAP_BLE = 2 282 } 283} 284 285export default socket;