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. [1]

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.

Discovery
DeviceName and Discovery
Bluetooth Server-side
Errors
Stream.Read and the Number of Bytes Returned
Connected Property
RFCOMM Control Information

Notes

[1] The one RFCOMM connection is a protocol limitation. TCP/IP has port numbers on source and destination ends and thus can support multiple connections, RFCOMM has only the remote channel number so only can support one connection.

If you need multiple connection and are in control of the 'server' device then have the service listen on two (or more) RFCOMM channels so the client can connect to different channel numbers.

Last edited Jan 15 at 11:17 AM by alanjmcf, version 16

Comments

nakram Apr 26 at 8:51 AM 
IMPORTANT:
The UUID you specify in the ServiceClass variable MUST be same on both sides of the connection.

For example, I did this on C# .Net on a Windows 8 laptop with Microsoft BT Stack:

string lgphone = "10:F9:6F:21:2E:2E"; // The MAC address of my phone, lets assume we know it

BluetoothAddress addr = BluetoothAddress.Parse(lgphone);
var btEndpoint = new BluetoothEndPoint(addr, MyConsts.MyServiceUuid);
var btClient = new BluetoothClient();
btClient.Connect(btEndpoint);

Stream peerStream = btClient.GetStream();

StreamWriter sw = new StreamWriter(peerStream);
sw.WriteLine("Hello World");
sw.Flush();
sw.Close();

btClient.Close();
btClient.Dispose();
btEndpoint = null;

Android:
btServerSock = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("LGP", java.util.UUID.fromString("00112233-4455-6677-8899-aabbccddeeff"));
btSock = btServerSock.accept();

if(btSock != null){
Log.d(strX, "Null returned");
}
InputStreamReader isr = new InputStreamReader(btSock.getInputStream());
int charRead = 0;
while((charRead = isr.read()) != -1){
strOut += (char)charRead;
}
I hope this helps.