Example #1
0
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
	var matched bool = false

	if len(deviceName) > 0 {
		matched = a.LocalName == deviceName
		if matched == false {
			return
		}
	}

	if len(deviceAddress) > 0 {
		var deviceAddrArr [6]byte
		copy(deviceAddrArr[:], deviceAddress[0:6])
		matched = a.Address == deviceAddrArr && a.AddressType == deviceAddressType
	}

	if matched == true {
		log.Debugf("Peripheral Discovered: %s, Address:%+v Address Type:%+v",
			p.Name(), a.Address, a.AddressType)
		p.Device().StopScanning()
		p.Device().Connect(p)
	}
}
Example #2
0
func onPeriphConnected(p gatt.Peripheral, err error) {
	log.Debugf("Peripheral Connected")

	services, err := p.DiscoverServices(nil)
	if err != nil {
		log.Debugf("Failed to discover services, err: %s", err)
		return
	}

	var isCoap bool = false

	for _, service := range services {

		if service.UUID().Equal(newtmgrServiceId) ||
			service.UUID().Equal(newtmgrCoapServiceId) {
			log.Debugf("Newtmgr Service Found %s", service.Name())

			if service.UUID().Equal(newtmgrCoapServiceId) {
				isCoap = true
			}

			cs, _ := p.DiscoverCharacteristics(nil, service)

			for _, c := range cs {
				if (isCoap == false &&
					c.UUID().Equal(newtmgrServiceCharId)) ||
					(isCoap == true &&
						c.UUID().Equal(newtmgrCoapServiceCharId)) {
					log.Debugf("Newtmgr Characteristic Found")
					p.SetNotifyValue(c, newtmgrNotifyCB)
					deviceChar = c
					devicePerph = p
					p.SetMTU(BleMTU)
					<-CharDisc
				}
			}
		}
	}
}