go-bluetooth

Golang bluetooth client based on bluez DBus interfaces

Github星跟踪图

go-bluetooth

Go bluetooth API for Bluez DBus interface.

GoDoc

Usage

  1. Build the binary

cd $GOPATH/src/github.com/muka/go-bluetooth && make build

  1. Run the examples eg.

go-bluetooth discovery

The examples/ folder offer an API overview.

Features

The library offers a wrapper to 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 basic API (from a fork of go-ble/ble)
  • Expose bluetooth service from go code
  • Pairing and authentication support (via agent)
  • Basic beaconing (iBeacon and Eddystone)

Setup

The go Bluez API is generated from the documentation, run make gen to re-generate go sources. There is also a commodity bluez JSON file available in the root folder for reference.

Code generation will not overwrite existing files, run make gen/clean to remove generated content.

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.

Note Ensure to install proper dbus rules on the system. For a dev setup, you can use the library configuration as follow

  cd $GOPATH/src/github.com/muka/go-bluetooth
  sudo ln -s `pwd`/scripts/dbus-go-bluetooth-service.conf /etc/dbus-1/system.d/
  sudo ln -s `pwd`/scripts/dbus-go-bluetooth-dev.conf /etc/dbus-1/system.d/
  # Reload dbus to load new policies:
  # via dbus
  # dbus-send --system --type=method_call --dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig
  # via systemctl
  systemctl reload dbus

Requirements

The library is tested with

  • golang 1.11
  • bluez bluetooth v5.50

Development notes

  • Standard GATT characteristics descriptions can be found on https://www.bluetooth.com/specifications/gatt/

  • 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
    
    

Contributing

Feel free to open an issue and/or a PR to contribute. If you would like to help improve the library without coding directly, you can also consider to contribute by providing some hardware to test on.

References

License

Copyright 2019 luca capra

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.

主要指标

概览
名称与所有者muka/go-bluetooth
主编程语言Go
编程语言Go (语言数: 4)
平台
许可证Apache License 2.0
所有者活动
创建于2016-10-14 12:01:17
推送于2024-07-01 04:45:17
最后一次提交2024-07-01 06:45:17
发布数12
最新版本名称bluez-5.66 (发布于 )
第一版名称0.9.0 (发布于 )
用户参与
星数659
关注者数18
派生数132
提交数493
已启用问题?
问题数128
打开的问题数12
拉请求数65
打开的拉请求数6
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?