General Bluetooth Data Connections
For RFCOMM connections, the library includes the
BluetoothClient,
BluetoothAddress,
BluetoothEndPoint, and
BluetoothListener classes. So, to connect to an RFCOMM/SPP service on a particular peer device, use code like the following. This creates a sockets-like connection which is accessed through a .NET
System.IO.Stream for read and write. It does
not create a virtual COM port, see
Bluetooth Serial Ports for that case.
Class MyConsts
Shared ReadOnly MyServiceUuid As Guid _
= New Guid("{00112233-4455-6677-8899-aabbccddeeff}")
End Class
Dim addr As BluetoothAddress _
= BluetoothAddress.Parse("001122334455")
Dim serviceClass As Guid
serviceClass = BluetoothService.SerialPort
' - or - etc
' serviceClass = MyConsts.MyServiceUuid
'
Dim ep As New BluetoothEndPoint(addr, serviceClass)
Dim cli As New BluetoothClient
cli.Connect(ep)
Dim peerStream As Stream = cli.GetStream()
peerStream.Write/Read ...
e.g.
Dim buf(1000) As Byte
Dim readLen as Integer = peerStream.Read(buf, 0, buf.Length)
If readLen = 0 Then
Console.WriteLine("Connection is closed")
Else
Console.WriteLine("Recevied {0} bytes", readLen)
End If
class MyConsts
{
static readonly Guid MyServiceUuid
= new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
}
BluetoothAddress addr
= BluetoothAddress.Parse("001122334455");
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
// - or - etc
// serviceClass = MyConsts.MyServiceUuid
//
var ep = new BluetoothEndPoint(addr, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write/Read ...
e.g.
byte[] buf = new byte[1000];
int readLen = peerStream.Read(buf, 0, buf.Length);
if (readLen == 0) {
Console.WriteLine("Connection is closed");
} else {
Console.WriteLine("Recevied {0} bytes", readLen);
}
The Service Class Id should be changed to suit the service you are connecting too, e.g. your custom UUID/Guid,
BluetoothService.ObexObjectPush,
BluetoothService.PhonebookAccessPse, etc. (Do
not use
BluetoothService.RFCommProtocol that is pointless, BluetoothClient
always uses RFCOMM).
A connection can also be made to a particular Port number (RFCOMM Channel number) by setting the port in the
BluetoothEndPoint. There are also cases where the service identifies itself not by a unique Service Class Id but by a particular Service Name, for instance when a device has multiple virtual serial port services then the correct one can be identified by its name. In that case see the
Connect by Service Name section in the
SDP chapter.
Note that RFCOMM/SPP only allows one connection from a remote device to each service. So do not use BluetoothClient and a virtual Serial port to connect to the same service at the same time; the second one will fail to connect.
Note that creating a
BluetoothClient or
BluetoothListener connection does
not create a virtual Serial Port that other applications can use. If you need a virtual COM port for another application to read and write from then see
Bluetooth Serial Ports.
DiscoveryDeviceName and DiscoveryBluetooth Server-sideErrorsStream.Read and the Number of Bytes Returned
Connected Property