Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Electric-Blue/NimBluez
Nim modules for access to system Bluetooth resources.
https://github.com/Electric-Blue/NimBluez
Last synced: 2 months ago
JSON representation
Nim modules for access to system Bluetooth resources.
- Host: GitHub
- URL: https://github.com/Electric-Blue/NimBluez
- Owner: Electric-Blue
- License: bsd-2-clause
- Created: 2016-01-06T17:39:30.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-01T18:40:53.000Z (almost 2 years ago)
- Last Synced: 2024-08-03T03:05:18.704Z (6 months ago)
- Language: Nim
- Size: 84 KB
- Stars: 22
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - nimbluez - Nim modules for access to system Bluetooth resources. (Operating System / System API)
README
# NimBluez
Modules for access to system [Bluetooth](https://www.bluetooth.com/) resources for the [Nim](http://nim-lang.org/) 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](http://www.bluez.org/) in `nimbluez/bluez` folder.
For [Microsoft Bluetooth](https://msdn.microsoft.com/en-us/library/windows/desktop/aa362761%28v=vs.85%29.aspx) protocol stack wrappers look at `nimbluez/msbt`.## Installation
To install using [Nimble](https://github.com/nim-lang/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
```nim
# Simple discovery example.
import nimbluez/bluetoothecho "All visible remote devices:"
for remoteDevice in getRemoteDevices():
echo remoteDevice.address, " - ", remoteDevice.name
``````nim
# Simple server example.
# Attention! This code does not contain error handling.
import nimbluez/bluetoothnativesocketsvar 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()
``````nim
# Simple client example.
# Attention! This code does not contain error handling.
import nimbluez/bluetoothnativesocketsvar 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.