Improve handling of peripheral disconnections and some refactoring.
This commit is contained in:
parent
e7f82ac9e7
commit
3bc712870d
|
@ -94,19 +94,26 @@ public class BlessedConnect extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onConnectedPeripheral(BluetoothPeripheral peripheral) {
|
public void onConnectedPeripheral(BluetoothPeripheral peripheral) {
|
||||||
|
if (peripheral.getState() == peripheral.STATE_CONNECTED) {
|
||||||
Log.d("gio", "BlessedConnect: Connected to " + peripheral.getName());
|
Log.d("gio", "BlessedConnect: Connected to " + peripheral.getName());
|
||||||
Log.d("gio", "BlessedConnect: Address = " + peripheral.getAddress());
|
Log.d("gio", "BlessedConnect: Address = " + peripheral.getAddress());
|
||||||
|
|
||||||
onConnect(peripheral, peripheral.getAddress());
|
onConnect(peripheral, peripheral.getAddress());
|
||||||
|
} else {
|
||||||
|
onDisconnect(peripheral, peripheral.getAddress());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onConnectionFailed(BluetoothPeripheral peripheral, int status) {
|
public void onConnectionFailed(BluetoothPeripheral peripheral, int status) {
|
||||||
Log.d("gio", "BlessedConnect: onConnectionFailed()");
|
Log.d("gio", "BlessedConnect: onConnectionFailed()");
|
||||||
|
onDisconnect(peripheral, peripheral.getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, int status) {
|
public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, int status) {
|
||||||
Log.d("gio", "BlessedConnect: Disconnected");
|
Log.d("gio", "BlessedConnect: Disconnected");
|
||||||
|
onDisconnect(peripheral, peripheral.getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -178,6 +185,7 @@ public class BlessedConnect extends Fragment {
|
||||||
}
|
}
|
||||||
Log.d("gio","BlessedConnect: disconnect");
|
Log.d("gio","BlessedConnect: disconnect");
|
||||||
peripheral.cancelConnection();
|
peripheral.cancelConnection();
|
||||||
|
onDisconnect(peripheral, peripheral.getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void discoverServices(BluetoothPeripheral peripheral) {
|
public void discoverServices(BluetoothPeripheral peripheral) {
|
||||||
|
@ -260,6 +268,7 @@ public class BlessedConnect extends Fragment {
|
||||||
static private native void updateState(int s);
|
static private native void updateState(int s);
|
||||||
static private native void onScan(String name, String id, int rssi, BluetoothPeripheral peripheral);
|
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 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 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 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);
|
static private native void characteristicChanged(String id, String cuuid, BluetoothGattCharacteristic chr, byte[] value, int length);
|
||||||
|
|
5
ble.go
5
ble.go
|
@ -130,6 +130,11 @@ type UpdateValueEvent struct {
|
||||||
type ConnectEvent struct {
|
type ConnectEvent struct {
|
||||||
Peripheral Peripheral
|
Peripheral Peripheral
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DisconnectEvent struct {
|
||||||
|
Peripheral Peripheral
|
||||||
|
}
|
||||||
|
|
||||||
type ConnectTimeoutEvent struct {
|
type ConnectTimeoutEvent struct {
|
||||||
Peripheral Peripheral
|
Peripheral Peripheral
|
||||||
}
|
}
|
||||||
|
|
110
ble_android.go
110
ble_android.go
|
@ -277,24 +277,7 @@ func goOnScan(cname, cid *C.char, rssi C.int, dev C.jobject) {
|
||||||
func goOnConnect(p C.jobject, cid *C.char) {
|
func goOnConnect(p C.jobject, cid *C.char) {
|
||||||
id := C.GoString(cid)
|
id := C.GoString(cid)
|
||||||
|
|
||||||
var peripheral Peripheral
|
peripheral := gBLE.retrievePeripheral(id)
|
||||||
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!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if peripheral.peripheral == 0 {
|
if peripheral.peripheral == 0 {
|
||||||
log.Printf("goOnConnect(): peripheral == null")
|
log.Printf("goOnConnect(): peripheral == null")
|
||||||
|
@ -305,27 +288,29 @@ func goOnConnect(p C.jobject, cid *C.char) {
|
||||||
log.Printf("Go: goOnConnect returning\n")
|
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
|
//export goOnDiscoverService
|
||||||
func goOnDiscoverService(cid, cuuid *C.char, serv C.jobject) {
|
func goOnDiscoverService(cid, cuuid *C.char, serv C.jobject) {
|
||||||
id := C.GoString(cid)
|
id := C.GoString(cid)
|
||||||
uuid := C.GoString(cuuid)
|
uuid := C.GoString(cuuid)
|
||||||
|
|
||||||
var peripheral Peripheral
|
peripheral := gBLE.retrievePeripheral(id)
|
||||||
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!")
|
|
||||||
}
|
|
||||||
|
|
||||||
service := Service{
|
service := Service{
|
||||||
UUID: uuid,
|
UUID: uuid,
|
||||||
|
@ -345,22 +330,7 @@ func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.c
|
||||||
cuuid := C.GoString(ccuuid)
|
cuuid := C.GoString(ccuuid)
|
||||||
log.Printf("goOnDiscoverCharacteristic: %s", cuuid)
|
log.Printf("goOnDiscoverCharacteristic: %s", cuuid)
|
||||||
|
|
||||||
var peripheral Peripheral
|
peripheral := gBLE.retrievePeripheral(id)
|
||||||
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!")
|
|
||||||
}
|
|
||||||
|
|
||||||
service := Service{
|
service := Service{
|
||||||
UUID: suuid,
|
UUID: suuid,
|
||||||
|
@ -385,23 +355,7 @@ func goOnCharacteristicChanged(cid, ccuuid *C.char, char C.jobject, cvalue *C.ch
|
||||||
id := C.GoString(cid)
|
id := C.GoString(cid)
|
||||||
cuuid := C.GoString(ccuuid)
|
cuuid := C.GoString(ccuuid)
|
||||||
|
|
||||||
var peripheral Peripheral
|
peripheral := gBLE.retrievePeripheral(id)
|
||||||
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!")
|
|
||||||
}
|
|
||||||
|
|
||||||
characteristic := Characteristic{
|
characteristic := Characteristic{
|
||||||
UUID: cuuid,
|
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),
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -174,6 +174,7 @@ func NewBLE() *BLE {
|
||||||
cd.CentralManagerDidUpdateStateCallback(didUpdateState)
|
cd.CentralManagerDidUpdateStateCallback(didUpdateState)
|
||||||
cd.CentralManagerDidDiscoverPeripheralCallback(didDiscoverPeripheral)
|
cd.CentralManagerDidDiscoverPeripheralCallback(didDiscoverPeripheral)
|
||||||
cd.CentralManagerDidConnectPeripheralCallback(didConnectPeripheral)
|
cd.CentralManagerDidConnectPeripheralCallback(didConnectPeripheral)
|
||||||
|
cd.CentralManagerDidDisconnectPeripheralCallback(didDisconnectPeripheral)
|
||||||
cd.PeripheralDidDiscoverServicesCallback(didDiscoverServices)
|
cd.PeripheralDidDiscoverServicesCallback(didDiscoverServices)
|
||||||
cd.PeripheralDidDiscoverCharacteristicsForServiceCallback(didDiscoverCharacteristics)
|
cd.PeripheralDidDiscoverCharacteristicsForServiceCallback(didDiscoverCharacteristics)
|
||||||
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
|
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
|
||||||
|
@ -281,6 +282,37 @@ func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
||||||
fmt.Printf("Go: didConnectPeripheral returning\n")
|
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) {
|
func didDiscoverServices(p *ns.CBPeripheral, e *ns.NSError) {
|
||||||
b := pdLookup[p.Ptr()]
|
b := pdLookup[p.Ptr()]
|
||||||
fmt.Printf("Did discover services\n")
|
fmt.Printf("Did discover services\n")
|
||||||
|
|
|
@ -104,6 +104,14 @@ Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject
|
||||||
(*env)->ReleaseStringUTFChars(env, jid, id);
|
(*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
|
void
|
||||||
Java_st_wow_git_ble_BlessedConnect_onDiscoverService(JNIEnv *env, jclass class, jstring jid, jstring juuid, jobject serv) {
|
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);
|
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
|
||||||
|
|
|
@ -38,6 +38,18 @@ func CBDelegateCentralManagerDidConnectPeripheral(o unsafe.Pointer, central unsa
|
||||||
cb(a1, a2)
|
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
|
//export CBDelegateCentralManagerDidDiscoverPeripheral
|
||||||
func CBDelegateCentralManagerDidDiscoverPeripheral(o unsafe.Pointer, central unsafe.Pointer, peripheral unsafe.Pointer, advertisementData unsafe.Pointer, RSSI unsafe.Pointer) {
|
func CBDelegateCentralManagerDidDiscoverPeripheral(o unsafe.Pointer, central unsafe.Pointer, peripheral unsafe.Pointer, advertisementData unsafe.Pointer, RSSI unsafe.Pointer) {
|
||||||
CBDelegateMux.RLock()
|
CBDelegateMux.RLock()
|
||||||
|
|
117383
ns/main.go
117383
ns/main.go
File diff suppressed because it is too large
Load Diff
|
@ -35,6 +35,7 @@ delegates:
|
||||||
- centralManagerDidUpdateState
|
- centralManagerDidUpdateState
|
||||||
- centralManagerDidDiscoverPeripheral
|
- centralManagerDidDiscoverPeripheral
|
||||||
- centralManagerDidConnectPeripheral
|
- centralManagerDidConnectPeripheral
|
||||||
|
- centralManagerDidDisconnectPeripheral
|
||||||
CBPeripheralDelegate:
|
CBPeripheralDelegate:
|
||||||
- peripheralDidDiscoverServices
|
- peripheralDidDiscoverServices
|
||||||
- peripheralDidDiscoverCharacteristicsForService
|
- peripheralDidDiscoverCharacteristicsForService
|
||||||
|
|
Loading…
Reference in New Issue
Block a user