• 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   * Disables an spp server socket and releases related resources.
90   *
91   * @param { number } socket - Indicates the server socket ID, returned by {@link sppListen}.
92   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
93   * <br>2. Incorrect parameter types.
94   * @throws { BusinessError } 801 - Capability not supported.
95   * @throws { BusinessError } 2900001 - Service stopped.
96   * @throws { BusinessError } 2900099 - Operation failed.
97   * @syscap SystemCapability.Communication.Bluetooth.Core
98   * @since 10
99   */
100  function sppCloseServerSocket(socket: number): void;
101
102  /**
103   * Disables an spp client socket and releases related resources.
104   *
105   * @param { number } socket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
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 sppCloseClientSocket(socket: number): void;
115
116  /**
117   * Write data through the socket.
118   *
119   * @param { number } clientSocket - Indicates the client socket ID, returned by {@link sppAccept} or {@link sppConnect}.
120   * @param { ArrayBuffer } data - Indicates the data to write.
121   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
122   * <br>2. Incorrect parameter types.
123   * @throws { BusinessError } 801 - Capability not supported.
124   * @throws { BusinessError } 2901054 - IO error.
125   * @throws { BusinessError } 2900099 - Operation failed.
126   * @syscap SystemCapability.Communication.Bluetooth.Core
127   * @since 10
128   */
129  function sppWrite(clientSocket: number, data: ArrayBuffer): void;
130
131  /**
132   * Subscribe the event reported when data is read from the socket.
133   *
134   * @param { 'sppRead' } type - Type of the spp read event to listen for.
135   * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect.
136   * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event.
137   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
138   * <br>2. Incorrect parameter types.
139   * @throws { BusinessError } 801 - Capability not supported.
140   * @throws { BusinessError } 2901054 - IO error.
141   * @throws { BusinessError } 2900099 - Operation failed.
142   * @syscap SystemCapability.Communication.Bluetooth.Core
143   * @since 10
144   */
145  function on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void;
146
147  /**
148   * Unsubscribe the event reported when data is read from the socket.
149   *
150   * @param { 'sppRead' } type - Type of the spp read event to listen for.
151   * @param { number } clientSocket - Client socket ID, which is obtained by sppAccept or sppConnect.
152   * @param { Callback<ArrayBuffer> } callback - Callback used to listen for the spp read event.
153   * @throws { BusinessError } 401 - Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified.
154   * <br>2. Incorrect parameter types.
155   * @throws { BusinessError } 801 - Capability not supported.
156   * @syscap SystemCapability.Communication.Bluetooth.Core
157   * @since 10
158   */
159  function off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void;
160
161  /**
162   * Describes the spp parameters.
163   *
164   * @typedef SppOptions
165   * @syscap SystemCapability.Communication.Bluetooth.Core
166   * @since 10
167   */
168  interface SppOptions {
169    /**
170     * Indicates the UUID in the SDP record.
171     *
172     * @type { string }
173     * @syscap SystemCapability.Communication.Bluetooth.Core
174     * @since 10
175     */
176    uuid: string;
177    /**
178     * Indicates secure channel or not
179     *
180     * @type { boolean }
181     * @syscap SystemCapability.Communication.Bluetooth.Core
182     * @since 10
183     */
184    secure: boolean;
185    /**
186     * Spp link type
187     *
188     * @type { SppType }
189     * @syscap SystemCapability.Communication.Bluetooth.Core
190     * @since 10
191     */
192    type: SppType;
193  }
194
195  /**
196   * The enum of SPP type.
197   *
198   * @enum { number }
199   * @syscap SystemCapability.Communication.Bluetooth.Core
200   * @since 10
201   */
202  enum SppType {
203    /**
204     * RFCOMM
205     *
206     * @syscap SystemCapability.Communication.Bluetooth.Core
207     * @since 10
208     */
209    SPP_RFCOMM
210  }
211}
212
213export default socket;