| 1 | # Package gatt provides a Bluetooth Low Energy GATT implementation. |
| 2 | |
| 3 | Gatt (Generic Attribute Profile) is the protocol used to write BLE peripherals (servers) and centrals (clients). |
| 4 | |
| 5 | As a peripheral, you can create services, characteristics, and descriptors, |
| 6 | advertise, accept connections, and handle requests. |
| 7 | |
| 8 | As a central, you can scan, connect, discover services, and make requests. |
| 9 | |
| 10 | ## SETUP |
| 11 | |
| 12 | ### gatt supports both Linux and OS X. |
| 13 | |
| 14 | ### On Linux: |
| 15 | To gain complete and exclusive control of the HCI device, gatt uses |
| 16 | HCI_CHANNEL_USER (introduced in Linux v3.14) instead of HCI_CHANNEL_RAW. |
| 17 | Those who must use an older kernel may patch in these relevant commits |
| 18 | from Marcel Holtmann: |
| 19 | |
| 20 | Bluetooth: Introduce new HCI socket channel for user operation |
| 21 | Bluetooth: Introduce user channel flag for HCI devices |
| 22 | Bluetooth: Refactor raw socket filter into more readable code |
| 23 | |
| 24 | Note that because gatt uses HCI_CHANNEL_USER, once gatt has opened the |
| 25 | device no other program may access it. |
| 26 | |
| 27 | Before starting a gatt program, make sure that your BLE device is down: |
| 28 | |
| 29 | sudo hciconfig |
| 30 | sudo hciconfig hci0 down # or whatever hci device you want to use |
| 31 | |
| 32 | If you have BlueZ 5.14+ (or aren't sure), stop the built-in |
| 33 | bluetooth server, which interferes with gatt, e.g.: |
| 34 | |
| 35 | sudo service bluetooth stop |
| 36 | |
| 37 | Because gatt programs administer network devices, they must |
| 38 | either be run as root, or be granted appropriate capabilities: |
| 39 | |
| 40 | sudo <executable> |
| 41 | # OR |
| 42 | sudo setcap 'cap_net_raw,cap_net_admin=eip' <executable> |
| 43 | <executable> |
| 44 | |
| 45 | ## Usage |
| 46 | Please see [godoc.org](http://godoc.org/github.com/paypal/gatt) for documentation. |
| 47 | |
| 48 | ## Examples |
| 49 | |
| 50 | ### Build and run the examples on a native environment (Linux or OS X) |
| 51 | |
| 52 | Go is a compiled language, which means to run the examples you need to build them first. |
| 53 | |
| 54 | # Build the sample server. |
| 55 | go build examples/server.go |
| 56 | # Start the sample server. |
| 57 | sudo ./server |
| 58 | |
| 59 | Alternatively, you can use "go run" to build and run the examples in a single step: |
| 60 | |
| 61 | # Build and run the sample server. |
| 62 | sudo go run examples/server.go |
| 63 | |
| 64 | Discoverer and explorer demonstrates central (client) functions: |
| 65 | |
| 66 | # Discover surrounding peripherals. |
| 67 | sudo go run examples/discoverer.go |
| 68 | |
| 69 | # Connect to and explorer a peripheral device. |
| 70 | sudo go run examples/explorer.go <peripheral ID> |
| 71 | |
| 72 | ### Cross-compile and deploy to a target device |
| 73 | |
| 74 | # Build and run the server example on a ARMv5 target device. |
| 75 | GOARCH=arm GOARM=5 GOOS=linux go build examples/server.go |
| 76 | cp server <target device> |
| 77 | # Start the server on the target device |
| 78 | sudo ./server |
| 79 | |
| 80 | See the server.go, discoverer.go, and explorer.go in the examples/ |
| 81 | directory for writing server or client programs that run on Linux |
| 82 | and OS X. |
| 83 | |
| 84 | Users, especially on Linux platforms, seeking finer-grained control |
| 85 | over the devices can see the examples/server_lnx.go for the usage |
| 86 | of Option, which are platform specific. |
| 87 | |
| 88 | See the rest of the docs for other options and finer-grained control. |
| 89 | |
| 90 | ## Note |
| 91 | Note that some BLE central devices, particularly iOS, may aggressively |
| 92 | cache results from previous connections. If you change your services or |
| 93 | characteristics, you may need to reboot the other device to pick up the |
| 94 | changes. This is a common source of confusion and apparent bugs. For an |
| 95 | OS X central, see http://stackoverflow.com/questions/20553957. |
| 96 | |
| 97 | ## Known Issues |
| 98 | |
| 99 | Currently OS X vesion does not support subscribing to indications. |
| 100 | Please check [#32](https://github.com/paypal/gatt/issues/32) for the status of this issue. |
| 101 | |
| 102 | ## REFERENCES |
| 103 | |
| 104 | gatt started life as a port of bleno, to which it is indebted: |
| 105 | https://github.com/sandeepmistry/bleno. If you are having |
| 106 | problems with gatt, particularly around installation, issues |
| 107 | filed with bleno might also be helpful references. |
| 108 | |
| 109 | To try out your GATT server, it is useful to experiment with a |
| 110 | generic BLE client. LightBlue is a good choice. It is available |
| 111 | free for both iOS and OS X. |
| 112 | |
| 113 | gatt is similar to [bleno](https://github.com/sandeepmistry/bleno) and [noble](https://github.com/sandeepmistry/noble), which offer BLE GATT implementations for node.js. |
| 114 | |
| 115 | Gatt is released under a [BSD-style license](./LICENSE.md). |