Skip to content
forked from go-ble/ble

Bluetooth Low Energy for Linux (Nordic Features)

License

Notifications You must be signed in to change notification settings

SensefinityCloud/go-ble

 
 

Repository files navigation

Go BLE Scan Response

Fork of github.com/go-ble/ble

  1. Removed macos support
  2. Added multiple features needed to integrate nordic features

New Features

General Advertisement function

Advertise(ctx context.Context, adv AdvertisementData) error

Struct:

// AdvertisementData represents the advertisement data for a Bluetooth Low Energy device.
type AdvertisementData struct {
	ShortName        string
	CompleteName     string
	ManufacturerData *ManufacturerData
	ScanResponse     *ScanResponse
	Services []UUID
}

type ScanResponse struct {
	ManufacturerData  *ManufacturerData
	IncludeDeviceName bool
}

// ManufacturerData represents the data structure for manufacturer-specific data in BLE advertising packets.
type ManufacturerData struct {
	CompanyId uint16
	Data      []byte
}

eg:

advData := ble.AdvertisementData{
	ShortName: "Example",
	Services:  []ble.UUID{ble.MustParse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")},
	ScanResponse: &ble.ScanResponse{
		ManufacturerData: &ble.ManufacturerData{
			CompanyId: uint16(89),
			Data:      []byte("Example"),
		},
	},
}

// It blocks the current goroutine until the advertising process is finished.
err = ble.Advertise(ctx, advData)
if err != nil {
	panic(err)
}

Added Notifier on HandleWrite

Feature: We can notify other characteristics when receiving new data

rx.HandleWrite(ble.WriteHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter, n func(h uint16, data []byte) (int, error))

eg:

tx.HandleNotify(ble.NotifyHandlerFunc(func(req ble.Request, n ble.Notifier) {
		log.Println("TX Notify handler called")

}))


rx.HandleWrite(ble.WriteHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter, n func(h uint16, data []byte) (int, error)) {
		n(tx.ValueHandle, []byte("ok"))
}))

Advertise Name And Services With ScanResponse

Advertises device name, and specified service UUIDs.
It tres to fit the UUIDs in the advertising packet as much as possible.
Advertises the given manufacturer data in the scan response.

AdvertiseNameAndServicesWithScanResponse(ctx context.Context, name string, companyId uint16, b []byte, uuids ...UUID) error

Go Ble

ble is a Golang Bluetooth Low Energy package for Linux and Mac OS.

Note: Is not being actively maintained, should use the original lib.