| // Copyright 2015 The Vanadium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // +build android |
| |
| package discovery |
| |
| import ( |
| "fmt" |
| "unsafe" |
| |
| jutil "v.io/x/jni/util" |
| "v.io/x/ref/lib/discovery" |
| ) |
| // #include "jni.h" |
| import "C" |
| var ( |
| // Global reference for io.v.android.libs.discovery.ble.BlePlugin |
| jBlePluginClass jutil.Class |
| // Global reference for io.v.x.ref.lib.discovery.Advertisement |
| jAdvertisementClass jutil.Class |
| // Global reference for java.util.UUID |
| jUUIdClass jutil.Class |
| // Global reference for io.v.android.libs.discovery.ble.NativeScanHandler |
| jNativeScanHandlerClass jutil.Class |
| ) |
| |
| func Init(env jutil.Env) error { |
| var err error |
| jUUIDClass, err = jutil.JFindClass(env, "java/util/UUID") |
| if err != nil { |
| return err |
| } |
| |
| jNativeScanHandlerClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/NativeScanHandler") |
| if err != nil { |
| return err |
| } |
| |
| jBlePluginClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/BlePlugin") |
| if err != nil { |
| return err |
| } |
| |
| jAdvertisementClass, err = jutil.JFindClass(env, "io/v/x/ref/lib/discovery/Advertisement") |
| return err |
| } |
| |
| //export Java_io_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate |
| func Java_io_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate(jenv *C.JNIEnv, _ C.jobject, jAdv C.jobject, goPtr C.jlong) { |
| env := jutil.Env(uintptr(unsafe.Pointer(jenv))) |
| c, ok := (*(*chan <- discovery.Advertisement)(jutil.NativePtr(goPtr))) |
| |
| if !ok { |
| jutil.JThrow(env, fmt.Errorf("Internal error")) |
| return |
| } |
| advObject := jutil.Object(uintptr(unsafe.Pointer(jAdv))) |
| var adv discovery.Advertisement |
| if err := jutil.GoVomCopy(env, advObject, jAdvertisementClass, &adv); err != nil { |
| jutil.JThrow(env, err) |
| return |
| } |
| ch <-advObject |
| } |
| |
| //export Java_io_android_libs_discovery_ble_NativeScanHandler_nativeCleanup |
| func Java_io_android_libs_discovery_ble_NativeScanHandler_nativeCleanup(jenv *C.JNIEnv, _ C.jobject, goPtr c.jlong) { |
| jutil.GoUnref(jutil.NativePtr(goPtr)) |
| } |