Categories

Server API

Events table

Event name Event description
event OnFinishedInit (BluetoothServer server, int status, string ErrorMessage); Raised when Bluetooth Server is fully initialized (only then methods can be called)
event OnConnectsCompleted (List<BluetoothDevice> devices); Raised when the devices are successfully connected to the server.
event OnDisconnectDevice (BluetoothDevice device); Raised when the device is disconnected from the server.
event OnReceivingMessage (string Message); Raised when receiving a message from clients.
event OnDisconnected (); Raised when the server is shutting down.

Properties table

Properties name Properties description
List<BluetoothDevice> Devices; List of connected devices

Methods table

Method name Method description
static BluetoothServer CreateServer (); Creates a new object on the scene and add component Bluetooth Server.
void Init (); Initializing the bluetooth server.
bool IsInit (); Is the bluetooth server initialized
void StartScan (float ScanTime, Action StopScanCallback); Search for Bluetooth clients, when you receive a callback, you can call the clients connections method.
void StopScan (); Stopping bluetooth clients search.
void ConnectDevices (); Connecting to the detected bluetooth clients.
void SendData (string data); Sending a message to all connected bluetooth clients.
void Disconnect (BluetoothDevice device); Disconnect the selected bluetooth client.
void Disconnect (); Terminates the bluetooth server completely.

Events

event OnFinishedInit (BluetoothServer server, int status, string ErrorMessage)

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

Parameters:

BluetoothServer server

The Bluetooth server 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:

server.OnFinishedInit += (RaisedServer, status, ErrorMessage) => //in this case, server == RaisedServer
{
// The bluetooth server is fully initialized, and you can call the bluetooth server methods
if (status == 0 && string.IsNullOrEmpty(ErrorMessage))
{
//subscribing to necessary bluetooth server events and start scanning devices.
server.StartScan(7f, FinishScan);
}
else
{
Debug.LogError("BluetoothServer failed init. status = " + status + " | Error: " + ErrorMessage);
}
};

event OnConnectsCompleted (List<BluetoothDevice> devices)

Raised when the devices are successfully connected to the server.

Parameters:

List<BluetoothDevice> devices

list of connected devices.

Example:

server.OnConnectsCompleted += (List<BluetoothDevice> devices) =>
{
if (devices.Count > 0)
{
Debug.Log("Bluetooth server is connected to the devices, you can exchange information with clients.");
}
else
{
Debug.Log("The Bluetooth server is not connected to any device, try searching for clients again.");
}
};

event OnDisconnectDevice (BluetoothDevice device)

Raised when the device is disconnected from the server. This happens when the connection is scheduled to terminate or when the connection is suddenly disconnected (in this case, it may take a few seconds until the Bluetooth protocol makes sure that the device is not available).

Parameters:

BluetoothDevice device

The device that has become unavailable.

Example:

server.OnDisconnectDevice += (device) =>
{
Debug.Log("The device is disconnect. Id device:" + device.GetAddress());
};

event OnReceivingMessage (string Message)

Raised when receiving a message from clients.

Parameters:

string Message

Received message from clients.

Example:

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

event OnDisconnected ()

Raised when the server is shutting down.

Example:

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

Methods

static BluetoothServer CreateServer ()

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

Example:

// Creates an instance of BluetoothServer.
BluetoothServer server = BluetoothServer.CreateServer();

void Init()

Initializing the bluetooth server.

Example:

server.Init();

void StartScan (float ScanTime, Action StopScanCallback)

Search for Bluetooth clients.

Parameters:

float ScanTime

Scan time

Action StopScanCallback

A callback that indicates that the device search has ended.

Example:

// Start scanning BluetoothClient.
server.StartScan(7f,FinishScan);

void StopScan()

Finish scanning.

Example:

server.StopScan();

void ConnectDevices()

Connecting to detected devices.

Example:

// You need to call after the scan! otherwise, there are no devices to connect.
server.ConnectDevices();

void SendData(string data)

Sending a message to all connected bluetooth clients.

Example:

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

void Disconnect(BluetoothDevice device)

Disconnect the selected bluetooth client.

Example:

// Disconnecting from the first connected device
server.Disconnect(server.Devices[0]);

void Disconnect()

Terminates the bluetooth server completely.If there are active clients, then it terminates communication with them and correctly collects data, after completing all procedures, the event OnDisconnected will be called;

Example:

// It must be completed after the work is finished. Since this frees up the Bluetooth module.
server.Disconnect();