bluetooth

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: MIT Imports: 10 Imported by: 0

README

Raw bluetooth for Go

This is a raw bluetooth library which connects to a MAC address by using syscall on Linux and Windows.

Features
  • Implements a simple Read/Write interface.
  • Same behavior on Linux and Windows.
  • Zero-dependency (expect Go's x/sys)
  • Built-in scanner for devices
    • Linux: using the hcitool
    • Windows: no dependency (WSALookupService)
Roadmap
  • Rewrite Linux scan not to use hcitool
  • Add Darwin supports to Communicator
Usage
package main

import (
	"fmt"

	"github.com/infiniteloopcloud/bluetooth-go"
)

func main() {
	// Connect
	conn, err := bluetooth.Connect(bluetooth.Params{
		Address: "58:CF:0A:BB:28:FC",
	})
	if err != nil {
		// handle err
	}

	// Write data into the connection
	_, err = conn.Write([]byte("data"))
	if err != nil {
		// handle err
	}

	// Read from connection
	_, data, err := conn.Read(500)
	if err != nil {
		// handle err
	}
	fmt.Println(string(data))
	
	// Close connection
	err = conn.Close()
	if err != nil {
		// handle err
	}
}
Implement custom logger
package main

import (
	"fmt"

	"github.com/infiniteloopcloud/bluetooth-go"
)

type Logger struct{}

func (Logger) Print(a ...interface{}) {
	fmt.Println(a...)
}

func main() {
	// Connect
	_, err := bluetooth.Connect(bluetooth.Params{
		Address: "58:CF:0A:BB:28:FC",
		Log: Logger{},
	})
	if err != nil {
		// handle err
	}
	//....
}
Scanner
package main

import (
	"fmt"

	"github.com/infiniteloopcloud/bluetooth-go"
)

func main() {
	devices, err := bluetooth.NewScanner().Scan()
	if err != nil {
		// handle err
	}
	fmt.Println(devices)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Communicator

type Communicator interface {
	Read(dataLen int) (int, []byte, error)
	io.Writer
	io.Closer
}

func Connect

func Connect(params Params) (Communicator, error)

type Device

type Device struct {
	Name       string `json:"name"`
	MACAddress string `json:"mac_address"`
}

type Log

type Log interface {
	Print(a ...interface{})
}

type Params

type Params struct {
	Address string
	Log     Log
}

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer is a mock implementation of Communicator

func (Printer) Close

func (p Printer) Close() error

func (Printer) Read

func (p Printer) Read(dataLen int) (int, []byte, error)

func (Printer) Write

func (p Printer) Write(d []byte) (int, error)

type Scanner

type Scanner interface {
	Scan() ([]Device, error)
}

func NewScanner added in v0.0.2

func NewScanner() Scanner

Jump to

Keyboard shortcuts

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