1# SPP Development 2 3## Introduction 4Serial Port Profile (SPP) is a Bluetooth protocol used to establish serial communication connections between Bluetooth devices. With SPP, Bluetooth devices can transmit data, such as files and text, just like using a serial port. 5 6## When to Use 7 8You can use the APIs provided by the **spp** module to: 9- Write data to the client. 10- Connect to the peer device over a socket. 11 12## Available APIs 13 14For details about the APIs and sample code, see [@ohos.bluetooth.socket](../../reference/apis-connectivity-kit/js-apis-bluetooth-socket.md). 15 16The following table describes the related APIs. 17 18| API | Description | 19| ---------------------------------- | ------------------------------------------------------------------------------ | 20| sppListen() | Creates an SPP listening socket for the server. | 21| sppAccept() | Accepts a connection request from the client over a socket of the server. | 22| sppConnect() | Initiates an SPP connection to a remote device from the client. | 23| sppCloseServerSocket() | Closes the listening socket of the server. | 24| sppCloseClientSocket() | Closes the client socket. | 25| sppWrite() | Sends data to the remote end over the socket. | 26| on(type: 'sppRead') | Subscribes to the SPP read request events. | 27| off(type: 'sppRead') | Unsubscribes from the SPP read request events. | 28 29## How to Develop 30 31### Writing Data to the Client 321. Import the **socket** module. 332. Check that the SystemCapability.Communication.Bluetooth.Core capability is available. 343. Enable Bluetooth on the device. 354. Creates a server socket. If the operation is successful, **serverId** is returned. 365. Create a communication channel between the server socket and the client socket. If the operation is successful, **clientId** is returned. 376. Write data to the client. 387. (Optional) Subscribe to the data written by the client. 398. Close the server socket. 409. Close the client socket. 41Example: 42 43 ```ts 44 import { socket } from '@kit.ConnectivityKit'; 45 import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 46 47 // Create a server listening socket. serverId is returned if the socket is created. 48 let serverNumber = -1; 49 let sppOption: socket.SppOptions = { 50 uuid: '00001101-0000-1000-8000-00805f9b34fb', 51 secure: true, 52 type: 0 53 }; 54 socket.sppListen('server1', sppOption, (code, serverSocketID) => { 55 if (code != null) { 56 console.error('sppListen error, code is ' + (code as BusinessError).code); 57 return; 58 } else { 59 serverNumber = serverSocketID; 60 console.info('sppListen success, serverNumber = ' + serverNumber); 61 } 62 }); 63 64 // Establish a connection between the server socket and client socket. If the connection is successful, clientId is returned. 65 let clientNumber = -1; 66 socket.sppAccept(serverNumber, (code, clientSocketID) => { 67 if (code != null) { 68 console.error('sppAccept error, code is ' + (code as BusinessError).code); 69 return; 70 } else { 71 clientNumber = clientSocketID; 72 console.info('accept the client success'); 73 } 74 }) 75 console.info('waiting for client connection'); 76 77 // Write data to the client. 78 let array = new Uint8Array(990); 79 array[0] = 'A'.charCodeAt(0); 80 array[1] = 'B'.charCodeAt(0); 81 array[2] = 'C'.charCodeAt(0); 82 array[3] = 'D'.charCodeAt(0); 83 socket.sppWrite(clientNumber, array.buffer); 84 console.info('sppWrite success'); 85 86 // Subscribe to the read request event. 87 socket.on('sppRead', clientNumber, (dataBuffer: ArrayBuffer) => { 88 const data = new Uint8Array(dataBuffer); 89 if (data != null) { 90 console.info('sppRead success, data = ' + JSON.stringify(data)); 91 } else { 92 console.error('sppRead error, data is null'); 93 } 94 }); 95 96 // Unsubscribe from the read request event. 97 socket.off('sppRead', clientNumber, (dataBuffer: ArrayBuffer) => { 98 const data = new Uint8Array(dataBuffer); 99 if (data != null) { 100 console.info('offSppRead success, data = ' + JSON.stringify(data)); 101 } else { 102 console.error('offSppRead error, data is null'); 103 } 104 }); 105 106 // Close the server socket. 107 socket.sppCloseServerSocket(serverNumber); 108 console.info('sppCloseServerSocket success'); 109 110 // Close the client socket. 111 socket.sppCloseClientSocket(clientNumber); 112 console.info('sppCloseClientSocket success'); 113 ``` 114 115For details about the error codes, see [Bluetooth Error Codes](../../reference/apis-connectivity-kit/errorcode-bluetoothManager.md). 116 117### Connecting to the Peer Device over a Socket 1181. Import the **socket** module. 1192. Check that the SystemCapability.Communication.Bluetooth.Core capability is available. 1203. Enable Bluetooth on the device. 1214. Start Bluetooth scanning to obtain the MAC address of the peer device. 1225. Connect to the peer device. 123Example: 124 125 ```ts 126 import { socket } from '@kit.ConnectivityKit'; 127 import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 128 129 // Start Bluetooth scanning to obtain the MAC address of the peer device. 130 let deviceId = 'xx:xx:xx:xx:xx:xx'; 131 132 // Connect to the peer device. 133 socket.sppConnect(deviceId, { 134 uuid: '00001101-0000-1000-8000-00805f9b34fb', 135 secure: true, 136 type: 0 137 }, (code, socketID) => { 138 if (code != null) { 139 console.error('sppConnect error, code = ' + (code as BusinessError).code); 140 return; 141 } 142 console.info('sppConnect success, socketId = ' + socketID); 143 }) 144 ``` 145 146For details about the error codes, see [Bluetooth Error Codes](../../reference/apis-connectivity-kit/errorcode-bluetoothManager.md). 147