Merge branch 'master' into blessed

This commit is contained in:
Greg 2019-12-06 13:59:07 -05:00
commit 57f19407dd
9 changed files with 58497 additions and 59075 deletions

BIN
Ble.jar

Binary file not shown.

View File

@ -74,7 +74,11 @@ public class BlessedConnect extends Fragment {
}
public void scan() {
central.scanForPeripherals();
if (enabled()) {
central.scanForPeripherals();
} else {
wantScan = true;
}
}
public void stopScan() {
@ -90,19 +94,26 @@ public class BlessedConnect extends Fragment {
@Override
public void onConnectedPeripheral(BluetoothPeripheral peripheral) {
Log.d("gio", "BlessedConnect: Connected to " + peripheral.getName());
Log.d("gio", "BlessedConnect: Address = " + peripheral.getAddress());
onConnect(peripheral, peripheral.getAddress());
if (peripheral.getState() == peripheral.STATE_CONNECTED) {
Log.d("gio", "BlessedConnect: Connected to " + peripheral.getName());
Log.d("gio", "BlessedConnect: Address = " + peripheral.getAddress());
onConnect(peripheral, peripheral.getAddress());
} else {
onDisconnect(peripheral, peripheral.getAddress());
}
}
@Override
public void onConnectionFailed(BluetoothPeripheral peripheral, int status) {
Log.d("gio", "BlessedConnect: onConnectionFailed()");
onDisconnect(peripheral, peripheral.getAddress());
}
@Override
public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, int status) {
Log.d("gio", "BlessedConnect: Disconnected");
onDisconnect(peripheral, peripheral.getAddress());
}
@Override
@ -174,6 +185,7 @@ public class BlessedConnect extends Fragment {
}
Log.d("gio","BlessedConnect: disconnect");
peripheral.cancelConnection();
onDisconnect(peripheral, peripheral.getAddress());
}
public void discoverServices(BluetoothPeripheral peripheral) {
@ -256,6 +268,7 @@ public class BlessedConnect extends Fragment {
static private native void updateState(int s);
static private native void onScan(String name, String id, int rssi, BluetoothPeripheral peripheral);
static private native void onConnect(BluetoothPeripheral peripheral, String id);
static private native void onDisconnect(BluetoothPeripheral peripheral, String id);
static private native void onDiscoverService(String id, String uuid, BluetoothGattService serv);
static private native void onDiscoverCharacteristic(String id, String suuid, BluetoothGattService serv, String cuuid, BluetoothGattCharacteristic chr);
static private native void characteristicChanged(String id, String cuuid, BluetoothGattCharacteristic chr, byte[] value, int length);

5
ble.go
View File

@ -130,6 +130,11 @@ type UpdateValueEvent struct {
type ConnectEvent struct {
Peripheral Peripheral
}
type DisconnectEvent struct {
Peripheral Peripheral
}
type ConnectTimeoutEvent struct {
Peripheral Peripheral
}

View File

@ -277,24 +277,7 @@ func goOnScan(cname, cid *C.char, rssi C.int, dev C.jobject) {
func goOnConnect(p C.jobject, cid *C.char) {
id := C.GoString(cid)
var peripheral Peripheral
found := false
gBLE.peripherals.Lock()
for n, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
peripheral.peripheral = p
gBLE.peripherals.items[n].p = peripheral
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
peripheral := gBLE.retrievePeripheral(id)
if peripheral.peripheral == 0 {
log.Printf("goOnConnect(): peripheral == null")
@ -305,27 +288,29 @@ func goOnConnect(p C.jobject, cid *C.char) {
log.Printf("Go: goOnConnect returning\n")
}
//export goOnDisconnect
func goOnDisconnect(p C.jobject, cid *C.char) {
id := C.GoString(cid)
peripheral := gBLE.retrievePeripheral(id)
if peripheral.peripheral == 0 {
log.Printf("goOnDisconnect(): peripheral == null")
}
go func() {
gBLE.connections.UpdateState(peripheral, "cancel")
gBLE.events <- DisconnectEvent{peripheral}
}()
log.Printf("Go: goOnDisconnect returning\n")
}
//export goOnDiscoverService
func goOnDiscoverService(cid, cuuid *C.char, serv C.jobject) {
id := C.GoString(cid)
uuid := C.GoString(cuuid)
var peripheral Peripheral
found := false
gBLE.peripherals.Lock()
for _, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
peripheral := gBLE.retrievePeripheral(id)
service := Service{
UUID: uuid,
@ -345,22 +330,7 @@ func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.c
cuuid := C.GoString(ccuuid)
log.Printf("goOnDiscoverCharacteristic: %s", cuuid)
var peripheral Peripheral
found := false
gBLE.peripherals.Lock()
for _, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
peripheral := gBLE.retrievePeripheral(id)
service := Service{
UUID: suuid,
@ -385,23 +355,7 @@ func goOnCharacteristicChanged(cid, ccuuid *C.char, char C.jobject, cvalue *C.ch
id := C.GoString(cid)
cuuid := C.GoString(ccuuid)
var peripheral Peripheral
found := false
gBLE.peripherals.Lock()
for _, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
peripheral := gBLE.retrievePeripheral(id)
characteristic := Characteristic{
UUID: cuuid,
@ -414,3 +368,25 @@ func goOnCharacteristicChanged(cid, ccuuid *C.char, char C.jobject, cvalue *C.ch
Data: C.GoBytes(unsafe.Pointer(cvalue), length),
}
}
// internal convenience functions
func (b *BLE) retrievePeripheral(id string) Peripheral {
found := false
var peripheral Peripheral
gBLE.peripherals.Lock()
for _, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
return peripheral
}

View File

@ -174,6 +174,7 @@ func NewBLE() *BLE {
cd.CentralManagerDidUpdateStateCallback(didUpdateState)
cd.CentralManagerDidDiscoverPeripheralCallback(didDiscoverPeripheral)
cd.CentralManagerDidConnectPeripheralCallback(didConnectPeripheral)
cd.CentralManagerDidDisconnectPeripheralCallback(didDisconnectPeripheral)
cd.PeripheralDidDiscoverServicesCallback(didDiscoverServices)
cd.PeripheralDidDiscoverCharacteristicsForServiceCallback(didDiscoverCharacteristics)
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
@ -281,6 +282,37 @@ 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) {
fmt.Printf("Did disconnect peripheral\n");
b := cdLookup[c.Ptr()]
uuidstring := p.Identifier().UUIDString().String()
peripheral := newPeripheral(p)
found := false
b.peripherals.Lock()
for _, item := range b.peripherals.items {
if item.p.Identifier == uuidstring {
peripheral = item.p
found = true
break
}
}
b.peripherals.Unlock()
if !found {
peripheral.Name = peripheralName(p)
if ok := b.peripherals.Add(peripheral); ok {
pdLookup[p.Ptr()] = b
}
}
go func() {
b.connections.UpdateState(peripheral, "cancelled")
b.events <- DisconnectEvent{peripheral}
}()
}
func didDiscoverServices(p *ns.CBPeripheral, e *ns.NSError) {
b := pdLookup[p.Ptr()]
fmt.Printf("Did discover services\n")

View File

@ -104,6 +104,14 @@ Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BlessedConnect_onDisconnect(JNIEnv *env, jclass class, jobject peripheral, jstring jid) {
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject gperipheral = (*env)->NewGlobalRef(env, peripheral);
goOnDisconnect(gperipheral, id);
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BlessedConnect_onDiscoverService(JNIEnv *env, jclass class, jstring jid, jstring juuid, jobject serv) {
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);

View File

@ -38,6 +38,18 @@ func CBDelegateCentralManagerDidConnectPeripheral(o unsafe.Pointer, central unsa
cb(a1, a2)
}
//export CBDelegateCentralManagerDidDisconnectPeripheral
func CBDelegateCentralManagerDidDisconnectPeripheral(o unsafe.Pointer, central unsafe.Pointer, peripheral unsafe.Pointer, error unsafe.Pointer) {
CBDelegateMux.RLock()
cb := CBDelegateLookup[o].CentralManagerDidDisconnectPeripheral
CBDelegateMux.RUnlock()
if cb == nil { return }
a1 := &CBCentralManager{}; a1.ptr = central
a2 := &CBPeripheral{}; a2.ptr = peripheral
a3 := &NSError{}; a3.ptr = error
cb(a1, a2, a3)
}
//export CBDelegateCentralManagerDidDiscoverPeripheral
func CBDelegateCentralManagerDidDiscoverPeripheral(o unsafe.Pointer, central unsafe.Pointer, peripheral unsafe.Pointer, advertisementData unsafe.Pointer, RSSI unsafe.Pointer) {
CBDelegateMux.RLock()

117383
ns/main.go

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,7 @@ delegates:
- centralManagerDidUpdateState
- centralManagerDidDiscoverPeripheral
- centralManagerDidConnectPeripheral
- centralManagerDidDisconnectPeripheral
CBPeripheralDelegate:
- peripheralDidDiscoverServices
- peripheralDidDiscoverCharacteristicsForService