go-bluetooth

module
v0.0.0-...-dfdf79b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: Apache-2.0

README

go-bluetooth

Go bluetooth API for Linux-based Bluez DBus interface.

GoDoc

Bluez API versioning

The version on master tracks Ubuntu bluetoothd version (v5.64). There are dedicated branches/tags with various version

See branches for available versions.

Features

The library is a wrapper to the Bluez DBus API and some high level API to ease the interaction.

High level features supported:

  • Client code generation from bluez documentation
  • Shell wrappers for rfkill, btmgmt, hciconfig, hcitool
  • An hci socket basic API (inspired by go-ble/ble)
  • Expose bluetooth service from go code [unstable]
  • Pairing and authentication support (via agent)
  • Beaconing send & receive (iBeacon and Eddystone)
  • Mesh API support (since v5.53)

Todo

  • Generate dbus-compatible XML from documentation
  • Generate mock service to test the library against

Using this library

Want to add your project? Please, add it to the README.md and open a PR!

Running examples

Examples are available in _examples folder.

cd _examples
go run main.go
# print available example commands
# Example discovery
go run main.go discovery

Development setup

  1. Clone the repository

git clone https://github.com/muka/go-bluetooth.git

  1. Retrieve the bluetooth API and generate GO code
make bluez/init bluez/checkout gen/clean gen/run

Code generation

To generate code for a new version of bluez use the command

BLUEZ_VERSION=5.65 make gen/clean gen

Change to a version available in bluez. Note that generated code may be broken or incomplete and the generation script may need fine tuning.

How generation works

The code structure follow this pattern:

  • ./api contains wrappers for the DBus Api
  • ./bluez contains the actual implementation, generated from the bluez documentation

Use make gen to re-generate go sources. There is also a commodity bluez JSON file available in the root folder for reference.

Generated code has gen_ prefix. If an API file exists with the same filename but without the prefix, generation will be skipped for that API.

Requirements

The library is tested with

  • golang 1.17.5
  • bluez bluetooth v5.50, v5.54, v5.60
Development notes
  • Inspect a service ObjectManager

    dbus-send --system --print-reply --dest=go.bluetooth /hci0/apps/0 org.freedesktop.DBus.ObjectManager.GetManagedObjects
    
  • Retrieve char properties

    dbus-send --system --print-reply --dest=go.bluetooth /hci0/apps/0/service000003e8/char0  org.freedesktop.DBus.Properties.GetAll string:org.bluez.GattCharacteristic1
    
  • Give access to hciconfig to any user and avoid sudo (may have security implications)

    sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hciconfig`
    
  • Monitor Bluetooth activity

    sudo btmon

  • Monitor DBus activity

    sudo dbus-monitor --system "type=error"

  • Start bluetoothd with experimental features and verbose debug messages make bluetoothd

  • Enable LE advertisement (on a single pc ensure to use at least 2x bluetooth adapter)

    
      sudo btmgmt -i 0 power off
      sudo btmgmt -i 0 name "my go app"
      sudo btmgmt -i 0 le on    
      sudo btmgmt -i 0 connectable on
      sudo btmgmt -i 0 advertising on
      sudo btmgmt -i 0 power on
    
    

References

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis
_examples module
api
package api wraps an high level API to simplify interaction
package api wraps an high level API to simplify interaction
profile/adapter
BlueZ D-Bus Adapter API description [adapter-api.txt]
BlueZ D-Bus Adapter API description [adapter-api.txt]
profile/admin_policy
BlueZ D-Bus Admin Policy API description [admin-policy-api.txt] This API provides methods to control the behavior of bluez as an administrator.
BlueZ D-Bus Admin Policy API description [admin-policy-api.txt] This API provides methods to control the behavior of bluez as an administrator.
profile/advertisement_monitor
BlueZ D-Bus Advertisement Monitor API Description [advertisement-monitor-api.txt] This API allows an client to specify a job of monitoring advertisements by registering the root of hierarchy and then exposing advertisement monitors under the root with filtering conditions, thresholds of RSSI and timers of RSSI thresholds.
BlueZ D-Bus Advertisement Monitor API Description [advertisement-monitor-api.txt] This API allows an client to specify a job of monitoring advertisements by registering the root of hierarchy and then exposing advertisement monitors under the root with filtering conditions, thresholds of RSSI and timers of RSSI thresholds.
profile/advertising
BlueZ D-Bus LE Advertising API Description [advertising-api.txt] Advertising packets are structured data which is broadcast on the LE Advertising channels and available for all devices in range.
BlueZ D-Bus LE Advertising API Description [advertising-api.txt] Advertising packets are structured data which is broadcast on the LE Advertising channels and available for all devices in range.
profile/agent
BlueZ D-Bus Agent API description [agent-api.txt]
BlueZ D-Bus Agent API description [agent-api.txt]
profile/battery
BlueZ D-Bus Battery API description [battery-api.txt]
BlueZ D-Bus Battery API description [battery-api.txt]
profile/device
BlueZ D-Bus Device API description [device-api.txt]
BlueZ D-Bus Device API description [device-api.txt]
profile/gatt
BlueZ D-Bus GATT API description [gatt-api.txt] GATT local and remote services share the same high-level D-Bus API.
BlueZ D-Bus GATT API description [gatt-api.txt] GATT local and remote services share the same high-level D-Bus API.
profile/health
BlueZ D-Bus Health API description [health-api.txt]
BlueZ D-Bus Health API description [health-api.txt]
profile/input
BlueZ D-Bus Input API description [input-api.txt]
BlueZ D-Bus Input API description [input-api.txt]
profile/media
BlueZ D-Bus Media API description [media-api.txt]
BlueZ D-Bus Media API description [media-api.txt]
profile/mesh
BlueZ D-Bus Mesh API description [mesh-api.txt]
BlueZ D-Bus Mesh API description [mesh-api.txt]
profile/network
BlueZ D-Bus Network API description [network-api.txt]
BlueZ D-Bus Network API description [network-api.txt]
profile/obex
OBEX D-Bus API description [obex-api.txt]
OBEX D-Bus API description [obex-api.txt]
profile/obex_agent
OBEX D-Bus Agent API description [obex-agent-api.txt]
OBEX D-Bus Agent API description [obex-agent-api.txt]
profile/profile
BlueZ D-Bus Profile API description [profile-api.txt]
BlueZ D-Bus Profile API description [profile-api.txt]
profile/sap
BlueZ D-Bus Sim Access API description [sap-api.txt]
BlueZ D-Bus Sim Access API description [sap-api.txt]
profile/thermometer
BlueZ D-Bus Thermometer API description [thermometer-api.txt]
BlueZ D-Bus Thermometer API description [thermometer-api.txt]
devices
examples
btmgmt
Example use of the btmgmt wrapper
Example use of the btmgmt wrapper
discovery
shows how to watch for new devices and list them
shows how to watch for new devices and list them
gen
hw
*
*

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL