ble/ble_android.go

141 lines
2.8 KiB
Go

package ble
import (
"log"
"gioui.org/app"
)
/*
#include <jni.h>
*/
import "C"
// Types required for ble.go
type bleState string
type bleHandle struct {
adapter C.jobject
class C.jclass
}
type Peripheral struct {
Name string
RSSI int
Identifier string
}
type Service string
type Characteristic string
// Internal global variables
var jvmContext uintptr
// Functions required by API
//Init needs to be called before the BLE library can be used. On Android we
//need to set up the JVM by calling setJVM in jni_android.go.
func Init() {
log.Print("ble.Init()")
h := app.PlatformHandle()
jvmContext = h.Context
setJVM(h.JVM, h.Context)
}
//peripheralLookup returns a pointer to a BLE struct related to the given
//Peripheral.
func peripheralLookup(p Peripheral) *BLE {
return &BLE{}
}
//newPeripheral creates a new Peripheral struct
func newPeripheral() Peripheral {
return Peripheral{}
}
func (p Peripheral) IsIncomplete() bool {
return false
}
func (p Peripheral) Retain() {
}
//stringState returns a string version of the BLE state
func (b *BLE) stringState() string {
return ""
}
//readyToScan returns true if the hardware is ready to initiate a scan
func (b *BLE) readyToScan() bool {
return true
}
//scan puts the BLE hardware into scanning mode
func (b *BLE) scan() {
}
//stopScan stops a scan in progress
func (b *BLE) stopScan() {
}
//connectPeripheral attempts to connect to a Peripheral
func (b *BLE) connectPeripheral(x Peripheral) {
}
//cancelConnection cancels an in-progress connection attempt
func (b *BLE) cancelConnection(p Peripheral) {
}
//knownPeripheral returns a Peripheral that is known to the system without
//scanning
func (b *BLE) knownPeripheral(p Peripheral) (Peripheral, bool) {
return Peripheral{}, false
}
//DiscoverServices asks a Peripheral for its Services
func (x Peripheral) DiscoverServices() {
}
//DiscoverCharacteristics asks a Peripheral for the Characteristics related
//to a Service
func (p Peripheral) DiscoverCharacteristics(serv Service) {
}
//SetNotifyValue subscribes to a characteristic
func (p Peripheral) SetNotifyValue(c Characteristic) {
}
//NewBLE returns a pointer to a BLE struct after setting up the OS
//Bluetooth API.
func NewBLE() *BLE {
ps := Peripherals{items: make([]PeripheralListItem, 0)}
h := bleHandle{}
RunInJVM(func(env *JNIEnv) {
h.adapter = GetBluetoothAdapter(env, jvmContext)
h.class = JniGetObjectClass(env, h.adapter)
})
ret := &BLE{
events: make(chan interface{}),
peripherals: ps,
handle: h,
}
//check if Bluetooth is enabled and if so, send an UpdateStateEvent
go func() {
RunInJVM(func(env *JNIEnv) {
mid := JniGetMethodID(env, h.class, "isEnabled", "()Z")
en := JniCallBooleanMethod(env, h.adapter, mid)
if en {
log.Print("It's enabled!")
ret.events <-UpdateStateEvent{"powered on"}
} else {
log.Print("It's not enabled")
}
})
}()
return ret
}