Compare commits
7
Commits
90ab682a44
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e53c7be976 | ||
|
|
99598c14fe | ||
|
|
876ea68622 | ||
|
|
9f32185b78 | ||
|
|
d70bbfed13 | ||
|
|
6da0474e59 | ||
|
|
a6cc776a0d |
@@ -1 +1,3 @@
|
||||
|
||||
classes/
|
||||
cb.ast
|
||||
|
||||
+15
-2
@@ -35,6 +35,7 @@ public class BlessedConnect extends Fragment {
|
||||
|
||||
final int PERMISSION_REQUEST = 1;
|
||||
final int REQUEST_ENABLE_BT = 2;
|
||||
final int PERMISSION_REQUEST_BLE = 3;
|
||||
|
||||
public BlessedConnect() {
|
||||
Log.d("gio", "BlessedConnect()");
|
||||
@@ -50,8 +51,12 @@ public class BlessedConnect extends Fragment {
|
||||
Activity act = (Activity)ctx;
|
||||
FragmentTransaction ft = act.getFragmentManager().beginTransaction();
|
||||
ft.add(inst, "BlessedConnect");
|
||||
if (android.os.Build.VERSION.SDK_INT >= 24) {
|
||||
ft.commitNowAllowingStateLoss();
|
||||
} else {
|
||||
ft.commitNow();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -59,7 +64,15 @@ public class BlessedConnect extends Fragment {
|
||||
super.onAttach(ctx);
|
||||
Log.d("gio", "BlessedConnect: onAttach()");
|
||||
ctx.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||
if (ctx.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= 31) {
|
||||
// BLE on Android 12+ needs the BLUETOOTH_SCAN/CONNECT runtime
|
||||
// permissions. BLUETOOTH_SCAN is declared with
|
||||
// usesPermissionFlags="neverForLocation", so no location
|
||||
// permission is required for scanning.
|
||||
requestPermissions(new String[]{
|
||||
"android.permission.BLUETOOTH_SCAN",
|
||||
"android.permission.BLUETOOTH_CONNECT"}, PERMISSION_REQUEST_BLE);
|
||||
} else if (ctx.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST);
|
||||
}
|
||||
central = new BluetoothCentral(ctx, centralCallback, null);
|
||||
@@ -264,7 +277,7 @@ public class BlessedConnect extends Fragment {
|
||||
@Override
|
||||
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
|
||||
Log.d("gio", "BlessedConnect: onRequestPermissionsResult");
|
||||
if (requestCode == PERMISSION_REQUEST) {
|
||||
if (requestCode == PERMISSION_REQUEST || requestCode == PERMISSION_REQUEST_BLE) {
|
||||
boolean granted = true;
|
||||
for (int x : grantResults) {
|
||||
if (x == PackageManager.PERMISSION_DENIED) {
|
||||
|
||||
+17
-8
@@ -225,6 +225,15 @@ func NewBLE() *BLE {
|
||||
//Enable
|
||||
func (b *BLE) Enable(view uintptr) {
|
||||
log.Printf("ble.Enable()")
|
||||
// view == 0 is Gio's detach signal (GioView.onDestroyView sends
|
||||
// ViewEvent{View: 0} when the view is destroyed, e.g. the activity
|
||||
// going away on a recents-wipe). Nothing to register; passing the null
|
||||
// ref on would abort in JNI (GetObjectClass on null). A re-attach
|
||||
// arrives as a fresh event with a live view. Callers that forward the
|
||||
// event unfiltered are protected by this guard.
|
||||
if view == 0 {
|
||||
return
|
||||
}
|
||||
setJVM(app.JavaVM())
|
||||
runInJVM(func(env *JNIEnv) {
|
||||
log.Printf("ble.Enable(): inside runInJVM()")
|
||||
@@ -326,7 +335,7 @@ func goOnDiscoverService(cid, cuuid *C.char, serv C.jobject) {
|
||||
}
|
||||
|
||||
//export goOnDiscoverCharacteristic
|
||||
func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.char, char C.jobject) {
|
||||
func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.char, characteristic C.jobject) {
|
||||
id := C.GoString(cid)
|
||||
suuid := C.GoString(csuuid)
|
||||
cuuid := C.GoString(ccuuid)
|
||||
@@ -339,34 +348,34 @@ func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.c
|
||||
service: serv,
|
||||
}
|
||||
|
||||
characteristic := Characteristic{
|
||||
ch := Characteristic{
|
||||
UUID: cuuid,
|
||||
characteristic: char,
|
||||
characteristic: characteristic,
|
||||
}
|
||||
|
||||
gBLE.events <- DiscoverCharacteristicEvent{
|
||||
Peripheral: peripheral,
|
||||
Service: service,
|
||||
Characteristic: characteristic,
|
||||
Characteristic: ch,
|
||||
Gatt: gatt.Characteristic{cuuid},
|
||||
}
|
||||
}
|
||||
|
||||
//export goOnCharacteristicChanged
|
||||
func goOnCharacteristicChanged(cid, ccuuid *C.char, char C.jobject, cvalue *C.char, length C.jint) {
|
||||
func goOnCharacteristicChanged(cid, ccuuid *C.char, characteristic C.jobject, cvalue *C.char, length C.jint) {
|
||||
id := C.GoString(cid)
|
||||
cuuid := C.GoString(ccuuid)
|
||||
|
||||
peripheral := gBLE.retrievePeripheral(id)
|
||||
|
||||
characteristic := Characteristic{
|
||||
ch := Characteristic{
|
||||
UUID: cuuid,
|
||||
characteristic: char,
|
||||
characteristic: characteristic,
|
||||
}
|
||||
|
||||
gBLE.events <- UpdateValueEvent{
|
||||
Peripheral: peripheral,
|
||||
Characteristic: characteristic,
|
||||
Characteristic: ch,
|
||||
Data: C.GoBytes(unsafe.Pointer(cvalue), length),
|
||||
}
|
||||
}
|
||||
|
||||
+9
-8
@@ -156,7 +156,7 @@ func (p Peripheral) DiscoverCharacteristics(serv Service) {
|
||||
|
||||
//SetNotifyValue subscribes to a characteristic
|
||||
func (p Peripheral) SetNotifyValue(c Characteristic) {
|
||||
p.p.SetNotifyValue(1, c)
|
||||
p.p.SetNotifyValue((ns.BOOL)(true), c)
|
||||
}
|
||||
|
||||
//NewBLE returns a pointer to a BLE struct after setting up the OS
|
||||
@@ -207,7 +207,8 @@ func (b *BLE) Enable(v uintptr) {
|
||||
|
||||
// Core Bluetooth callback functions
|
||||
|
||||
func didUpdateState(c *ns.CBCentralManager) {
|
||||
func didUpdateState(d ns.CBDelegate, c *ns.CBCentralManager) {
|
||||
fmt.Print("didUpdateState")
|
||||
b := cdLookup[c.Ptr()]
|
||||
st := c.CBManager.State()
|
||||
if st == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
|
||||
@@ -230,7 +231,7 @@ func didUpdateState(c *ns.CBCentralManager) {
|
||||
b.events <- UpdateStateEvent{State: b.stringState()}
|
||||
}
|
||||
|
||||
func didDiscoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
|
||||
func didDiscoverPeripheral(d_ ns.CBDelegate, c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
|
||||
b := cdLookup[c.Ptr()]
|
||||
peripheral := newPeripheral(p)
|
||||
peripheral.RSSI = (int)(rssi.IntValue())
|
||||
@@ -247,7 +248,7 @@ func _didDiscoverPeripheral(b *BLE, peripheral Peripheral) {
|
||||
}
|
||||
}
|
||||
|
||||
func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
||||
func didConnectPeripheral(d ns.CBDelegate, c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
||||
fmt.Printf("Did connect peripheral\n")
|
||||
b := cdLookup[c.Ptr()]
|
||||
|
||||
@@ -280,7 +281,7 @@ func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
||||
fmt.Printf("Go: didConnectPeripheral returning\n")
|
||||
}
|
||||
|
||||
func didDisconnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, e *ns.NSError) {
|
||||
func didDisconnectPeripheral(d ns.CBDelegate, c *ns.CBCentralManager, p *ns.CBPeripheral, e *ns.NSError) {
|
||||
fmt.Printf("Did disconnect peripheral\n");
|
||||
b := cdLookup[c.Ptr()]
|
||||
|
||||
@@ -311,7 +312,7 @@ func didDisconnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, e *ns.N
|
||||
}()
|
||||
}
|
||||
|
||||
func didDiscoverServices(p *ns.CBPeripheral, e *ns.NSError) {
|
||||
func didDiscoverServices(d ns.CBDelegate, p *ns.CBPeripheral, e *ns.NSError) {
|
||||
b := pdLookup[p.Ptr()]
|
||||
fmt.Printf("Did discover services\n")
|
||||
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
@@ -327,7 +328,7 @@ func didDiscoverServices(p *ns.CBPeripheral, e *ns.NSError) {
|
||||
fmt.Printf("Go: didDiscoverServices returning\n")
|
||||
}
|
||||
|
||||
func didDiscoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
|
||||
func didDiscoverCharacteristics(d ns.CBDelegate, p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
|
||||
b := pdLookup[p.Ptr()]
|
||||
fmt.Printf("Did discover characteristics\n")
|
||||
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
@@ -345,7 +346,7 @@ func didDiscoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSErr
|
||||
fmt.Printf("Go: didDiscoverCharacteristics returning\n")
|
||||
}
|
||||
|
||||
func didUpdateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
|
||||
func didUpdateValue(d ns.CBDelegate, p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
|
||||
b := pdLookup[p.Ptr()]
|
||||
v := chr.Value()
|
||||
b.events <-UpdateValueEvent{
|
||||
|
||||
@@ -5,6 +5,14 @@
|
||||
|
||||
void
|
||||
registerFragment(JNIEnv *env, jobject view) {
|
||||
if (view == NULL) {
|
||||
// Detach signal (Gio sends ViewEvent{View: 0} when the view is
|
||||
// destroyed, e.g. the activity going away on a recents-wipe).
|
||||
// The Go side already filters these; this guard keeps the JNI
|
||||
// calls safe if one ever slips through (GetObjectClass on null
|
||||
// aborts the process).
|
||||
return;
|
||||
}
|
||||
jclass cls = (*env)->GetObjectClass(env, view);
|
||||
jmethodID mid = (*env)->GetMethodID(env, cls, "getContext", "()Landroid/content/Context;");
|
||||
jobject ctx = (*env)->CallObjectMethod(env, view, mid);
|
||||
|
||||
+32
-16
@@ -15,27 +15,20 @@ import "C"
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//export CBDelegateCentralManagerDidUpdateState
|
||||
func CBDelegateCentralManagerDidUpdateState(o unsafe.Pointer, central unsafe.Pointer) {
|
||||
CBDelegateMux.RLock()
|
||||
cb := CBDelegateLookup[o].CentralManagerDidUpdateState
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
a1 := &CBCentralManager{}; a1.ptr = central
|
||||
cb(a1)
|
||||
}
|
||||
|
||||
//export CBDelegateCentralManagerDidConnectPeripheral
|
||||
func CBDelegateCentralManagerDidConnectPeripheral(o unsafe.Pointer, central unsafe.Pointer, peripheral unsafe.Pointer) {
|
||||
CBDelegateMux.RLock()
|
||||
cb := CBDelegateLookup[o].CentralManagerDidConnectPeripheral
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBCentralManager{}; a1.ptr = central
|
||||
a2 := &CBPeripheral{}; a2.ptr = peripheral
|
||||
cb(a1, a2)
|
||||
cb(self, a1, a2)
|
||||
}
|
||||
|
||||
//export CBDelegateCentralManagerDidDisconnectPeripheral
|
||||
@@ -44,10 +37,12 @@ func CBDelegateCentralManagerDidDisconnectPeripheral(o unsafe.Pointer, central u
|
||||
cb := CBDelegateLookup[o].CentralManagerDidDisconnectPeripheral
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBCentralManager{}; a1.ptr = central
|
||||
a2 := &CBPeripheral{}; a2.ptr = peripheral
|
||||
a3 := &NSError{}; a3.ptr = error
|
||||
cb(a1, a2, a3)
|
||||
cb(self, a1, a2, a3)
|
||||
}
|
||||
|
||||
//export CBDelegateCentralManagerDidDiscoverPeripheral
|
||||
@@ -56,11 +51,26 @@ func CBDelegateCentralManagerDidDiscoverPeripheral(o unsafe.Pointer, central uns
|
||||
cb := CBDelegateLookup[o].CentralManagerDidDiscoverPeripheral
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBCentralManager{}; a1.ptr = central
|
||||
a2 := &CBPeripheral{}; a2.ptr = peripheral
|
||||
a3 := &NSDictionary{}; a3.ptr = advertisementData
|
||||
a4 := &NSNumber{}; a4.ptr = RSSI
|
||||
cb(a1, a2, a3, a4)
|
||||
cb(self, a1, a2, a3, a4)
|
||||
}
|
||||
|
||||
//export CBDelegateCentralManagerDidUpdateState
|
||||
func CBDelegateCentralManagerDidUpdateState(o unsafe.Pointer, central unsafe.Pointer) {
|
||||
fmt.Print("CentralManagerDidUpdateState")
|
||||
CBDelegateMux.RLock()
|
||||
cb := CBDelegateLookup[o].CentralManagerDidUpdateState
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBCentralManager{}; a1.ptr = central
|
||||
cb(self, a1)
|
||||
}
|
||||
|
||||
//export CBDelegatePeripheralDidDiscoverServices
|
||||
@@ -69,9 +79,11 @@ func CBDelegatePeripheralDidDiscoverServices(o unsafe.Pointer, peripheral unsafe
|
||||
cb := CBDelegateLookup[o].PeripheralDidDiscoverServices
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBPeripheral{}; a1.ptr = peripheral
|
||||
a2 := &NSError{}; a2.ptr = error
|
||||
cb(a1, a2)
|
||||
cb(self, a1, a2)
|
||||
}
|
||||
|
||||
//export CBDelegatePeripheralDidDiscoverCharacteristicsForService
|
||||
@@ -80,10 +92,12 @@ func CBDelegatePeripheralDidDiscoverCharacteristicsForService(o unsafe.Pointer,
|
||||
cb := CBDelegateLookup[o].PeripheralDidDiscoverCharacteristicsForService
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBPeripheral{}; a1.ptr = peripheral
|
||||
a2 := &CBService{}; a2.ptr = service
|
||||
a3 := &NSError{}; a3.ptr = error
|
||||
cb(a1, a2, a3)
|
||||
cb(self, a1, a2, a3)
|
||||
}
|
||||
|
||||
//export CBDelegatePeripheralDidUpdateValueForCharacteristic
|
||||
@@ -92,8 +106,10 @@ func CBDelegatePeripheralDidUpdateValueForCharacteristic(o unsafe.Pointer, perip
|
||||
cb := CBDelegateLookup[o].PeripheralDidUpdateValueForCharacteristic
|
||||
CBDelegateMux.RUnlock()
|
||||
if cb == nil { return }
|
||||
self := CBDelegate{}
|
||||
self.ptr = o
|
||||
a1 := &CBPeripheral{}; a1.ptr = peripheral
|
||||
a2 := &CBCharacteristic{}; a2.ptr = characteristic
|
||||
a3 := &NSError{}; a3.ptr = error
|
||||
cb(a1, a2, a3)
|
||||
cb(self, a1, a2, a3)
|
||||
}
|
||||
|
||||
+51466
-52408
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -1,6 +1,6 @@
|
||||
inputfiles:
|
||||
- /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
|
||||
- /System/Library/Frameworks/CoreBluetooth.framework/Headers/CoreBluetooth.h
|
||||
- /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
|
||||
- /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework/Headers/CoreBluetooth.h
|
||||
|
||||
classes:
|
||||
- NSObject
|
||||
|
||||
Reference in New Issue
Block a user