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 16import type { AsyncCallback, Callback } from './@ohos.base'; 17 18/** 19 * Provides methods to operate or manage bluetooth socket connection. 20 * 21 * @namespace socket 22 * @syscap SystemCapability.Communication.Bluetooth.Core 23 * @since 10 24 */ 25declare namespace socket { 26 /** 27 * Creates a Bluetooth server listening socket. 28 * 29 * @permission ohos.permission.ACCESS_BLUETOOTH 30 * @param { string } name - Indicates the service name. 31 * @param { SppOptions } options - Indicates the listen parameters. 32 * @param { AsyncCallback<number> } callback - Callback used to return a server socket ID. 33 * @throws { BusinessError } 201 - Permission denied. 34 * @throws { BusinessError } 401 - Invalid parameter. 35 * @throws { BusinessError } 801 - Capability not supported. 36 * @throws { BusinessError } 2900001 - Service stopped. 37 * @throws { BusinessError } 2900003 - Bluetooth switch is off. 38 * @throws { BusinessError } 2900004 - Profile is not supported. 39 * @throws { BusinessError } 2900099 - Operation failed. 40 * @syscap SystemCapability.Communication.Bluetooth.Core 41 * @since 10 42 */ 43 function sppListen(name: string, options: SppOptions, callback: AsyncCallback<number>): void; 44 45 /** 46 * Waits for a remote device to connect. 47 * 48 * @param { number } serverSocket - Indicates the server socket ID, returned by {@link sppListen}. 49 * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 50 * @throws { BusinessError } 401 - Invalid parameter. 51 * @throws { BusinessError } 801 - Capability not supported. 52 * @throws { BusinessError } 2900001 - Service stopped. 53 * @throws { BusinessError } 2900003 - Bluetooth switch is off. 54 * @throws { BusinessError } 2900004 - Profile is not supported. 55 * @throws { BusinessError } 2900099 - Operation failed. 56 * @syscap SystemCapability.Communication.Bluetooth.Core 57 * @since 10 58 */ 59 function sppAccept(serverSocket: number, callback: AsyncCallback<number>): void; 60 61 /** 62 * Connects to a remote device over the socket. 63 * 64 * @permission ohos.permission.ACCESS_BLUETOOTH 65 * @param { string } deviceId - Indicates device ID. For example, "11:22:33:AA:BB:FF". 66 * @param { SppOptions } options - Indicates the connect parameters {@link SppOptions}. 67 * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID. 68 * @throws { BusinessError } 201 - Permission denied. 69 * @throws { BusinessError } 401 - Invalid parameter. 70 * @throws { BusinessError } 801 - Capability not supported. 71 * @throws { BusinessError } 2900001 - Service stopped. 72 * @throws { BusinessError } 2900003 - Bluetooth switch is off. 73 * @throws { BusinessError } 2900004 - Profile is not supported. 74 * @throws { BusinessError } 2900099 - Operation failed. 75 * @syscap SystemCapability.Communication.Bluetooth.Core 76 * @since 10 77 */ 78 function sppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void; 79 80 /** 81 * Disables an spp server socket and releases related resources. 82 * 83 * @param { number } socket - Indicates the server socket ID, returned by {@link sppListen}. 84 * @throws { BusinessError } 401 - Invalid parameter. 85 * @throws { BusinessError } 801 - Capability not supported. 86 * @throws { BusinessError } 2900001 - Service stopped. 87 * @throws { BusinessError } 2900099 - Operation failed. 88 * @syscap SystemCapability.Communication.Bluetooth.Core 89 * @since 10 90 */ 91 function sppCloseServerSocket(socket: number): void; 92 93 /** 94 * Disables an spp client socket and releases related resources. 95 * 96 * @param { number } socket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 97 * @throws { BusinessError } 401 - Invalid parameter. 98 * @throws { BusinessError } 801 - Capability not supported. 99 * @throws { BusinessError } 2900001 - Service stopped. 100 * @throws { BusinessError } 2900099 - Operation failed. 101 * @syscap SystemCapability.Communication.Bluetooth.Core 102 * @since 10 103 */ 104 function sppCloseClientSocket(socket: number): void; 105 106 /** 107 * Write data through the socket. 108 * 109 * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}. 110 * @param { ArrayBuffer } data - Indicates the data to write. 111 * @throws { BusinessError } 401 - Invalid parameter. 112 * @throws { BusinessError } 801 - Capability not supported. 113 * @throws { BusinessError } 2901054 - IO error. 114 * @throws { BusinessError } 2900099 - Operation failed. 115 * @syscap SystemCapability.Communication.Bluetooth.Core 116 * @since 10 117 */ 118 function sppWrite(clientSocket: number, data: ArrayBuffer): void; 119 120 /** 121 * Subscribe the event reported when data is read from the socket. 122 * 123 * @param { 'sppRead' } type - Type of the spp read event to listen for. 124 * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 125 * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 126 * @throws { BusinessError } 401 - Invalid parameter. 127 * @throws { BusinessError } 801 - Capability not supported. 128 * @throws { BusinessError } 2901054 - IO error. 129 * @throws { BusinessError } 2900099 - Operation failed. 130 * @syscap SystemCapability.Communication.Bluetooth.Core 131 * @since 10 132 */ 133 function on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void; 134 135 /** 136 * Unsubscribe the event reported when data is read from the socket. 137 * 138 * @param { 'sppRead' } type - Type of the spp read event to listen for. 139 * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect. 140 * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event. 141 * @throws { BusinessError } 401 - Invalid parameter. 142 * @throws { BusinessError } 801 - Capability not supported. 143 * @syscap SystemCapability.Communication.Bluetooth.Core 144 * @since 10 145 */ 146 function off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void; 147 148 /** 149 * Describes the spp parameters. 150 * 151 * @typedef SppOptions 152 * @syscap SystemCapability.Communication.Bluetooth.Core 153 * @since 10 154 */ 155 interface SppOptions { 156 /** 157 * Indicates the UUID in the SDP record. 158 * 159 * @syscap SystemCapability.Communication.Bluetooth.Core 160 * @since 10 161 */ 162 uuid: string; 163 /** 164 * Indicates secure channel or not 165 * 166 * @syscap SystemCapability.Communication.Bluetooth.Core 167 * @since 10 168 */ 169 secure: boolean; 170 /** 171 * Spp link type 172 * 173 * @syscap SystemCapability.Communication.Bluetooth.Core 174 * @since 10 175 */ 176 type: SppType; 177 } 178 179 /** 180 * The enum of SPP type. 181 * 182 * @enum { number } 183 * @syscap SystemCapability.Communication.Bluetooth.Core 184 * @since 10 185 */ 186 enum SppType { 187 /** 188 * RFCOMM 189 * 190 * @syscap SystemCapability.Communication.Bluetooth.Core 191 * @since 10 192 */ 193 SPP_RFCOMM 194 } 195} 196 197export default socket;