| package main |
| |
| import ( |
| "crypto/sha256" |
| "log" |
| "mojo/public/go/application" |
| "mojo/public/go/bindings" |
| "mojo/public/go/system" |
| "mojom/v.io/x/ref/services/discovery/ble/ble" |
| ) |
| |
| //#include "mojo/public/c/system/types.h" |
| import "C" |
| |
| type responseHandler struct{} |
| |
| func (r *responseHandler) Found(a ble.Advertisement) error { |
| log.Println("Found:", a) |
| return nil |
| } |
| |
| func (r *responseHandler) Lost(a ble.Advertisement) error { |
| log.Println("Removed:", a) |
| return nil |
| } |
| |
| type scannerDelegate struct { |
| bleHandlerProxy *ble.PluginStopper_Proxy |
| stubs []*bindings.Stub |
| } |
| |
| func (s *scannerDelegate) Initialize(ctx application.Context) { |
| hash := sha256.Sum256([]byte("foobar")) |
| uuid := hash[:16] |
| hash = sha256.Sum256([]byte("key1")) |
| // key1 := hex.EncodeToString(hash[:16]) |
| bleRequest, blePointer := ble.CreateMessagePipeForV23Ble() |
| ctx.ConnectToApplication("https://mojo.v.io/ble.mojo").ConnectToService(&bleRequest) |
| bleProxy := ble.NewV23BleProxy(blePointer, bindings.GetAsyncWaiter()) |
| cbRequest, cbPointer := ble.CreateMessagePipeForPluginScanHandler() |
| stub := ble.NewPluginScanHandlerStub(cbRequest, &responseHandler{}, bindings.GetAsyncWaiter()) |
| s.stubs = append(s.stubs, stub) |
| go func() { |
| for { |
| if err := stub.ServeRequest(); err != nil { |
| connErr, ok := err.(*bindings.ConnectionError) |
| if !ok || !connErr.Closed() { |
| log.Println(err) |
| } |
| break |
| } |
| } |
| }() |
| log.Println("Calling scanner") |
| handlerPtr, err := bleProxy.Scan(&uuid, map[string]string{}, cbPointer) |
| if err != nil { |
| log.Println(err) |
| ctx.Close() |
| return |
| } |
| s.bleHandlerProxy = ble.NewPluginStopperProxy(handlerPtr, bindings.GetAsyncWaiter()) |
| } |
| |
| func (*scannerDelegate) AcceptConnection(conn *application.Connection) { |
| conn.Close() |
| } |
| |
| func (s *scannerDelegate) Quit() { |
| if s.bleHandlerProxy != nil { |
| s.bleHandlerProxy.Stop() |
| } |
| |
| for _, stub := range s.stubs { |
| stub.Close() |
| } |
| } |
| |
| //export MojoMain |
| func MojoMain(handle C.MojoHandle) C.MojoResult { |
| application.Run(&scannerDelegate{}, system.MojoHandle(handle)) |
| return C.MOJO_RESULT_OK |
| } |
| |
| func main() {} |