diff --git a/Ble.jar b/Ble.jar index 9b04446..f52d79b 100644 Binary files a/Ble.jar and b/Ble.jar differ diff --git a/BleConnect.java b/BleConnect.java index 909857c..604c404 100644 --- a/BleConnect.java +++ b/BleConnect.java @@ -124,21 +124,24 @@ public class BleConnect extends Fragment { private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { + BluetoothDevice device = gatt.getDevice(); + String addr = device.getAddress(); switch (newState) { case BluetoothProfile.STATE_CONNECTED: { - BluetoothDevice device = gatt.getDevice(); Log.d("gio", "Connected"); - String addr = device.getAddress(); Log.d("gio", "Address = " + addr); - onConnect(gatt, device.getAddress()); + onConnect(gatt, addr); break; } case BluetoothProfile.STATE_DISCONNECTED: { Log.d("gio", "Disconnected"); + onDisconnect(gatt, addr); + gatt.close(); break; } default: { Log.d("gio", "onConnectionStateChange: unknown state"); + gatt.close(); break; } } @@ -260,6 +263,7 @@ public class BleConnect extends Fragment { static private native void updateState(int s); static private native void onScan(String name, String id, int rssi, BluetoothDevice dev); static private native void onConnect(BluetoothGatt gatt, String id); + static private native void onDisconnect(BluetoothGatt gatt, 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); diff --git a/ble_android.go b/ble_android.go index 50b710f..8baf4c0 100644 --- a/ble_android.go +++ b/ble_android.go @@ -274,7 +274,24 @@ func goOnScan(cname, cid *C.char, rssi C.int, dev C.jobject) { func goOnConnect(gatt C.jobject, cid *C.char) { id := C.GoString(cid) - peripheral := gBLE.retrievePeripheral(id) + var peripheral Peripheral + found := false + + gBLE.peripherals.Lock() + for n, item := range gBLE.peripherals.items { + if item.p.Identifier == id { + peripheral = item.p + peripheral.gatt = gatt + gBLE.peripherals.items[n].p = peripheral + found = true + break + } + } + gBLE.peripherals.Unlock() + + if !found { + log.Printf("Go: peripheral not found!") + } if peripheral.gatt == 0 { log.Printf("goOnConnect(): gatt == null") @@ -291,8 +308,8 @@ func goOnDisconnect(p C.jobject, cid *C.char) { peripheral := gBLE.retrievePeripheral(id) - if peripheral.peripheral == 0 { - log.Printf("goOnDisconnect(): peripheral == null") + if peripheral.device == 0 { + log.Printf("goOnDisconnect(): device == null") } go func() {