BtLibrary
0.97
Bluetooth Classic Library for Unity
|
Represents a remote Bluetooth device. More...
Public Member Functions | |||||
BluetoothDevice () | |||||
Initializes a new instance of this class. More... | |||||
byte[] | read () | ||||
Read all available bytes , or read the next available packet if packetization was used. More... | |||||
byte[] | read (int size) | ||||
Read from available bytes up to a max size number of bytes, or read all bytes Regarding its Size of the next available packet if packetization was used. More... | |||||
BtPackets | readAllPackets () | ||||
Read all available bytes , or read the next available packet if packetization was used. More... | |||||
void | setPacketSize (int size) | ||||
Packetize data into packets of a size number of bytes per each. More... | |||||
void | setEndByte (byte byt) | ||||
Packetize data into packets, each packets ends with the byte byt . More... | |||||
void | setBufferSize (int size) | ||||
Sets the underlying buffer size, and stops it from changing size dynamically. This will save resources. More... | |||||
void | normal_connect (bool isBrutal, bool isSecure) | ||||
Connect to this device. Unlike connect(int, int, bool) it's just a one attempt connection.
| |||||
void | connect () | ||||
Equivalent to connect(10, 1000, false) . More... | |||||
void | connect (int attempts) | ||||
Equivalent to connect(tries, 1000, false) . More... | |||||
void | connect (int attempts, int time) | ||||
Equivalent to connect(attempts, time, false) . More... | |||||
void | connect (int attempts, int time, bool allowDiscovery) | ||||
Connect to this device. More... | |||||
void | close () | ||||
Close connection with this device. More... | |||||
void | send (byte[] msg) | ||||
Sends a byte[] More... | |||||
void | send_Blocking (byte[] msg) | ||||
Similar to send(byte[]) , but it's a synchronouse/blocking method. send(byte[]) will raise exception if it was called from a C# thread, so if you're using threads in Unity, it's better to use this method. More... | |||||
Properties | |
bool | IsReading [get, set] |
Gets a value indicating whether this device is reading. More... | |
bool | IsDataAvailable [get, set] |
Gets a value indicating whether this device has data available to read. More... | |
bool | IsConnected [get, set] |
Gets a value indicating whether this device is connected. More... | |
Func< BluetoothDevice, IEnumerator > | ReadingCoroutine [get, set] |
If it's not Null then the referenced IEnumerator will be started directly after a reading started. More... | |
string | Name [get, set] |
Gets or sets the name of this device. More... | |
string | MacAddress [get, set] |
Gets or sets the mac address of this device. More... | |
string | UUID [get, set] |
Gets or sets the UUI. More... | |
bool | WillRead [get, set] |
Gets or sets a value indicating whether this device will read. More... | |
int | ThreadID [get, set] |
Gets and Sets the reading thread/process ID number of this device instance. More... | |
Events | |
Action< BluetoothDevice > | OnConnected |
Occurs when this BluetoothDevice instance get connected, and pass its reference. More... | |
Action< BluetoothDevice > | OnDisconnected |
Occurs when this BluetoothDevice instance get disconnected, and pass its reference. More... | |
Action< BluetoothDevice > | OnDeviceNotFound |
Occurs when this BluetoothDevice instance can't be found as a near by Device, and pass its reference. More... | |
Action< BluetoothDevice > | OnDeviceOFF |
Occurs when this BluetoothDevice instance has been found but failed to connect to it, and pass its reference. More... | |
Action< BluetoothDevice > | OnSendingError |
Occurs when on sending error. Passes this BluetoothDevice reference that has the error. More... | |
Action< BluetoothDevice > | OnReadingError |
Occurs when on reading error. Passes this BluetoothDevice reference that has the error. More... | |
Action< BluetoothDevice > | OnReadingStarted |
Occurs when on reading starts for this BluetoothDevice insance and Passes its reference. More... | |
Action< BluetoothDevice > | OnReadingStoped |
Occurs when on reading stops for this BluetoothDevice insance and Passes its reference. More... | |
Represents a remote Bluetooth device.
A BluetoothDevice lets you create a connection with the respective device or query information about it, such as the name, address, class, and bonding state.
TechTweaking.Bluetooth.BluetoothDevice.BluetoothDevice | ( | ) |
Initializes a new instance of this class.
void TechTweaking.Bluetooth.BluetoothDevice.close | ( | ) |
Close connection with this device.
void TechTweaking.Bluetooth.BluetoothDevice.connect | ( | ) |
Equivalent to connect(10, 1000, false) .
void TechTweaking.Bluetooth.BluetoothDevice.connect | ( | int | attempts | ) |
Equivalent to connect(tries, 1000, false) .
void TechTweaking.Bluetooth.BluetoothDevice.connect | ( | int | attempts, |
int | time | ||
) |
Equivalent to connect(attempts, time, false) .
void TechTweaking.Bluetooth.BluetoothDevice.connect | ( | int | attempts, |
int | time, | ||
bool | allowDiscovery | ||
) |
Connect to this device.
Asynchronous Method
If this instance has initialize proberly, either by providing enough data to recognize the device like its Name or MAC address, or it was a reference returned by a method/event of this Library, then this method will try to connet to the remote device without blocking
attempts | Number of connection attempts before it fails, each connection try might differ than the other internally so it's not like calling this method multiple times |
time | The time in melisecond to wait between two connection attempts, it's better to set it to 1000 which means 1 second |
allowDiscovery | When you use the Name property to identefy the device, the Library needs to search for a match for that name within the paired devices, setting this parameters to true will start a discovery process for 12 seconds if the device hasn't been found as a Paired device. it's not a good idea to use it, because you'll block any further connection for 12 seconds. By just calling startDiscover() , it will enlist any nearby found device in the Paired devices list so there will be no need for this parameter to be |
connect(...)
method multiple times, it will save the order of those calls and execute them in order,}
byte [] TechTweaking.Bluetooth.BluetoothDevice.read | ( | ) |
Read all available bytes
, or read the next available packet if packetization was used.
The default returned value would be all available bytes
, but if data was packetized using a packetization method , then it would return the next available packet.
Available packetization methods : setEndByte(), setPacketSize()
byte[]
contains all available bytes, or all available bytes in the next available packet if packetization was usedbyte [] TechTweaking.Bluetooth.BluetoothDevice.read | ( | int | size | ) |
Read from available bytes
up to a max size
number of bytes, or read all bytes
Regarding its Size of the next available packet if packetization was used.
The default returned value would be at most a size
number of available bytes
, but if data was packetized using a packetization method , then it would return the next available packet regarding its size.
Available packetization methods : setEndByte(), setPacketSize()
byte[]
contains all available bytes, or all available bytes in the next available packet if packetization was usedsize | maximum number of bytes to read |
BtPackets TechTweaking.Bluetooth.BluetoothDevice.readAllPackets | ( | ) |
Read all available bytes
, or read the next available packet if packetization was used.
The default returned value would be all available bytes
, but if data was packetized using a packetization method , then it would return the next available packet.
Available packetization methods : setEndByte(), setPacketSize()
byte[]
contains all available bytes, or all available bytes in the next available packet if packetization was usedvoid TechTweaking.Bluetooth.BluetoothDevice.send | ( | byte[] | msg | ) |
Sends a byte[]
array to this device.
{Asynchronous Method}
Notice the last code with a one call is more efficient, since it doesn't need to create a multiple messages to send one after the other.
}
msg | Array of bytes to send |
void TechTweaking.Bluetooth.BluetoothDevice.send_Blocking | ( | byte[] | msg | ) |
Similar to send(byte[]) , but it's a synchronouse/blocking method. send(byte[]) will raise exception if it was called from a C# thread, so if you're using threads in Unity, it's better to use this method.
void TechTweaking.Bluetooth.BluetoothDevice.setBufferSize | ( | int | size | ) |
Sets the underlying buffer size, and stops it from changing size dynamically. This will save resources.
If you know what buffer size is good for you (you can find that by experimenting), then it's better to make the buffer static with a fixed size, because changing its size dynamically costs a lot of resources
void TechTweaking.Bluetooth.BluetoothDevice.setEndByte | ( | byte | byt | ) |
Packetize data into packets, each packets ends with the byte byt
.
Should be called before reading started, otherwize it might make no effect, so it's safer to call it before calling connect().
The method will override any previous packetization. Check what effect this method has on read() and read(int).
Packets won't contain the end Byte byt
Example Calling setEndByte(13), then read() or read(int) will return the next available bunch of bytes as a byte
array that ends with the byte 13
, if there was no end Byte '13' in your sequence of bytes, they won't return anything.
byt | a byte that seperates each packate from the next one |
void TechTweaking.Bluetooth.BluetoothDevice.setPacketSize | ( | int | size | ) |
Packetize data into packets of a size
number of bytes per each.
Should be called before reading started, otherwize it might make no effect, so it's safer to call it before calling connect().
The method will override any previous packetization. Check what effect this method has on read() and read(int).
Example Calling setPacketSize(10), then read() or read(int) will return a byte
array of size 10, if 10 or more bytes are available.
size | size of each packet |
|
getset |
Gets a value indicating whether this device is connected.
true
if this device is connected; otherwise, false
.
|
getset |
Gets a value indicating whether this device has data available to read.
true
if it has data available; otherwise, false
.
Data mean bytes unless a method to packetize data has been called on this instance, then data would mean packets.
Available packetization methods : setEndByte(), setPacketSize()
|
getset |
Gets a value indicating whether this device is reading.
true
if this instance is reading; otherwise, false
.
|
getset |
Gets or sets the mac address of this device.
If you assigned a value to this Property, then the connect() method will try to find a device with the Mac Address
assigned and connect to it.
You can either use this property to identfy the device and connect or the MacAddress property, using both of them isn't possible simply the last call will override everything.
Most of the time its value is the name of the device, if this Library has recognized the device after connecting, or you're using this property to connect.
The mac address.
|
getset |
Gets or sets the name of this device.
If you assigned a value to this Property, then the connect() method will try to find a device with the the device Name
assigned and connect to it.
You can either use this property to identfy the device and connect or the property, using both of them isn't possible simply the last call will override everything.
if this Library has recognized the device after connecting, or you're using this property to connect, then its value will be a string of the device name
, otherwise empty string
|
getset |
If it's not Null
then the referenced IEnumerator
will be started directly after a reading started.
Example Code
MyMethodName(BluetoothDevice device)
will start directly after a reading channel has been istablished. the parameter device will be equal to yourDevice.
|
getset |
Gets and Sets the reading thread/process ID number of this device instance.
making two devices has the same threadID and not equal to 0, will remove the overhead of creating a thread/process for each device by sharing the same thread/process. this of course will introduce a delay for both devices.
0
is the default value and means that it will has a single thread/process for itself, otherwise it will share a thread/process with other devices that has a similar ID
|
getset |
Gets or sets the UUI.
A Universally Unique Identifier (UUID) is a standardized 128-bit format for a string ID used to uniquely identify information. The point of a UUID is that it's big enough that you can select any random and it won't clash. In this case, it's used to uniquely identify your application's Bluetooth service. To get a UUID to use with your application, you can use one of the many random UUID generators on the web
The Default UUID is the one mostly used with Electronic classic bluetooth prepherals (SPP) which is "00001101-0000-1000-8000-00805F9B34FB"
|
getset |
Gets or sets a value indicating whether this device will read.
If WillRead is false before calling connect() the device won't reads
true
if will read; otherwise, false
. Default is true
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnConnected |
Occurs when this BluetoothDevice instance get connected, and pass its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnDeviceNotFound |
Occurs when this BluetoothDevice instance can't be found as a near by Device, and pass its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnDeviceOFF |
Occurs when this BluetoothDevice instance has been found but failed to connect to it, and pass its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnDisconnected |
Occurs when this BluetoothDevice instance get disconnected, and pass its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnReadingError |
Occurs when on reading error. Passes this BluetoothDevice reference that has the error.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnReadingStarted |
Occurs when on reading starts for this BluetoothDevice insance and Passes its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnReadingStoped |
Occurs when on reading stops for this BluetoothDevice insance and Passes its reference.
Action<BluetoothDevice> TechTweaking.Bluetooth.BluetoothDevice.OnSendingError |
Occurs when on sending error. Passes this BluetoothDevice reference that has the error.