mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
Simple implementation that can read data
This commit is contained in:
parent
b662559ae9
commit
9de106d089
@ -52,7 +52,7 @@ func (b *BLEBatteryDriver) Halt() (errs []error) { return }
|
||||
func (b *BLEBatteryDriver) GetBatteryLevel() (level uint8) {
|
||||
var l uint8
|
||||
c, _ := b.adaptor().ReadCharacteristic("180f", "2a19")
|
||||
buf := bytes.NewBuffer(<-c)
|
||||
buf := bytes.NewBuffer(c)
|
||||
val, _ := buf.ReadByte()
|
||||
l = uint8(val)
|
||||
return l
|
||||
|
@ -81,12 +81,8 @@ func (b *BLEAdaptor) Reconnect() (errs []error) {
|
||||
|
||||
// Disconnect terminates the connection to the BLE peripheral. Returns true on successful disconnect.
|
||||
func (b *BLEAdaptor) Disconnect() (errs []error) {
|
||||
// if a.connected {
|
||||
// if err := a.sp.Close(); err != nil {
|
||||
// return []error{err}
|
||||
// }
|
||||
// a.connected = false
|
||||
// }
|
||||
b.peripheral.Device().CancelConnection(b.peripheral)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -97,95 +93,20 @@ func (b *BLEAdaptor) Finalize() (errs []error) {
|
||||
|
||||
// ReadCharacteristic returns bytes from the BLE device for the
|
||||
// requested service and characteristic
|
||||
func (b *BLEAdaptor) ReadCharacteristic(sUUID string, cUUID string) (data chan []byte, err error) {
|
||||
//defer b.peripheral.Device().CancelConnection(b.peripheral)
|
||||
fmt.Println("ReadCharacteristic")
|
||||
func (b *BLEAdaptor) ReadCharacteristic(sUUID string, cUUID string) (data []byte, err error) {
|
||||
if !b.connected {
|
||||
log.Fatalf("Cannot read from BLE device until connected")
|
||||
return
|
||||
}
|
||||
|
||||
c := make(chan []byte)
|
||||
b.performRead(c, sUUID, cUUID)
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (b *BLEAdaptor) performRead(c chan []byte, sUUID string, cUUID string) {
|
||||
fmt.Println("performRead")
|
||||
characteristic := b.services[sUUID].characteristics[cUUID]
|
||||
|
||||
val, err := b.peripheral.ReadCharacteristic(characteristic)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
||||
c <- []byte{}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf(" value %x | %q\n", val, val)
|
||||
c <- val
|
||||
}
|
||||
|
||||
func (b *BLEAdaptor) getPeripheral() {
|
||||
|
||||
}
|
||||
|
||||
func (b *BLEAdaptor) getService(sUUID string) (service *gatt.Service) {
|
||||
fmt.Println("getService")
|
||||
ss, err := b.Peripheral().DiscoverServices(nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to discover services, err: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("service")
|
||||
|
||||
for _, s := range ss {
|
||||
msg := "Service: " + s.UUID().String()
|
||||
if len(s.Name()) > 0 {
|
||||
msg += " (" + s.Name() + ")"
|
||||
}
|
||||
fmt.Println(msg)
|
||||
|
||||
id := strings.ToUpper(s.UUID().String())
|
||||
if strings.ToUpper(sUUID) != id {
|
||||
continue
|
||||
}
|
||||
|
||||
msg = "Found Service: " + s.UUID().String()
|
||||
if len(s.Name()) > 0 {
|
||||
msg += " (" + s.Name() + ")"
|
||||
}
|
||||
fmt.Println(msg)
|
||||
return s
|
||||
}
|
||||
|
||||
fmt.Println("getService: none found")
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BLEAdaptor) getCharacteristic(s *gatt.Service, cUUID string) (c *gatt.Characteristic) {
|
||||
fmt.Println("getCharacteristic")
|
||||
cs, err := b.Peripheral().DiscoverCharacteristics(nil, s)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, char := range cs {
|
||||
id := strings.ToUpper(char.UUID().String())
|
||||
if strings.ToUpper(cUUID) != id {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := " Found Characteristic " + char.UUID().String()
|
||||
if len(char.Name()) > 0 {
|
||||
msg += " (" + char.Name() + ")"
|
||||
}
|
||||
msg += "\n properties " + char.Properties().String()
|
||||
fmt.Println(msg)
|
||||
return char
|
||||
}
|
||||
|
||||
return nil
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (b *BLEAdaptor) onStateChanged(d gatt.Device, s gatt.State) {
|
||||
|
51
platforms/ble/device_information.go
Normal file
51
platforms/ble/device_information.go
Normal file
@ -0,0 +1,51 @@
|
||||
package ble
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.Driver = (*BLEDeviceInformationDriver)(nil)
|
||||
|
||||
type BLEDeviceInformationDriver struct {
|
||||
name string
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewBLEDeviceInformationDriver creates a BLEDeviceInformationDriver
|
||||
// by name
|
||||
func NewBLEDeviceInformationDriver(a *BLEAdaptor, name string) *BLEDeviceInformationDriver {
|
||||
n := &BLEDeviceInformationDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
func (b *BLEDeviceInformationDriver) Connection() gobot.Connection { return b.connection }
|
||||
func (b *BLEDeviceInformationDriver) Name() string { return b.name }
|
||||
|
||||
// adaptor returns BLE adaptor for this device
|
||||
func (b *BLEDeviceInformationDriver) adaptor() *BLEAdaptor {
|
||||
return b.Connection().(*BLEAdaptor)
|
||||
}
|
||||
|
||||
// Start tells driver to get ready to do work
|
||||
func (b *BLEDeviceInformationDriver) Start() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stops driver (void)
|
||||
func (b *BLEDeviceInformationDriver) Halt() (errs []error) { return }
|
||||
|
||||
func (b *BLEDeviceInformationDriver) GetModelNumber() (model string) {
|
||||
var l string
|
||||
c, _ := b.adaptor().ReadCharacteristic("180a", "2a24")
|
||||
buf := bytes.NewBuffer(c)
|
||||
val, _ := buf.ReadByte()
|
||||
l = string(val)
|
||||
return l
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user