harmony 鸿蒙@ohos.bluetooth.socket (Bluetooth socket Module)

  • 2023-10-30
  • 浏览 (153)

@ohos.bluetooth.socket (Bluetooth socket Module)

The socket module provides APIs for operating and managing Bluetooth sockets.

NOTE

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.

Modules to Import

import socket from '@ohos.bluetooth.socket';

socket.sppListen

sppListen(name: string, options: SppOptions, callback: AsyncCallback<number>): void

Creates a Serial Port Profile (SPP) listening socket for the server.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
name string Yes Name of the service.
option SppOptions Yes SPP listening configuration.
callback AsyncCallback<number> Yes Callback invoked to return the server socket ID.

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let serverNumber = -1;
function serverSocket(code: BusinessError, number: number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}

let sppOption:socket.SppOptions = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
try {
    socket.sppListen('server1', sppOption, serverSocket);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.sppAccept

sppAccept(serverSocket: number, callback: AsyncCallback<number>): void

Accepts a connection request from the client over a socket of the server.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
serverSocket number Yes Server socket ID.
callback AsyncCallback<number> Yes Callback invoked to return the client socket ID.

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let serverNumber = -1;
function serverSocket(code: BusinessError, number: number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}
let clientNumber = -1;
function acceptClientSocket(code: BusinessError, number: number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth clientSocket Number: ' + number);
    // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the server.
    clientNumber = number;
  }
}
try {
    socket.sppAccept(serverNumber, acceptClientSocket);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.sppConnect

sppConnect(deviceId: string, options: SppOptions, callback: AsyncCallback<number>): void

Initiates an SPP connection to a remote device from the client.

Required permissions: ohos.permission.ACCESS_BLUETOOTH

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
deviceId string Yes Address of the remote device, for example, XX:XX:XX:XX:XX:XX.
option SppOptions Yes SPP listening configuration for the connection.
callback AsyncCallback<number> Yes Callback invoked to return the client socket ID.

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';

let clientNumber = -1;
function clientSocket(code: BusinessError, number: number) {
  if (code.code != 0||code == null) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
let sppOption:socket.SppOptions = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
try {
    socket.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.sppCloseServerSocket

sppCloseServerSocket(socket: number): void

Closes an SPP listening socket of the server.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
socket number Yes Server socket ID, which is obtained by sppListen().

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2900001 Service stopped.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let serverNumber = -1;
function serverSocket(code: BusinessError, number: number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}
try {
    socket.sppCloseServerSocket(serverNumber);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.sppCloseClientSocket

sppCloseClientSocket(socket: number): void

Closes an SPP listening socket of the client.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
socket number Yes Client socket ID, which is obtained by sppAccept() or sppConnect().

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2900001 Service stopped.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let clientNumber = -1;
function clientSocket(code: BusinessError, number: number) {
  if (code.code != 0||code == null) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
try {
    socket.sppCloseClientSocket(clientNumber);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.sppWrite

sppWrite(clientSocket: number, data: ArrayBuffer): void

Writes data to the remote device through a socket.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
clientSocket number Yes Client socket ID, which is obtained by sppAccept() or sppConnect().
data ArrayBuffer Yes Data to write.

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2901054 IO error.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let clientNumber = -1;
function clientSocket(code: BusinessError, number: number) {
  if (code.code != 0||code == null) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
let arrayBuffer = new ArrayBuffer(8);
let data = new Uint8Array(arrayBuffer);
data[0] = 123;
try {
    socket.sppWrite(clientNumber, arrayBuffer);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.on(‘sppRead’)

on(type: ‘sppRead’, clientSocket: number, callback: Callback<ArrayBuffer>): void

Subscribes to SPP read request events.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is sppRead, which indicates an SPP read request event.
clientSocket number Yes Client socket ID, which is obtained by sppAccept() or sppConnect().
callback Callback<ArrayBuffer> Yes Callback invoked to return the data read.

Error codes

For details about the error codes, see Bluetooth Error Codes.

ID Error Message
2901054 IO error.
2900099 Operation failed.

Example

import { BusinessError } from '@ohos.base';
let clientNumber = -1;
function clientSocket(code: BusinessError, number: number) {
  if (code.code != 0||code == null) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
function dataRead(dataBuffer: ArrayBuffer) {
  let data = new Uint8Array(dataBuffer);
  console.log('bluetooth data is: ' + data[0]);
}
try {
    socket.on('sppRead', clientNumber, dataRead);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

socket.off(‘sppRead’)

off(type: ‘sppRead’, clientSocket: number, callback?: Callback<ArrayBuffer>): void

Unsubscribes from SPP read request events.

System capability: SystemCapability.Communication.Bluetooth.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is sppRead, which indicates an SPP read request event.
clientSocket number Yes Client socket ID, which is obtained by sppAccept() or sppConnect().
callback Callback<ArrayBuffer> No Callback for the SPP read request event. If this parameter is not set, this API unsubscribes from all callbacks corresponding to type.

Example

import { BusinessError } from '@ohos.base';
let clientNumber = -1;
function clientSocket(code: BusinessError, number: number) {
  if (code.code != 0||code == null) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
try {
    socket.off('sppRead', clientNumber);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

SppOptions

Defines the SPP configuration.

System capability: SystemCapability.Communication.Bluetooth.Core

Name Type Readable Writable Description
uuid string Yes Yes UUID of the SPP.
secure boolean Yes Yes Whether it is a secure channel.
type SppType Yes Yes Type of the SPP link.

SppType

Enumerates the SPP link types.

System capability: SystemCapability.Communication.Bluetooth.Core

Name Value Description
SPP_RFCOMM 0 Radio frequency communication (RFCOMM) link.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞