api-native

module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT

README

Go Reference

api-native

This is a library which provides multi-platform support to handle Bluetooth Classic functionality. Since this is an alpha release, expect the API to change at any time.

Currently, support is present for the following platforms:

  • Linux
  • Windows

Funding

This project is funded through NGI Zero Core, a fund established by NLnet with financial support from the European Commission's Next Generation Internet program. Learn more at the NLnet project page.

NLnet foundation logo NGI Zero Logo

Dependencies

  • Linux

    • Bluez
    • DBus
    • NetworkManager (optional, required for PANU)
    • ModemManager (optional, required for DUN)
    • PulseAudio (optional, required to manage device audio profiles)
  • Windows

Documentation

The documentation is not very extensive right now, more documentation will be added later. See the package reference for more information.

Notes:

  • For Windows, currently only pairing, connection (automatic and profile based), device discovery, and OBEX Object PUSH functionality is implemented.

Sample Usage

package main

import (
	"fmt"

	"github.com/bluetuith-org/api-native/api/bluetooth"
	"github.com/bluetuith-org/api-native/api/config"
	"github.com/bluetuith-org/api-native/session"
	"github.com/google/uuid"
)

func main() {
    // Initialize a session configuration.
	cfg := config.New()

    // Create a new session.
	session := session.NewSession()

    // Attempt to start the session, and if it returns with no errors,
    // it will provide the supported features and platform information of the session.
    // The autHandler struct handles all authentication requests, like for pairing or file transfer.
	featureSet, pinfo, err := session.Start(&authHandler{}, cfg)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer session.Stop()

    // Listen for any device events when devices are added to or removed from the system or its properties are updated.
	go func() {
		id := bluetooth.DeviceEvent().Subscribe()
		if !id.Subscribable {
			return
		}

		for device := range id.C {
			fmt.Println(device.Data)
		}
	}()

    // Listen for any adapter events when adapters are added to or removed from the system or its properties are updated.
    go func() {
		id := bluetooth.AdapterEvent().Subscribe()
		if !id.Subscribable {
			return
		}

		for adapter := range id.C {
			fmt.Println(adapter.Data)
		}
	}()

    // Print the platform information.
	fmt.Println(pinfo)

    // Print the supported features of the session.
	fmt.Println(featureSet.Supported.String())
    
    // Get a list of adapters.
    adapters := session.Adapters()
	for _, adapter := range adapters {
		fmt.Println(adapter.Name)
	}
	if len(adapters) == 0 {
		panic("no adapters found")
	}

    // Select an adapter from the list.
	selectedAdapter := session.Adapter(adapters[0].Address)

    // Start discovering devices on the selected adapter.
    // All discovered devices will be sent as device events.
	err = selectedAdapter.StartDiscovery()
	if err != nil {
		panic(err)
	}

	time.Sleep(10000)

    // Stop discovering devices on the selected adapter.
	err = selectedAdapter.StopDiscovery()
	if err != nil {
		panic(err)
	}
}

// authHandler provides a custom SessionAuthorizer based implementation to handle authentication requests,
// like for pairing or incoming file transfers. For each of the handler functions, return an error if
// the authentication request should be denied.
type authHandler struct {
}

func (a *authHandler) AuthorizeTransfer(timeout bluetooth.AuthTimeout, props bluetooth.FileTransferData) error {
    fmt.Println(props)
	return nil
}

func (a *authHandler) DisplayPinCode(timeout bluetooth.AuthTimeout, address bluetooth.MacAddress, pincode string) error {
    return errors.New("do not authenticate")
}

func (a *authHandler) DisplayPasskey(timeout bluetooth.AuthTimeout, address bluetooth.MacAddress, passkey uint32, entered uint16) error {
    fmt.Println(passkey)
	return nil
}

func (a *authHandler) ConfirmPasskey(timeout bluetooth.AuthTimeout, address bluetooth.MacAddress, passkey uint32) error {
	fmt.Println(passkey)

	return nil
}

func (a *authHandler) AuthorizePairing(timeout bluetooth.AuthTimeout, address bluetooth.MacAddress) error {
	return nil
}

func (a *authHandler) AuthorizeService(timeout bluetooth.AuthTimeout, address bluetooth.MacAddress, uuid uuid.UUID) error {
	return nil
}

Directories

Path Synopsis
api
appfeatures
Package appfeatures provides a mechanism for an application to advertise its supported capabilities to the user.
Package appfeatures provides a mechanism for an application to advertise its supported capabilities to the user.
bluetooth
Package bluetooth provides all the core types and related interfaces for adapters, devices and session managers.
Package bluetooth provides all the core types and related interfaces for adapters, devices and session managers.
config
Package config provides a general purpose configuration for a session.
Package config provides a general purpose configuration for a session.
errorkinds
Package errorkinds defines various session-specific generic errors.
Package errorkinds defines various session-specific generic errors.
eventbus
Package eventbus provides a publish/subscribe mechanism to send/listen to events.
Package eventbus provides a publish/subscribe mechanism to send/listen to events.
helpers/sessionstore
Package sessionstore provides a cache to store and access adapter and device data using their respective Bluetooth addresses.
Package sessionstore provides a cache to store and access adapter and device data using their respective Bluetooth addresses.
platforminfo
Package platforminfo provides methods to describe the Bluetooth Stack and the Operating System information.
Package platforminfo provides methods to describe the Bluetooth Stack and the Operating System information.
Package linux provides an implementation of a Linux-specific Bluetooth session.
Package linux provides an implementation of a Linux-specific Bluetooth session.
internal/dbushelper
Package dbushelper provides DBus specific helpers to: - Translate DBus paths to Bluetooth addresses.
Package dbushelper provides DBus specific helpers to: - Translate DBus paths to Bluetooth addresses.
networkmanager
Package networkmanager provides functionality to tether the host adapter to a device's internet connection via the NetworkManager application.
Package networkmanager provides functionality to tether the host adapter to a device's internet connection via the NetworkManager application.
obex
Package obex provides functionality to interact with the Bluetooth OBEX profile of a device, by interfacing with the Bluez Obex DBus session bus (org.bluez.obex).
Package obex provides functionality to interact with the Bluetooth OBEX profile of a device, by interfacing with the Bluez Obex DBus session bus (org.bluez.obex).
Package session provides OS platform-specific Bluetooth sessions.
Package session provides OS platform-specific Bluetooth sessions.
Package shim provides an implementation of a platform-agnostic Bluetooth session (provided that the "bluetuith-shim" binary is available).
Package shim provides an implementation of a platform-agnostic Bluetooth session (provided that the "bluetuith-shim" binary is available).
internal/commands
Package commands provides utilities to create and execute functions on a remote RPC server.
Package commands provides utilities to create and execute functions on a remote RPC server.
internal/events
Package events provides utilities to handle raw server-sent events.
Package events provides utilities to handle raw server-sent events.
internal/serde
Package serde provides utilities to marshal to/unmarshal from JSON data.
Package serde provides utilities to marshal to/unmarshal from JSON data.

Jump to

Keyboard shortcuts

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