美文网首页
使用github.com/paypal/gatt库接收EddyS

使用github.com/paypal/gatt库接收EddyS

作者: 路过麦田 | 来源:发表于2019-05-08 17:18 被阅读0次

gatt对于google的EddyStone广播支持的不是很好,在adv.go文件中的

func (a *Advertisement) unmarshall(b []byte) error {
    ...
}

其中EddyStone的ServiceData数据被注释掉了,而且作者也并没有去处理该数据

    // case typeServiceData16,
    // case typeServiceData32,
    // case typeServiceData128:

所以只能自己写代码处理:

case typeServiceData16:
    a.ServiceData = make([]ServiceData, 0)
    a.ServiceData = append(a.ServiceData, ServiceData{UUID{d[:2]}, d[2:]})
// case typeServiceData32,
// case typeServiceData128:

重新编译运行之后即可在onPeripheralDiscovered方法中处理得到的数据:


func onPeripheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
    if len(a.ServiceData) > 0 {
        // log.Debug("a.ServiceData:", a.ServiceData)
        uuid := a.ServiceData[0].UUID
        data := a.ServiceData[0].Data
        ...
    }
}

还有一点需要注意,gatt库只提供了 device.StopScanning() 方法,并没有向外暴露出断开与蓝牙设备之间的连接的方法,其实这个方法是存在的,在device_linux.go文件中存在Stop方法,可以关闭连接,并且触发回调方法:

func (d *device) Stop() error {
    d.state = StatePoweredOff
    defer d.stateChanged(d, d.state)
    return d.hci.Close()
}

但是这个方法对外是不可见的,所以需要用到golang的反射机制来调用该方法来关闭device:

func StopBle() {
    if device != nil {
        t := reflect.ValueOf(device)
        m := t.MethodByName("Stop")
        m.Call([]reflect.Value{})
        device = nil
    }
}

相关文章

网友评论

      本文标题:使用github.com/paypal/gatt库接收EddyS

      本文链接:https://www.haomeiwen.com/subject/kcevoqtx.html