No description
Find a file
2023-04-01 21:40:53 +03:00
examples Minor change in indentation. 2016-02-23 18:00:53 +03:00
nimbluez Delete bluetoothnativesockets.nim~ 2023-03-27 19:52:09 +02:00
.gitignore Update on documentation. 2016-01-26 01:46:00 +03:00
LICENSE Update on documentation. 2016-01-26 01:46:00 +03:00
nimbluez.nimble NIm v1.0.0 syntax fixes. 2020-02-09 11:23:59 +03:00
README.md Documentation update. 2020-03-20 22:51:03 -07:00

NimBluez

Modules for access to system Bluetooth resources for the Nim programming language.

Use nimbluez/bluetooth module for cross-platform discovery and managing Bluetooth devices and services.

For cross-platform low-level sockets interface implementation use nimbluez/bluetoothnativesockets.

You can find wrappers for BlueZ in nimbluez/bluez folder. For Microsoft Bluetooth protocol stack wrappers look at nimbluez/msbt.

Installation

To install using Nimble run the following:

$ nimble install nimbluez

Linux

You may need to install libbluetooth-dev, if you see error could not load: libbluetooth.so on application start.

# for Ubuntu
 sudo apt install libbluetooth-dev

Examples

# Simple discovery example.  
import nimbluez/bluetooth

echo "All visible remote devices:"
for remoteDevice in getRemoteDevices():
  echo remoteDevice.address, " - ", remoteDevice.name
# Simple server example.
# Attention! This code does not contain error handling.
import nimbluez/bluetoothnativesockets

var serverSocket = newBluetoothNativeSocket(SOCK_STREAM, BTPROTO_RFCOMM)
var name = getRfcommAddr(RfcommPort(1))
discard bindAddr(serverSocket,
                 cast[ptr SockAddr](addr(name)),
                 sizeof(name).SockLen)
discard serverSocket.listen()
var
  clientName = getRfcommAddr()
  clientNameLen = sizeof(clientName).SockLen
var clientSocket = accept(serverSocket,
                          cast[ptr SockAddr](addr(clientName)),
                          addr(clientNameLen))
var message: string = ""
message.setLen(1000)
let recvLen = clientSocket.recv(cstring(message), cint(message.len), cint(0))
message.setLen(recvLen)
echo message
clientSocket.close()
serverSocket.close()
# Simple client example.
# Attention! This code does not contain error handling.
import nimbluez/bluetoothnativesockets

var socket = newBluetoothNativeSocket(SOCK_STREAM, BTPROTO_RFCOMM)
var name = getRfcommAddr(RfcommPort(1), "00:02:72:0F:5C:87")
discard connect(socket, cast[ptr SockAddr](addr(name)), sizeof(name).SockLen)
var message = "Hi there!"
discard send(socket, cstring(message), cint(message.len), cint(0))
socket.close()

For more examples look at examples folder.