[ble] Able to connect and retrieve battery notifications

This commit is contained in:
ron 2016-07-04 19:46:42 +02:00
parent e001cce97c
commit 7887140073
3 changed files with 34 additions and 6 deletions

View File

@ -1,8 +1,6 @@
package main
import (
"fmt"
"time"
"os"
"github.com/hybridgroup/gobot"
@ -16,7 +14,7 @@ func main() {
drone := ble.NewBLEMinidroneDriver(bleAdaptor, "drone")
work := func() {
battery.Init()
drone.Init()
}
robot := gobot.NewRobot("bleBot",

View File

@ -108,7 +108,8 @@ func (b *BLEAdaptor) WriteCharacteristic(sUUID string, cUUID string, data []byte
}
characteristic := b.services[sUUID].characteristics[cUUID]
err = b.peripheral.WriteCharacteristic(characteristic, data, false)
err = b.peripheral.WriteCharacteristic(characteristic, data, true)
if err != nil {
fmt.Printf("Failed to write characteristic, err: %s\n", err)
return err
@ -117,6 +118,29 @@ func (b *BLEAdaptor) WriteCharacteristic(sUUID string, cUUID string, data []byte
return
}
// SubscribeNotify subscribes to the BLE device for the
// requested service and characteristic
func (b *BLEAdaptor) SubscribeNotify(sUUID string, cUUID string, f func([]byte, error)) (err error) {
if !b.connected {
log.Fatalf("Cannot subscribe to BLE device until connected")
return
}
characteristic := b.services[sUUID].characteristics[cUUID]
fn := func(c *gatt.Characteristic, b []byte, err error) {
f(b, err)
}
err = b.peripheral.SetNotifyValue(characteristic, fn)
if err != nil {
fmt.Printf("Failed to subscribe to characteristic, err: %s\n", err)
return err
}
return
}
func (b *BLEAdaptor) StateChangeHandler(d gatt.Device, s gatt.State) {
fmt.Println("State:", s)
switch s {

View File

@ -1,7 +1,7 @@
package ble
import (
//"bytes"
"fmt"
"github.com/hybridgroup/gobot"
)
@ -46,6 +46,12 @@ func (b *BLEMinidroneDriver) Halt() (errs []error) { return }
func (b *BLEMinidroneDriver) Init() (err error) {
b.stepsfa0b++
buf := []byte{0x04, byte(b.stepsfa0b), 0x00, 0x04, 0x01, 0x00, 0x32, 0x30, 0x31, 0x34, 0x2D, 0x31, 0x30, 0x2D, 0x32, 0x38, 0x00}
err = b.adaptor().WriteCharacteristic("fa00", "fa0b", buf)
err = b.adaptor().WriteCharacteristic("9a66fa000800919111e4012d1540cb8e", "9a66fa0b0800919111e4012d1540cb8e", buf)
f := func(b []byte, e error) {
fmt.Printf("battery: %d\n", b[len(b)-1])
}
b.adaptor().SubscribeNotify("9a66fb000800919111e4012d1540cb8e", "9a66fb0f0800919111e4012d1540cb8e", f)
return err
}