Server API
Events table
Properties table
Methods table
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) =>
{
if (status ==
0 && string.IsNullOrEmpty(ErrorMessage))
{
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 += () =>
{
};
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:
BluetoothServer server =
BluetoothServer.CreateServer();
void Init()
Initializing the bluetooth server.
Example:
server.Init();
void StartScan
(float ScanTime, Action<List<BluetoothDevice>>
StopScanCallback)
Search for Bluetooth clients.
Parameters:
float ScanTime
Scan time
Action StopScanCallback
A callback that indicates that the device search has ended.
Example:
server.StartScan(7f,FinishScan);
void StopScan()
Finish scanning.
Example:
server.StopScan();
void ConnectDevices()
Connecting to detected devices.
Example:
server.ConnectDevices();
void SendData(string data)
Sending a message to all connected bluetooth clients.
Example:
server.SendData("Some kind of own message");
void SendDataToSingleDevice(string data, BluetoothDevice
device)
Sending a message to the selected connected Bluetooth client.
Example:
server.SendDataToSingleDevice("Some kind of own message", device);
void Disconnect(BluetoothDevice
device)
Disconnect the selected bluetooth client.
Example:
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:
server.Disconnect();