Search code examples
c#bluetoothraspberry-pirfcommdisconnection

Raspberry PI Bluetooth RFcomm Immediately Disconnects


I am trying to create a connection between a Raspberry Pi, and a .NET application, on a Windows 10 computer. Thus far, I have managed to pair the 2 devices. The next step is to send data between them.

To do this, I used the following command to start listening on a port

sudo rfcomm watch /dev/rfcomm1

When I use PuTTY on the Windows machine to establish the connection, it works fine, so I suppose I configured the Raspberry PI correctly.

My goal is, however, to establish the connection via the .NET application. When I run the app, it does connect, but disconnects immediately after.

pi@raspberrypi:~ $ sudo rfcomm watch /dev/rfcomm1 Waiting for connection on channel 1 Connection from B4:0E:DE:13:FB:D6 to /dev/rfcomm1 Press CTRL-C for hangup Disconnected Waiting for connection on channel 1

Connection Bug

I used the following code for the .NET application.

using System;
using System.IO.Ports;

namespace ControllerManager
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ports = SerialPort.GetPortNames();
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }

            if (ports.Length > 0)
            {
                SerialPort Port = new SerialPort("COM12", 9600, Parity.None, 8, StopBits.One)
                {
                    Handshake = Handshake.XOnXOff
                };

                Console.WriteLine("");              
                Console.WriteLine($"Opened {Port.PortName}");              
                if (!Port.IsOpen) Port.Open();

                Console.ReadKey();
            }
        }
    }
}

Sudo btmon responses with the .NET app:

pi@raspberrypi:~ $ sudo btmon
Bluetooth monitor ver 5.55
= Note: Linux version 5.10.63-v7+ (armv7l)                             0.457478
= Note: Bluetooth subsystem version 2.22                               0.457492
= New Index: B8:27:EB:B5:04:83 (Primary,UART,hci0)              [hci0] 0.457502
= Open Index: B8:27:EB:B5:04:83                                 [hci0] 0.457506
= Index Info: B8:27:EB:B5:04:83 (Cypress Semiconductor)         [hci0] 0.457512
@ MGMT Open: bluetoothd (privileged) version 1.18             {0x0001} 0.457518
> HCI Event: Connect Request (0x04) plen 10                #1 [hci0] 105.381895
        Address: B4:0E:DE:13:FB:D6 (Intel Corporate)
        Class: 0x2a010c
          Major class: Computer (desktop, notebook, PDA, organizers)
          Minor class: Laptop
          Networking (LAN, Ad hoc)
          Capturing (Scanner, Microphone)
          Audio (Speaker, Microphone, Headset)
        Link type: ACL (0x01)
< HCI Command: Accept Connection R.. (0x01|0x0009) plen 7  #2 [hci0] 105.382027
        Address: B4:0E:DE:13:FB:D6 (Intel Corporate)
        Role: Master (0x00)
> HCI Event: Command Status (0x0f) plen 4                  #3 [hci0] 105.382508
      Accept Connection Request (0x01|0x0009) ncmd 1
        Status: Success (0x00)
> HCI Event: Role Change (0x12) plen 8                     #4 [hci0] 105.541361
        Status: Success (0x00)
        Address: B4:0E:DE:13:FB:D6 (Intel Corporate)
        Role: Master (0x00)
> HCI Event: Connect Complete (0x03) plen 11               #5 [hci0] 105.562721
        Status: Success (0x00)
        Handle: 11
        Address: B4:0E:DE:13:FB:D6 (Intel Corporate)
        Link type: ACL (0x01)
        Encryption: Disabled (0x00)
< HCI Command: Read Remote Support.. (0x01|0x001b) plen 2  #6 [hci0] 105.563343
        Handle: 11
> HCI Event: Command Status (0x0f) plen 4                  #7 [hci0] 105.564214
      Read Remote Supported Features (0x01|0x001b) ncmd 1
        Status: Success (0x00)
< HCI Command: Write Scan Enable (0x03|0x001a) plen 1      #8 [hci0] 105.564316
        Scan enable: No Scans (0x00)
> HCI Event: Read Remote Supported Featu.. (0x0b) plen 11  #9 [hci0] 105.564769
        Status: Success (0x00)
        Handle: 11
        Features: 0xbf 0xfe 0x0f 0xfe 0xdb 0xff 0x7b 0x87
          3 slot packets
          5 slot packets
          Encryption
          Slot offset
          Timing accuracy
          Role switch
          Sniff mode
          Power control requests
          Channel quality driven data rate (CQDDR)
          SCO link
          HV2 packets
          HV3 packets
          u-law log synchronous data
          A-law log synchronous data
          CVSD synchronous data
          Paging parameter negotiation
          Power control
          Transparent synchronous data
          Enhanced Data Rate ACL 2 Mbps mode
          Enhanced Data Rate ACL 3 Mbps mode
          Enhanced inquiry scan
          Interlaced inquiry scan
          Interlaced page scan
          RSSI with inquiry results
          Extended SCO link (EV3 packets)
          EV4 packets
          EV5 packets
          AFH capable slave
          AFH classification slave
          LE Supported (Controller)
          3-slot Enhanced Data Rate ACL packets
          5-slot Enhanced Data Rate ACL packets
          Sniff subrating
          Pause encryption
          AFH capable master
          AFH classification master
          Enhanced Data Rate eSCO 2 Mbps mode
          Enhanced Data Rate eSCO 3 Mbps mode
          3-slot Enhanced Data Rate eSCO packets
          Extended Inquiry Response
          Simultaneous LE and BR/EDR (Controller)
          Secure Simple Pairing
          Encapsulated PDU
          Erroneous Data Reporting
          Non-flushable Packet Boundary Flag
          Link Supervision Timeout Changed Event
          Inquiry TX Power Level
          Enhanced Power Control
          Extended features
> HCI Event: Command Complete (0x0e) plen 4               #10 [hci0] 105.565058
      Write Scan Enable (0x03|0x001a) ncmd 1
        Status: Success (0x00)

< ACL Data TX: Handle 11 flags 0x00 dlen 12                #76 [hci0] 36.293040
      Channel: 65 len 8 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0xef poll/final 0
         Length: 4
         FCS: 0xaa
         MCC Message type: Modem Status Command RSP (0x38)
           Length: 2
           dlci 2
           fc 0 rtc 1 rtr 1 ic 0 dv 1
> HCI Event: Number of Completed Packets (0x13) plen 5     #77 [hci0] 36.316233
        Num handles: 1
        Handle: 11
        Count: 2
> ACL Data RX: Handle 11 flags 0x02 dlen 18                #78 [hci0] 36.319919
      Channel: 64 len 14 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x03 cr 1 dlci 0x00
         Control: 0xef poll/final 0
         Length: 10
         FCS: 0x70
         MCC Message type: Remote Port Negotiation Command CMD (0x24)
           Length: 8
           dlci 2
           br 3 db 0 sb 0 p 0 pt 0 xi 0 xo 0
           rtri 0 rtro 0 rtci 0 rtco 0 xon 0 xoff 0
           pm 0x0001
< ACL Data TX: Handle 11 flags 0x00 dlen 18                #79 [hci0] 36.320062
      Channel: 65 len 14 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0xef poll/final 0
         Length: 10
         FCS: 0xaa
         MCC Message type: Remote Port Negotiation Command RSP (0x24)
           Length: 8
           dlci 2
           br 3 db 0 sb 0 p 0 pt 0 xi 0 xo 0
           rtri 0 rtro 0 rtci 0 rtco 0 xon 0 xoff 0
           pm 0x3f7f
> HCI Event: Number of Completed Packets (0x13) plen 5     #80 [hci0] 36.322590
        Num handles: 1
        Handle: 11
        Count: 2
> ACL Data RX: Handle 11 flags 0x02 dlen 18                #81 [hci0] 36.324936
      Channel: 64 len 14 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x03 cr 1 dlci 0x00
         Control: 0xef poll/final 0
         Length: 10
         FCS: 0x70
         MCC Message type: Remote Port Negotiation Command CMD (0x24)
           Length: 8
           dlci 2
           br 3 db 0 sb 0 p 0 pt 0 xi 0 xo 0
           rtri 0 rtro 0 rtci 0 rtco 0 xon 0 xoff 0
           pm 0x0001
< ACL Data TX: Handle 11 flags 0x00 dlen 18                #82 [hci0] 36.325051
      Channel: 65 len 14 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0xef poll/final 0
         Length: 10
         FCS: 0xaa
         MCC Message type: Remote Port Negotiation Command RSP (0x24)
           Length: 8
           dlci 2
           br 3 db 0 sb 0 p 0 pt 0 xi 0 xo 0
           rtri 0 rtro 0 rtci 0 rtco 0 xon 0 xoff 0
           pm 0x3f7f
> ACL Data RX: Handle 11 flags 0x02 dlen 12                #83 [hci0] 36.329897
      Channel: 64 len 8 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x03 cr 1 dlci 0x00
         Control: 0xef poll/final 0
         Length: 4
         FCS: 0x70
         MCC Message type: Modem Status Command CMD (0x38)
           Length: 2
           dlci 2
           fc 0 rtc 1 rtr 0 ic 0 dv 0
< ACL Data TX: Handle 11 flags 0x00 dlen 12                #84 [hci0] 36.330044
      Channel: 65 len 8 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0xef poll/final 0
         Length: 4
         FCS: 0xaa
         MCC Message type: Modem Status Command RSP (0x38)
           Length: 2
           dlci 2
           fc 0 rtc 1 rtr 0 ic 0 dv 0
< ACL Data TX: Handle 11 flags 0x00 dlen 8                 #85 [hci0] 36.330290
      Channel: 65 len 4 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Disconnect (DISC) (0x43)
         Address: 0x09 cr 0 dlci 0x02
         Control: 0x53 poll/final 1
         Length: 0
         FCS: 0xd9
> HCI Event: Number of Completed Packets (0x13) plen 5     #86 [hci0] 36.333749
        Num handles: 1
        Handle: 11
        Count: 2
> ACL Data RX: Handle 11 flags 0x02 dlen 12                #87 [hci0] 36.336186
      Channel: 64 len 8 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x03 cr 1 dlci 0x00
         Control: 0xef poll/final 0
         Length: 4
         FCS: 0x70
         MCC Message type: Modem Status Command CMD (0x38)
           Length: 2
           dlci 2
           fc 0 rtc 0 rtr 0 ic 0 dv 0
< ACL Data TX: Handle 11 flags 0x00 dlen 12                #88 [hci0] 36.336372
      Channel: 65 len 8 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Info with Header Check (UIH) (0xef)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0xef poll/final 0
         Length: 4
         FCS: 0xaa
         MCC Message type: Modem Status Command RSP (0x38)
           Length: 2
           dlci 2
           fc 0 rtc 0 rtr 0 ic 0 dv 0
> ACL Data RX: Handle 11 flags 0x02 dlen 8                 #89 [hci0] 36.337405
      Channel: 64 len 4 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Ack (UA) (0x63)
         Address: 0x09 cr 0 dlci 0x02
         Control: 0x73 poll/final 1
         Length: 0
         FCS: 0xf3
< ACL Data TX: Handle 11 flags 0x00 dlen 8                 #90 [hci0] 36.337588
      Channel: 65 len 4 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Disconnect (DISC) (0x43)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0x53 poll/final 1
         Length: 0
         FCS: 0x9c
> HCI Event: Number of Completed Packets (0x13) plen 5     #91 [hci0] 36.340002
        Num handles: 1
        Handle: 11
        Count: 2
> ACL Data RX: Handle 11 flags 0x02 dlen 8                 #92 [hci0] 36.344859
      Channel: 64 len 4 [PSM 3 mode Basic (0x00)] {chan 0}
      RFCOMM: Unnumbered Ack (UA) (0x63)
         Address: 0x01 cr 0 dlci 0x00
         Control: 0x73 poll/final 1
         Length: 0
         FCS: 0xb6
< ACL Data TX: Handle 11 flags 0x00 dlen 12                #93 [hci0] 36.345027
      L2CAP: Disconnection Request (0x06) ident 6 len 4
        Destination CID: 65
        Source CID: 64
> ACL Data RX: Handle 11 flags 0x02 dlen 12                #94 [hci0] 36.387495
      L2CAP: Disconnection Response (0x07) ident 6 len 4
        Destination CID: 65
        Source CID: 64
> HCI Event: Number of Completed Packets (0x13) plen 5     #95 [hci0] 36.387898
        Num handles: 1
        Handle: 11
        Count: 2
> HCI Event: Disconnect Complete (0x05) plen 4             #96 [hci0] 39.523319
        Status: Success (0x00)
        Handle: 11
        Reason: Remote User Terminated Connection (0x13)
@ MGMT Event: Device Disconnected (0x000c) plen 8     {0x0001} [hci0] 39.523393
        BR/EDR Address: B4:0E:DE:13:FB:D6 (Intel Corporate)
        Reason: Connection terminated by remote host (0x03)
< HCI Command: Write Scan Enable (0x03|0x001a) plen 1      #97 [hci0] 39.645666
        Scan enable: Page Scan (0x02)
> HCI Event: Command Complete (0x0e) plen 4                #98 [hci0] 39.646023
      Write Scan Enable (0x03|0x001a) ncmd 1
        Status: Success (0x00)

Any suggestions on how to solve this issue?, thanks in advance.


Solution

  • So I still haven't managed to use the System.IO.ports library properly; Instead, I now use the InTheHand bluetooth library, which functions well.