func main()

in nmxact/example/ble_loop/ble_loop.go [69:160]


func main() {
	nmxutil.SetLogLevel(log.DebugLevel)
	//nmxutil.SetLogLevel(log.InfoLevel)

	// Initialize the BLE transport.
	params := nmble.NewXportCfg()
	params.SockPath = "/tmp/blehostd-uds"
	params.BlehostdPath = "blehostd"
	params.DevPath = "/dev/cu.usbmodem142141"

	x, err := nmble.NewBleXport(params)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating BLE transport: %s\n",
			err.Error())
		os.Exit(1)
	}

	// Start the BLE transport.
	if err := x.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "error starting BLE transport: %s\n",
			err.Error())
		os.Exit(1)
	}
	defer x.Stop()

	// Find a device to connect to:
	//     * Peer has name "nimble-bleprph"
	//     * We use a random address.
	dev, err := nmble.DiscoverDeviceWithName(
		x, bledefs.BLE_ADDR_TYPE_RANDOM, 10*time.Second, "c4")
	if err != nil {
		fmt.Fprintf(os.Stderr, "error discovering device: %s\n", err.Error())
		os.Exit(1)
	}
	if dev == nil {
		fmt.Fprintf(os.Stderr, "couldn't find device")
		os.Exit(1)
	}

	// Prepare a BLE session:
	//     * Plain NMP (not tunnelled over OIC).
	//     * We use a random address.
	sc := sesn.NewSesnCfg()
	sc.MgmtProto = sesn.MGMT_PROTO_OMP
	sc.Ble.OwnAddrType = bledefs.BLE_ADDR_TYPE_RANDOM
	sc.PeerSpec.Ble = *dev

	s, err := x.BuildSesn(sc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating BLE session: %s\n", err.Error())
		os.Exit(1)
	}

	configExitHandler(x, s)

	// Repeatedly:
	//     * Connect to peer if unconnected.
	//     * Send an echo command to peer.
	//
	// If blehostd crashes or the controller is unplugged, nmxact should
	// recover on the next connect attempt.
	for {
		if !s.IsOpen() {
			// Connect to the peer (open the session).
			if err := s.Open(); err != nil {
				fmt.Fprintf(os.Stderr, "error starting BLE session: %s\n",
					err.Error())
				time.Sleep(time.Second)
				continue
			}
		}

		// Send an echo command to the peer.
		c := xact.NewEchoCmd()
		c.Payload = "hello"

		res, err := c.Run(s)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error executing echo command: %s\n",
				err.Error())
			continue
		}

		if res.Status() != 0 {
			fmt.Printf("Peer responded negatively to echo command; status=%d\n",
				res.Status())
		}

		eres := res.(*xact.EchoResult)
		fmt.Printf("Peer echoed back: %s\n", eres.Rsp.Payload)
	}
}