aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 611a80bb3f3e93fe340e5fc31df378649edde045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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
```
(Not yet published.)
## Examples

```nim
# Simple discovery example.  
import nimbluez/bluetooth

echo "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/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()
```

```nim
# 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.