Hi.
For bluetooth communication I use 32feet library.
First of all you have to "discover" devices using something like this:
InTheHand.Net.Sockets.BluetoothClient bc = null;
InTheHand.Net.Sockets.BluetoothDeviceInfo[] deviceInfo = null;
try
{
bc = new InTheHand.Net.Sockets.BluetoothClient();
deviceInfo = bc.DiscoverDevices();
if (deviceInfo.GetLength(0) != 0)
{
List<object> listData = new List<object>();
foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo bdinfo in deviceInfo)
{
listData.Add(bdinfo.DeviceName + " {" + bdinfo.DeviceAddress.ToInt64().ToString() + "}");
}
/// SHOW BLUETOOTH LIST AND SELECT ONE...
}
else
MessageBox.Show("no bluetooth device found");
}
finally
{
if (bc != null)
{
bc.Close();
bc.Dispose();
bc = null;
}
}
Then, when you have the device address, you can pair and print to it
InTheHand.Net.BluetoothAddress deviceAddress = InTheHand.Net.BluetoothAddress.None;
InTheHand.Net.BluetoothEndPoint ep = null;
InTheHand.Net.Sockets.BluetoothClient bc = null;
System.Net.Sockets.NetworkStream stream = null;
InTheHand.Net.BluetoothAddress bthAddress = null;
if (address == 0)
{
throw new Exception("no printer found");
}
else
bthAddress = new InTheHand.Net.BluetoothAddress(address); // THIS ADDRESS IS THE DEVICE ADDRESS
ep = new InTheHand.Net.BluetoothEndPoint(bthAddress, InTheHand.Net.Bluetooth.BluetoothService.SerialPort);
bc = new InTheHand.Net.Sockets.BluetoothClient();
InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(bthAddress, "0000"); // SET PIN
bc.Connect(ep);
stream = bc.GetStream();
}
try
{
string line = " TEST LINE";
System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
byte [] array = encoding.GetBytes(line);
stream.Write(array, 0, array.GetLength(0));
byte [] array = {13,10};
stream.Write(array, 0, array.GetLength(0));
}
catch (Exception ex)
{
logger.LogError("SendToPrinter", "", ex.Message);
throw ex;
}
finally
{
if (stream != null)
{
stream.Close();
stream = null;
}
if (bc != null)
{
bc.Close();
bc.Dispose();
bc = null;
}
}