使用github.com/paypal/gatt庫接收EddyStone廣播


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

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

其中EddyStone的ServiceData數(shù)據(jù)被注釋掉了,而且作者也并沒有去處理該數(shù)據(jù)

    // 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方法中處理得到的數(shù)據(jù):


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方法,可以關閉連接,并且觸發(fā)回調(diào)方法:

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

但是這個方法對外是不可見的,所以需要用到golang的反射機制來調(diào)用該方法來關閉device:

func StopBle() {
    if device != nil {
        t := reflect.ValueOf(device)
        m := t.MethodByName("Stop")
        m.Call([]reflect.Value{})
        device = nil
    }
}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容