1e41f4b71Sopenharmony_ci# @ohos.bluetooth.socket (Bluetooth Socket Module)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **socket** module provides APIs for operating and managing Bluetooth sockets.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Modules to Import
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```js
14e41f4b71Sopenharmony_ciimport { socket } from '@kit.ConnectivityKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## socket.sppListen
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cisppListen(name: string, options: SppOptions, callback: AsyncCallback<number>): void
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciCreates a Serial Port Profile (SPP) listening socket for the server. This API uses an asynchronous callback to return the result.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**Parameters**
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| Name     | Type                         | Mandatory  | Description                     |
30e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ----------------------- |
31e41f4b71Sopenharmony_ci| name     | string                      | Yes   | Name of the service.                 |
32e41f4b71Sopenharmony_ci| options   | [SppOptions](#sppoptions)     | Yes   | SPP listening configuration.             |
33e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes   | Callback used to return the server socket ID.|
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Error codes**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| ID| Error Message|
40e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
41e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
42e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
43e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
44e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
45e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
46e41f4b71Sopenharmony_ci|2900004 | Profile not supported.                |
47e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**Example**
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci```js
52e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
53e41f4b71Sopenharmony_cilet serverNumber = -1;
54e41f4b71Sopenharmony_cilet serverSocket = (code: BusinessError, number: number) => {
55e41f4b71Sopenharmony_ci  if (code) {
56e41f4b71Sopenharmony_ci    console.error('sppListen error, code is ' + code);
57e41f4b71Sopenharmony_ci    return;
58e41f4b71Sopenharmony_ci  } else {
59e41f4b71Sopenharmony_ci    serverNumber = number;
60e41f4b71Sopenharmony_ci    console.info('sppListen success, serverNumber = ' + serverNumber);
61e41f4b71Sopenharmony_ci  }
62e41f4b71Sopenharmony_ci}
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_cilet sppOption:socket.SppOptions = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
65e41f4b71Sopenharmony_citry {
66e41f4b71Sopenharmony_ci    socket.sppListen('server1', sppOption, serverSocket);
67e41f4b71Sopenharmony_ci} catch (err) {
68e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
69e41f4b71Sopenharmony_ci}
70e41f4b71Sopenharmony_ci```
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci## socket.sppAccept
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_cisppAccept(serverSocket: number, callback: AsyncCallback<number>): void
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ciAccepts a connection request from the client over a socket of the server. This API uses an asynchronous callback to return the result.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**Parameters**
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci| Name         | Type                         | Mandatory  | Description                     |
84e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | ----------------------- |
85e41f4b71Sopenharmony_ci| serverSocket | number                      | Yes   | Server socket ID.          |
86e41f4b71Sopenharmony_ci| callback     | AsyncCallback<number> | Yes   | Callback used to return the client socket ID.|
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Error codes**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| ID| Error Message|
93e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
94e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
95e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
96e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
97e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
98e41f4b71Sopenharmony_ci|2900004 | Profile not supported.                |
99e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**Example**
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci```js
104e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
105e41f4b71Sopenharmony_cilet clientNumber = -1;
106e41f4b71Sopenharmony_cilet serverNumber = -1;
107e41f4b71Sopenharmony_cilet acceptClientSocket = (code: BusinessError, number: number) => {
108e41f4b71Sopenharmony_ci  console.info('bluetooth error code: ' + code.code);
109e41f4b71Sopenharmony_ci  if (code) {
110e41f4b71Sopenharmony_ci    console.error('sppListen error, code is ' + code);
111e41f4b71Sopenharmony_ci    return;
112e41f4b71Sopenharmony_ci  } else {
113e41f4b71Sopenharmony_ci    clientNumber = number; // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
114e41f4b71Sopenharmony_ci    console.info('sppListen success, serverNumber = ' + clientNumber);
115e41f4b71Sopenharmony_ci  }
116e41f4b71Sopenharmony_ci}
117e41f4b71Sopenharmony_citry {
118e41f4b71Sopenharmony_ci    socket.sppAccept(serverNumber, acceptClientSocket); // serverNumber is the server number returned by the sppListen callback.
119e41f4b71Sopenharmony_ci} catch (err) {
120e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
121e41f4b71Sopenharmony_ci}
122e41f4b71Sopenharmony_ci```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci## socket.sppConnect
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_cisppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ciInitiates an SPP connection to a remote device from the client. This API uses an asynchronous callback to return the result.
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci**Parameters**
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci| Name     | Type                         | Mandatory  | Description                            |
138e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------ |
139e41f4b71Sopenharmony_ci| deviceId | string                      | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
140e41f4b71Sopenharmony_ci| options   | [SppOptions](#sppoptions)     | Yes   | SPP listening configuration for the connection.                 |
141e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes   | Callback used to return the client socket ID.       |
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci**Error codes**
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci| ID| Error Message|
148e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
149e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
150e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
151e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
152e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
153e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
154e41f4b71Sopenharmony_ci|2900004 | Profile not supported.                |
155e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci**Example**
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci```js
160e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_cilet clientNumber = -1;
163e41f4b71Sopenharmony_cilet clientSocket = (code: BusinessError, number: number) => {
164e41f4b71Sopenharmony_ci  if (code) {
165e41f4b71Sopenharmony_ci    console.error('sppListen error, code is ' + code);
166e41f4b71Sopenharmony_ci    return;
167e41f4b71Sopenharmony_ci  } else {
168e41f4b71Sopenharmony_ci    console.info('bluetooth serverSocket Number: ' + number);
169e41f4b71Sopenharmony_ci    // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
170e41f4b71Sopenharmony_ci    clientNumber = number;
171e41f4b71Sopenharmony_ci  }
172e41f4b71Sopenharmony_ci}
173e41f4b71Sopenharmony_cilet sppOption:socket.SppOptions = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
174e41f4b71Sopenharmony_citry {
175e41f4b71Sopenharmony_ci    socket.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
176e41f4b71Sopenharmony_ci} catch (err) {
177e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
178e41f4b71Sopenharmony_ci}
179e41f4b71Sopenharmony_ci```
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci## socket.sppCloseServerSocket
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_cisppCloseServerSocket(socket: number): void
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ciCloses an SPP listening socket of the server.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci**Parameters**
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci| Name   | Type    | Mandatory  | Description             |
193e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- |
194e41f4b71Sopenharmony_ci| socket | number | Yes   | Server socket ID, which is obtained by **sppListen()**.|
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci**Error codes**
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci| ID| Error Message|
201e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
202e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.             |
203e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
204e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
205e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci**Example**
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci```js
210e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
211e41f4b71Sopenharmony_cilet serverNumber = -1; // Set serverNumber to the value of serverNumber returned by the sppListen callback.
212e41f4b71Sopenharmony_citry {
213e41f4b71Sopenharmony_ci    socket.sppCloseServerSocket(serverNumber);
214e41f4b71Sopenharmony_ci} catch (err) {
215e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
216e41f4b71Sopenharmony_ci}
217e41f4b71Sopenharmony_ci```
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci## socket.sppCloseClientSocket
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_cisppCloseClientSocket(socket: number): void
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ciCloses an SPP listening socket of the client.
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**Parameters**
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci| Name   | Type    | Mandatory  | Description      |
231e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------- |
232e41f4b71Sopenharmony_ci| socket | number | Yes   | Client socket ID, which is obtained by **sppAccept()** or **sppConnect()**.|
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci**Error codes**
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci| ID| Error Message|
239e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
240e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.              |
241e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
242e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
243e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci**Example**
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci```js
248e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
249e41f4b71Sopenharmony_cilet clientNumber = -1; // clientNumber is obtained by sppAccept or sppConnect.
250e41f4b71Sopenharmony_citry {
251e41f4b71Sopenharmony_ci    socket.sppCloseClientSocket(clientNumber);
252e41f4b71Sopenharmony_ci} catch (err) {
253e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
254e41f4b71Sopenharmony_ci}
255e41f4b71Sopenharmony_ci```
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci## socket.sppWrite
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_cisppWrite(clientSocket: number, data: ArrayBuffer): void
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ciWrites data to the remote device through a socket.
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci**Parameters**
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci| Name         | Type         | Mandatory  | Description           |
269e41f4b71Sopenharmony_ci| ------------ | ----------- | ---- | ------------- |
270e41f4b71Sopenharmony_ci| clientSocket | number      | Yes   | Client socket ID, which is obtained by **sppAccept()** or **sppConnect()**.|
271e41f4b71Sopenharmony_ci| data         | ArrayBuffer | Yes   | Data to write.       |
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci**Error codes**
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci| ID| Error Message|
278e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
279e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.             |
280e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
281e41f4b71Sopenharmony_ci|2901054 | IO error.                                |
282e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**Example**
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci```js
287e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
288e41f4b71Sopenharmony_cilet clientNumber = -1; // clientNumber is obtained by sppAccept or sppConnect.
289e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8);
290e41f4b71Sopenharmony_cilet data = new Uint8Array(arrayBuffer);
291e41f4b71Sopenharmony_cidata[0] = 123;
292e41f4b71Sopenharmony_citry {
293e41f4b71Sopenharmony_ci    socket.sppWrite(clientNumber, arrayBuffer);
294e41f4b71Sopenharmony_ci} catch (err) {
295e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
296e41f4b71Sopenharmony_ci}
297e41f4b71Sopenharmony_ci```
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci## socket.on('sppRead')
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_cion(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ciSubscribes to the SPP read request events. This API uses an asynchronous callback to return the result.
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci**Parameters**
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci| Name         | Type                         | Mandatory  | Description                        |
311e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | -------------------------- |
312e41f4b71Sopenharmony_ci| type         | string                      | Yes   | Event type. The value is **sppRead**, which indicates an SPP read request event.|
313e41f4b71Sopenharmony_ci| clientSocket | number                      | Yes   | Client socket ID, which is obtained by **sppAccept()** or **sppConnect()**.             |
314e41f4b71Sopenharmony_ci| callback     | Callback<ArrayBuffer> | Yes   | Callback used to return the data read.         |
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci**Error codes**
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci| ID| Error Message|
321e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
322e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.            |
323e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
324e41f4b71Sopenharmony_ci|2901054 | IO error.                                |
325e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci**Example**
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci```js
330e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
331e41f4b71Sopenharmony_cilet clientNumber = -1; // clientNumber is obtained by sppAccept or sppConnect.
332e41f4b71Sopenharmony_cilet dataRead = (dataBuffer: ArrayBuffer) => {
333e41f4b71Sopenharmony_ci    let data = new Uint8Array(dataBuffer);
334e41f4b71Sopenharmony_ci    console.info('bluetooth data is: ' + data[0]);
335e41f4b71Sopenharmony_ci}
336e41f4b71Sopenharmony_citry {
337e41f4b71Sopenharmony_ci    socket.on('sppRead', clientNumber, dataRead);
338e41f4b71Sopenharmony_ci} catch (err) {
339e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
340e41f4b71Sopenharmony_ci}
341e41f4b71Sopenharmony_ci```
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci## socket.off('sppRead')
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_cioff(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_ciUnsubscribes from SPP read request events.
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci**Parameters**
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_ci| Name         | Type                         | Mandatory  | Description                                      |
355e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | ---------------------------------------- |
356e41f4b71Sopenharmony_ci| type         | string                      | Yes   | Event type. The value is **sppRead**, which indicates an SPP read request event.              |
357e41f4b71Sopenharmony_ci| clientSocket | number                      | Yes   | Client socket ID, which is obtained by **sppAccept()** or **sppConnect()**.                       |
358e41f4b71Sopenharmony_ci| callback     | Callback<ArrayBuffer> | No   | Callback to unregister. If this parameter is not set, this API unregisters all callbacks for the specified **type**.|
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci**Error codes**
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci| ID| Error Message|
365e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
366e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.              |
367e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**Example**
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci```js
372e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
373e41f4b71Sopenharmony_cilet clientNumber = -1; // clientNumber is obtained by sppAccept or sppConnect.
374e41f4b71Sopenharmony_citry {
375e41f4b71Sopenharmony_ci    socket.off('sppRead', clientNumber);
376e41f4b71Sopenharmony_ci} catch (err) {
377e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
378e41f4b71Sopenharmony_ci}
379e41f4b71Sopenharmony_ci```
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci## SppOptions
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ciDefines the SPP configuration.
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci| Name    | Type               | Readable  | Writable  | Description         |
389e41f4b71Sopenharmony_ci| ------ | ------------------- | ---- | ---- | ----------- |
390e41f4b71Sopenharmony_ci| uuid   | string              | Yes   | Yes   | UUID of the SPP.|
391e41f4b71Sopenharmony_ci| secure | boolean             | Yes   | Yes   | Whether it is a secure channel. The value **true** indicates a secure channel, and the value **false** indicates a non-secure channel.   |
392e41f4b71Sopenharmony_ci| type   | [SppType](#spptype)            | Yes   | Yes   | Type of the SPP link.   |
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci## SppType
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ciEnumerates the SPP link types.
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci| Name        | Value | Description           |
402e41f4b71Sopenharmony_ci| ---------- | ---- | ------------- |
403e41f4b71Sopenharmony_ci| SPP_RFCOMM | 0    | Radio frequency communication (RFCOMM) link.|
404