BtLibrary  0.97
Bluetooth Classic Library for Unity
Public Member Functions | Properties | Events | List of all members
TechTweaking.Bluetooth.BluetoothDevice Class Reference

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.

Parameters
isBrutalIf set to true it will attempt to do a hacked connection, in some devices speciall fake/cloned devices a hacked connection works better than normal connection.
isSecureIf set to true it will istablish a secure/encoded communication channel, "Secure Connection" will add extra security matters that aren't useful for most users, so it's better to leave it as false

 
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[] array to this device. 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< BluetoothDeviceOnConnected
 Occurs when this BluetoothDevice instance get connected, and pass its reference. More...
 
Action< BluetoothDeviceOnDisconnected
 Occurs when this BluetoothDevice instance get disconnected, and pass its reference. More...
 
Action< BluetoothDeviceOnDeviceNotFound
 Occurs when this BluetoothDevice instance can't be found as a near by Device, and pass its reference. More...
 
Action< BluetoothDeviceOnDeviceOFF
 Occurs when this BluetoothDevice instance has been found but failed to connect to it, and pass its reference. More...
 
Action< BluetoothDeviceOnSendingError
 Occurs when on sending error. Passes this BluetoothDevice reference that has the error. More...
 
Action< BluetoothDeviceOnReadingError
 Occurs when on reading error. Passes this BluetoothDevice reference that has the error. More...
 
Action< BluetoothDeviceOnReadingStarted
 Occurs when on reading starts for this BluetoothDevice insance and Passes its reference. More...
 
Action< BluetoothDeviceOnReadingStoped
 Occurs when on reading stops for this BluetoothDevice insance and Passes its reference. More...
 

Detailed Description

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.

Constructor & Destructor Documentation

TechTweaking.Bluetooth.BluetoothDevice.BluetoothDevice ( )

Initializes a new instance of this class.

Member Function Documentation

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

Parameters
attemptsNumber 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
timeThe time in melisecond to wait between two connection attempts, it's better to set it to 1000 which means 1 second
allowDiscoveryWhen 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
Remarks
{ If you called any connect(...) method multiple times, it will save the order of those calls and execute them in order,
device1.connect(30)//30 trials, and lets assume they all fails
device2.connect(10)//It might take around 30 seconds to start executing this operation [In the background, becaus it's asynchronous]

}

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()

Returns
byte[] contains all available bytes, or all available bytes in the next available packet if packetization was used
byte [] 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()

Returns
byte[] contains all available bytes, or all available bytes in the next available packet if packetization was used
Parameters
sizemaximum 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()

Returns
byte[] contains all available bytes, or all available bytes in the next available packet if packetization was used
void TechTweaking.Bluetooth.BluetoothDevice.send ( byte[]  msg)

Sends a byte[] array to this device.

{Asynchronous Method}

Remarks
{ Asynchronous method, which means that after the method returns sending might even hasn't started yet. It's optemized to start at the most appropriate time, and it saves the order of mu. so for example the following code snippest has the same result:
device.send(new byte[]{1 , 2};
device.send(new byte[]{3 , 4};
device.send(new byte[]{5 , 6};
it has the same effect as the following :
device.send(new byte[]{1 , 2 , 3 , 4 , 5 , 6};

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.

}

Parameters
msgArray 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.

Parameters
byta 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.

Parameters
sizesize of each packet

Property Documentation

bool TechTweaking.Bluetooth.BluetoothDevice.IsConnected
getset

Gets a value indicating whether this device is connected.

true if this device is connected; otherwise, false.

bool TechTweaking.Bluetooth.BluetoothDevice.IsDataAvailable
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()

bool TechTweaking.Bluetooth.BluetoothDevice.IsReading
getset

Gets a value indicating whether this device is reading.

true if this instance is reading; otherwise, false.

string TechTweaking.Bluetooth.BluetoothDevice.MacAddress
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.

string TechTweaking.Bluetooth.BluetoothDevice.Name
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

Func<BluetoothDevice,IEnumerator> TechTweaking.Bluetooth.BluetoothDevice.ReadingCoroutine
getset

If it's not Null then the referenced IEnumerator will be started directly after a reading started.

Example Code

IEnumerator MyMethodName (BluetoothDevice device) { ... }
...
yourDevice.readingCoroutine = MyMethodName;

MyMethodName(BluetoothDevice device) will start directly after a reading channel has been istablished. the parameter device will be equal to yourDevice.

int TechTweaking.Bluetooth.BluetoothDevice.ThreadID
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

string TechTweaking.Bluetooth.BluetoothDevice.UUID
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"

bool TechTweaking.Bluetooth.BluetoothDevice.WillRead
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

Event Documentation

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.


The documentation for this class was generated from the following file: