You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Add AddressFamily.Bluetooth, ProtocolType.L2Cap, .RFComm, .Hci and .Sco #56828
Both Windows and Linux support connecting to Bluetooth devices using sockets:
On Linux, you create a socket with AF_BLUETOOTH and BTPROTO_L2CAP, BTPROTO_RFCOMM, BTPROTO_SCO or BTPROTO_HCI to connect to devices over Bluetooth. See bluetooth.h for more details
On Windows, you create a socket with AF_BTH and BTHPROTO_RFCOMM, see Bluetooth and socket
I believe macOS uses a higher-level API and doesn't expose Bluetooth sockets as such.
Similar to #28636, this API proposal suggests adding the BluetoothAddressFamily and the corresponding ProtocolType values to enable programmers to use .NET to connect to Bluetooth sockets.
Requests for (basic) support for Bluetooth came up before: see #20379, #862 (comment).
namespace System.Net.Sockets
{
public enum AddressFamily
{
+ Bluetooth = 33,
}
public enum ProtocolType
{
+ L2Cap, /* Logical link control and adaptation protocol */+ RFComm, /* Radio frequency communication */+ Hci, /* Host Controller Interface */+ Sco, /* Synchronous Connection-Oriented */
}
// A single BluetoothEndPoint class could be used for L2Cap, RFComm and SCO
+ public sealed class BluetoothEndPoint : EndPoint+ {+ public L2CapEndPoint(ReadOnlySpan<byte> macAddress, int port);+ }+ public enum HciChannel+ {+ Raw,+ User,+ Monitor,+ Control,+ Logging+ }
// HCI endpoints are a bit different, they are not used to connect to remote devices over Bluetooth but
// rather provide endpoints to interact with the Bluetooth controller in the local host.
+ public sealed class HciEndPoint : EndPoint+ {+ public HciEndPoint(ReadOnlySpan<byte> adapter, HciChannel channel);+ }
}
API Usage
Set up a L2Cap server which listens on a local Bluetooth adapter:
varadapter=newbyte[]{/* MAC address of a local bluetooth adapter */};varsocket=newSocket(AddressFamily.Bluetooth,SocketType.Seqpacket,ProtocolType.L2Cap);socket.Bind(newBluetoothEndPoint(adapter,0x13));varclient=awaitsocket.AcceptAsync();
@scalablecory I haven't found any. What I've seen so far seems to be focused on Bluetooth LE on iOS/Android.
My use case is to set up a L2Cap server with which Bluetooth devices can interface, on Linux. This involves setting up a L2Cap socket listener.
The main limitation I'm hitting is that the .NET Socket/SocketListener classes don't support AddressFamily.Bluetooth / ProtocolType.L2Cap. The workaround is to P/Invoke into libc. That involves duplicating a lot of the code which already exists in .net, unfortunately.
PS: @ManickaP 's comment about this being non-trivial work may refer to a full-fledged Bluetooth API; that's not what I'm asking/proposing. Having support for Bluetooth sockets would be sufficient.
By P/Invoking into libc functions it is possible to overcome this limitation. However it does involve a lot of reinventing the wheel (https://inthehand.com/2023/08/21/reinventing-the-wheel-again-bluetooth-on-linux/)
I'm working on finishing this implementation - this adds Linux Bluetooth Sockets to the existing support for Windows, Android and iOS (External Accessory devices only). I also have a binding library for IOBluetooth on macOS which needs updating to .NET 6.0. I don't think that Microsoft should add Bluetooth support to .NET because it requires a number of other APIs and structures, however the Socket class on Linux shouldn't limit what address families are used - it should just pass values to the native API and return the correct exception types as required as it does on Windows.
On Windows, If I don't specify the type of the OS in the C# project parameters I get an exception when creating a Bluetooth client (closed Issue inthehand/32feet#332).
When I run the program on Linux I get the same exception but there's no Linux option in the OS type combo box.
Can you help me to use the Bluetooth RFComm in a C# program under Ubuntu ? I need to provide a Stream object to my API that sends/receives data. I found this article https://people.csail.mit.edu/albert/bluez-intro/x502.html which contains a C code but this last depends on libbluetooth
Activity
ManickaP commentedon Aug 5, 2021
Triage: this is non-trivial work . It's not just adding new enum values but also making it work with the OS of choice.
scalablecory commentedon Sep 29, 2021
Does .NET MAUI have Bluetooth APIs that would make sense for you?
qmfrederik commentedon Sep 30, 2021
@scalablecory I haven't found any. What I've seen so far seems to be focused on Bluetooth LE on iOS/Android.
My use case is to set up a L2Cap server with which Bluetooth devices can interface, on Linux. This involves setting up a L2Cap socket listener.
The main limitation I'm hitting is that the .NET Socket/SocketListener classes don't support
AddressFamily.Bluetooth/ProtocolType.L2Cap. The workaround is to P/Invoke into libc. That involves duplicating a lot of the code which already exists in .net, unfortunately.PS: @ManickaP 's comment about this being non-trivial work may refer to a full-fledged Bluetooth API; that's not what I'm asking/proposing. Having support for Bluetooth sockets would be sufficient.
MatthiasJentsch commentedon Jul 6, 2023
@qmfrederik Did you found a workaround via P/Invoke? I'd like to use a .NET Bluetooth socket connection with Linux.
peterfoot commentedon Aug 21, 2023
By P/Invoking into libc functions it is possible to overcome this limitation. However it does involve a lot of reinventing the wheel (https://inthehand.com/2023/08/21/reinventing-the-wheel-again-bluetooth-on-linux/)
I'm working on finishing this implementation - this adds Linux Bluetooth Sockets to the existing support for Windows, Android and iOS (External Accessory devices only). I also have a binding library for IOBluetooth on macOS which needs updating to .NET 6.0. I don't think that Microsoft should add Bluetooth support to .NET because it requires a number of other APIs and structures, however the Socket class on Linux shouldn't limit what address families are used - it should just pass values to the native API and return the correct exception types as required as it does on Windows.
embeddedmz commentedon Sep 5, 2023
Hi @peterfoot
On Windows, If I don't specify the type of the OS in the C# project parameters I get an exception when creating a Bluetooth client (closed Issue inthehand/32feet#332).
When I run the program on Linux I get the same exception but there's no Linux option in the OS type combo box.
Can you help me to use the Bluetooth RFComm in a C# program under Ubuntu ? I need to provide a Stream object to my API that sends/receives data. I found this article https://people.csail.mit.edu/albert/bluez-intro/x502.html which contains a C code but this last depends on libbluetooth
peterfoot commentedon Sep 6, 2023
@embeddedmz I've started a discussion for this as it is off topic for this dotnet runtime issue - inthehand/32feet#342