Working version with old BleConnect code instead of Blessed.

This commit is contained in:
Greg 2019-12-06 14:22:16 -05:00
parent 4ae98b622b
commit c6f6e4f317
3 changed files with 27 additions and 6 deletions

BIN
Ble.jar

Binary file not shown.

View File

@ -124,21 +124,24 @@ public class BleConnect extends Fragment {
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
BluetoothDevice device = gatt.getDevice();
String addr = device.getAddress();
switch (newState) { switch (newState) {
case BluetoothProfile.STATE_CONNECTED: { case BluetoothProfile.STATE_CONNECTED: {
BluetoothDevice device = gatt.getDevice();
Log.d("gio", "Connected"); Log.d("gio", "Connected");
String addr = device.getAddress();
Log.d("gio", "Address = " + addr); Log.d("gio", "Address = " + addr);
onConnect(gatt, device.getAddress()); onConnect(gatt, addr);
break; break;
} }
case BluetoothProfile.STATE_DISCONNECTED: { case BluetoothProfile.STATE_DISCONNECTED: {
Log.d("gio", "Disconnected"); Log.d("gio", "Disconnected");
onDisconnect(gatt, addr);
gatt.close();
break; break;
} }
default: { default: {
Log.d("gio", "onConnectionStateChange: unknown state"); Log.d("gio", "onConnectionStateChange: unknown state");
gatt.close();
break; break;
} }
} }
@ -260,6 +263,7 @@ public class BleConnect 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, BluetoothDevice dev); 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 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 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);

View File

@ -274,7 +274,24 @@ func goOnScan(cname, cid *C.char, rssi C.int, dev C.jobject) {
func goOnConnect(gatt C.jobject, cid *C.char) { func goOnConnect(gatt C.jobject, cid *C.char) {
id := C.GoString(cid) 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 { if peripheral.gatt == 0 {
log.Printf("goOnConnect(): gatt == null") log.Printf("goOnConnect(): gatt == null")
@ -291,8 +308,8 @@ func goOnDisconnect(p C.jobject, cid *C.char) {
peripheral := gBLE.retrievePeripheral(id) peripheral := gBLE.retrievePeripheral(id)
if peripheral.peripheral == 0 { if peripheral.device == 0 {
log.Printf("goOnDisconnect(): peripheral == null") log.Printf("goOnDisconnect(): device == null")
} }
go func() { go func() {