• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   * Waits for a remote device to connect.
53   *
54   * @param { number } serverSocket - Indicates the server socket ID, returned by {@link sppListen}.
55   * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID.
56   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
57   * <br>2. Incorrect parameter types. 3. Parameter verification failed.
58   * @throws { BusinessError } 801 - Capability not supported.
59   * @throws { BusinessError } 2900001 - Service stopped.
60   * @throws { BusinessError } 2900003 - Bluetooth disabled.
61   * @throws { BusinessError } 2900004 - Profile not supported.
62   * @throws { BusinessError } 2900099 - Operation failed.
63   * @syscap SystemCapability.Communication.Bluetooth.Core
64   * @since 10
65   */
66  function sppAccept(serverSocket: number, callback: AsyncCallback<number>): void;
67
68  /**
69   * Connects to a remote device over the socket.
70   *
71   * @permission ohos.permission.ACCESS_BLUETOOTH
72   * @param { string } deviceId - Indicates device ID. For example, "11:22:33:AA:BB:FF".
73   * @param { SppOptions } options - Indicates the connect parameters {@link SppOptions}.
74   * @param { AsyncCallback<number> } callback - Callback used to return a client socket ID.
75   * @throws { BusinessError } 201 - Permission denied.
76   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
77   * <br>2. Incorrect parameter types. 3. Parameter verification failed.
78   * @throws { BusinessError } 801 - Capability not supported.
79   * @throws { BusinessError } 2900001 - Service stopped.
80   * @throws { BusinessError } 2900003 - Bluetooth disabled.
81   * @throws { BusinessError } 2900004 - Profile not supported.
82   * @throws { BusinessError } 2900099 - Operation failed.
83   * @syscap SystemCapability.Communication.Bluetooth.Core
84   * @since 10
85   */
86  function sppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void;
87
88  /**
89   * Obtain the device id in the client socket.
90   *
91   * @param { number } clientSocket - Indicates client socket.
92   * @returns { string } Returns the connected device id
93   * @throws { BusinessError } 401 - Parameter error. Possible causes:
94   * 1. Mandatory parameters are left unspecified;
95   * 2. Incorrect parameter types;
96   * 3. Parameter verification failed.
97   * @syscap SystemCapability.Communication.Bluetooth.Core
98   * @since 17
99   */
100  function getDeviceId(clientSocket: number): string;
101
102  /**
103   * Disables an spp server socket and releases related resources.
104   *
105   * @param { number } socket - Indicates the server socket ID, returned by {@link sppListen}.
106   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
107   * <br>2. Incorrect parameter types.
108   * @throws { BusinessError } 801 - Capability not supported.
109   * @throws { BusinessError } 2900001 - Service stopped.
110   * @throws { BusinessError } 2900099 - Operation failed.
111   * @syscap SystemCapability.Communication.Bluetooth.Core
112   * @since 10
113   */
114  function sppCloseServerSocket(socket: number): void;
115
116  /**
117   * Disables an spp client socket and releases related resources.
118   *
119   * @param { number } socket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
120   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
121   * <br>2. Incorrect parameter types.
122   * @throws { BusinessError } 801 - Capability not supported.
123   * @throws { BusinessError } 2900001 - Service stopped.
124   * @throws { BusinessError } 2900099 - Operation failed.
125   * @syscap SystemCapability.Communication.Bluetooth.Core
126   * @since 10
127   */
128  function sppCloseClientSocket(socket: number): void;
129
130  /**
131   * Write data through the socket.
132   *
133   * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
134   * @param { ArrayBuffer } data - Indicates the data to write.
135   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
136   * <br>2. Incorrect parameter types.
137   * @throws { BusinessError } 801 - Capability not supported.
138   * @throws { BusinessError } 2901054 - IO error.
139   * @throws { BusinessError } 2900099 - Operation failed.
140   * @syscap SystemCapability.Communication.Bluetooth.Core
141   * @since 10
142   */
143  function sppWrite(clientSocket: number, data: ArrayBuffer): void;
144
145  /**
146   * Subscribe the event reported when data is read from the socket.
147   *
148   * @param { 'sppRead' } type - Type of the spp read event to listen for.
149   * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect.
150   * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event.
151   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
152   * <br>2. Incorrect parameter types.
153   * @throws { BusinessError } 801 - Capability not supported.
154   * @throws { BusinessError } 2901054 - IO error.
155   * @throws { BusinessError } 2900099 - Operation failed.
156   * @syscap SystemCapability.Communication.Bluetooth.Core
157   * @since 10
158   */
159  function on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void;
160
161  /**
162   * Unsubscribe the event reported when data is read from the socket.
163   *
164   * @param { 'sppRead' } type - Type of the spp read event to listen for.
165   * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect.
166   * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event.
167   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
168   * <br>2. Incorrect parameter types.
169   * @throws { BusinessError } 801 - Capability not supported.
170   * @syscap SystemCapability.Communication.Bluetooth.Core
171   * @since 10
172   */
173  function off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void;
174
175  /**
176   * Asynchronous interface for writing data to the socket.
177   *
178   * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
179   * @param { ArrayBuffer } data - Indicates the data to write.
180   * @returns { Promise<void> } Returns the promise object.
181   * @throws { BusinessError } 801 - Capability not supported.
182   * @throws { BusinessError } 2901054 - IO error.
183   * @throws { BusinessError } 2900099 - Operation failed.
184   * @syscap SystemCapability.Communication.Bluetooth.Core
185   * @since 18
186   */
187  function sppWriteAsync(clientSocket: number, data: ArrayBuffer): Promise<void>;
188
189  /**
190   * Asynchronous interface for reading data from the socket.
191   *
192   * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
193   * @returns { Promise<ArrayBuffer> } Returns the promise object, used to get the spp read data.
194   * @throws { BusinessError } 801 - Capability not supported.
195   * @throws { BusinessError } 2901054 - IO error.
196   * @throws { BusinessError } 2900099 - Operation failed.
197   * @syscap SystemCapability.Communication.Bluetooth.Core
198   * @since 18
199   */
200  function sppReadAsync(clientSocket: number): Promise<ArrayBuffer>;
201
202  /**
203   * Describes the spp parameters.
204   *
205   * @typedef SppOptions
206   * @syscap SystemCapability.Communication.Bluetooth.Core
207   * @since 10
208   */
209  interface SppOptions {
210    /**
211     * Indicates the UUID in the SDP record.
212     *
213     * @type { string }
214     * @syscap SystemCapability.Communication.Bluetooth.Core
215     * @since 10
216     */
217    uuid: string;
218    /**
219     * Indicates secure channel or not
220     *
221     * @type { boolean }
222     * @syscap SystemCapability.Communication.Bluetooth.Core
223     * @since 10
224     */
225    secure: boolean;
226    /**
227     * Spp link type
228     *
229     * @type { SppType }
230     * @syscap SystemCapability.Communication.Bluetooth.Core
231     * @since 10
232     */
233    type: SppType;
234  }
235
236  /**
237   * The enum of SPP type.
238   *
239   * @enum { number }
240   * @syscap SystemCapability.Communication.Bluetooth.Core
241   * @since 10
242   */
243  enum SppType {
244    /**
245     * RFCOMM
246     *
247     * @syscap SystemCapability.Communication.Bluetooth.Core
248     * @since 10
249     */
250    SPP_RFCOMM
251  }
252}
253
254export default socket;