Categories

Client API

Events table

Event name Event description
event OnFinishedInit (BluetoothClient client, int status, string ErrorMessage); Raised when Bluetooth Client is fully initialized (only then methods can be called)
event OnAdvertisingStarted (BluetoothClient client, int status, string ErrorMessage); Raised when device has successfully started advertising.
event OnReceivingMessage (string Message); Raised when receiving a message from server.
event OnDisconnected (); Raised when the client is shutting down.

Methods table

Method name Method description
static BluetoothClient CreateClient (); Creates a new object on the scene and add component Bluetooth Client.
void Init (); Initializing the bluetooth client.
bool IsInit (); Is the bluetooth client initialized
void StartAdvertising (); Begins preparation for advertising. (the result of the start of advertising can be found in the OnAdvertisingStarted event)
void StopAdvertising (); Stops advertising on the device. (To start advertising again, it is necessary to stop advertising).
void SendData (string data); Sending a message to the connected server.
void Disconnect (); Disconnect the bluetooth client.

Events

event OnFinishedInit (BluetoothClient client, int status, string ErrorMessage)

The event that raised when the Bluetooth client initialization is completed. In response, the Bluetooth Client status comes and, if there is an error, its error description (error will be displayed if bluetooth activation is rejected by the user)

Parameters:

BluetoothClient client

The Bluetooth client that raised the event.

int status

Status code. 0 - successful, the others mean an error during initialization.

string ErrorMessage

Description of the error. If initialization is successful, it will be empty.

Example:

client.OnFinishedInit += (RaisedClient, status, ErrorMessage) => //in this case, client == RaisedClient
{
// The bluetooth client is fully initialized, and you can call the bluetooth client methods
if (status == 0 && string.IsNullOrEmpty(ErrorMessage))
{
//subscribing to necessary bluetooth client events and start advertising.
client.StartAdvertising();
}
else
{
Debug.LogError("BluetoothClient failed init. status = " + status + " | Error: " + ErrorMessage);
}
};

event OnAdvertisingStarted (BluetoothClient client, int status, string ErrorMessage)

The event that raised at the start of advertising. The response is the status of the Bluetooth client and, if there is an error, a description of its error.

Parameters:

BluetoothClient client

The Bluetooth client that raised the event.

int status

Status code. 0 - successful, the others mean an error during initialization.

string ErrorMessage

Description of the error. If initialization is successful, it will be empty.

Example:

client.OnAdvertisingStarted += (RaisedClient, status, ErrorMessage) => //in this case, client == RaisedClient
{
if (status == 0 && string.IsNullOrEmpty(ErrorMessage))
{
//everything went well, the device advertises itself, at this time the server can find the client.
}
else
{
Debug.LogError("BluetoothClient failed AdvertisingStarted. status = " + status + " | Error: " + ErrorMessage);
}
};

event OnReceivingMessage (string Message)

Raised when receiving a message from server.

Parameters:

string Message

Received message from server.

Example:

client.OnReceivingMessage += (Message) =>
{
Debug.Log("ReceivingMessage:: " + Message);
};

event OnDisconnected ()

Raised when the client is shutting down.

Example:

client.OnDisconnected += () =>
{
client.Close();
//Execution of some logic after the client shutdown.
};

Methods

static BluetoothClient CreateClient ()

Creates an instance of BluetoothClient. Important:When creating an instance of GameObject, DontDestroyOnLoad is used. To avoid this, you can add the BluetoothClient component via AddComponent<BluetoothClient>();

Example:

// Creates an instance of BluetoothClient.
BluetoothClient client = BluetoothClient.CreateClient();

void Init()

Initializing the bluetooth client.

Example:

client.Init();

void StartAdvertising ()

Start of advertising (it is necessary for the server to be able to detect the device).

Example:

// The beginning of advertising.
client.StartAdvertising();

void StopAdvertising()

Completion of advertising.

Example:

client.StopAdvertising();

void SendData(string data)

Sending a message to server.

Example:

// Sending a message to server
client.SendData("Some kind of own message");

void Disconnect()

Disconnect the bluetooth client. after completing all procedures, the event OnDisconnected will be called.

Example:

// Disconnecting bluetooth client
client.Disconnect();