diff --git a/Ble.jar b/Ble.jar index 395873d..49dd545 100644 Binary files a/Ble.jar and b/Ble.jar differ diff --git a/BlessedConnect.java b/BlessedConnect.java index d8b8dd7..0db8486 100644 --- a/BlessedConnect.java +++ b/BlessedConnect.java @@ -2,12 +2,13 @@ package st.wow.git.ble; import java.lang.String; import java.util.List; +import java.util.UUID; import android.app.Activity; import android.app.Fragment; -import android.os.Handler; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; +import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothManager; import android.bluetooth.le.ScanResult; @@ -17,6 +18,8 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.Manifest; +import android.os.Handler; +import android.os.HandlerThread; import android.util.Log; import com.welie.blessed.BluetoothCentral; @@ -42,7 +45,11 @@ public class BlessedConnect extends Fragment { 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, new Handler(ctx.getMainLooper())); + Log.d("gio", "BlessedConnect: creating HandlerThread"); + HandlerThread thread = new HandlerThread("Bluetooth Thread"); +thread.start(); + Handler handler = new Handler(thread.getLooper()); + central = new BluetoothCentral(ctx, centralCallback, handler); installComplete(this); } @@ -60,8 +67,9 @@ public class BlessedConnect extends Fragment { Context ctx = getContext(); //The Blessed library does not expose this functionality, so //we need to get our own BluetoothAdapter and check its status. - BluetoothAdapter adapter = ((BluetoothManager)ctx.getSystemService(ctx.BLUETOOTH_SERVICE)).getAdapter(); - return (adapter != null && adapter.isEnabled()); + //BluetoothAdapter adapter = ((BluetoothManager)ctx.getSystemService(ctx.BLUETOOTH_SERVICE)).getAdapter(); + //return (adapter != null && adapter.isEnabled()); + return true; } @Override @@ -135,7 +143,12 @@ public class BlessedConnect extends Fragment { Log.d("gio", "onCharacteristicWrite(): " + characteristic.getUuid().toString()); } public void onDescriptorRead(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor) { - Log.d("gio", "onDescriptorRead(): " + descriptor.getUuid().toString()); + Log.d("gio", "onDescriptorRead()"); + if (value == BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) { + Log.d("gio", "onDescriptorRead(): " + descriptor.getUuid().toString() + ": notification enabled"); + } else { + Log.d("gio", "onDescriptorRead(): " + descriptor.getUuid().toString() + ": notification not enabled"); + } } public void onDescriptorWrite(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor, int status) { Log.d("gio", "onDescriptorWrite(): " + descriptor.getUuid().toString()); @@ -150,6 +163,9 @@ public class BlessedConnect extends Fragment { if (status == BluetoothGatt.GATT_SUCCESS) { if(peripheral.isNotifying(characteristic)) { Log.i("gio", String.format("BlessedConnect: Notify set to 'on' for %s", characteristic.getUuid())); + BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); + Log.d("gio", "Reading descriptor"); + peripheral.readDescriptor(descriptor); } else { Log.i("gio", String.format("BlessedConnect: Notify set to 'off' for %s", characteristic.getUuid())); } @@ -195,6 +211,9 @@ public class BlessedConnect extends Fragment { } } + public void readCharacteristic(BluetoothPeripheral peripheral, BluetoothGattCharacteristic chr) { + peripheral.readCharacteristic(chr); + } public boolean setCharacteristicNotification(BluetoothPeripheral peripheral, BluetoothGattCharacteristic chr) { return peripheral.setNotify(chr, true); } diff --git a/ble_android.go b/ble_android.go index 825a936..779a94f 100644 --- a/ble_android.go +++ b/ble_android.go @@ -7,7 +7,6 @@ package ble import ( "log" - "time" "sync" "unsafe" @@ -155,7 +154,6 @@ func (b *BLE) stopScan() { //connectPeripheral attempts to connect to a Peripheral func (b *BLE) connectPeripheral(x Peripheral) { - connect() runInJVM(func(env *C.JNIEnv) { C.connect(env, b.handle.BleConnect, x.peripheral) }) @@ -206,13 +204,11 @@ func (p Peripheral) DiscoverCharacteristics(serv Service) { //SetNotifyValue subscribes to a characteristic func (p Peripheral) SetNotifyValue(c Characteristic) { - go func() { - time.Sleep(time.Second / 2) - runInJVM(func(env *C.JNIEnv) { - result := (C.setCharacteristicNotification(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic) == C.JNI_TRUE) - log.Printf("setCharacteristicNotification: %s = %t", c.UUID, result) - }) - }() + runInJVM(func(env *C.JNIEnv) { + C.readCharacteristic(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic) + //result := (C.setCharacteristicNotification(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic) == C.JNI_TRUE) + //log.Printf("setCharacteristicNotification: %s = %t", c.UUID, result) + }) } //NewBLE returns a pointer to a BLE struct after setting up the OS diff --git a/blessed-full.jar b/blessed-full.jar index e6cd12a..cb9affa 100644 Binary files a/blessed-full.jar and b/blessed-full.jar differ diff --git a/jni_android.c b/jni_android.c index 0cfbac9..1b0e1a8 100644 --- a/jni_android.c +++ b/jni_android.c @@ -59,6 +59,13 @@ setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c) { return (*env)->CallBooleanMethod(env, b, mid, g, c); } +void +readCharacteristic(JNIEnv *env, jobject b, jobject g, jobject c) { + jclass cls = (*env)->GetObjectClass(env, b); + jmethodID mid = (*env)->GetMethodID(env, cls, "readCharacteristic", "(Lcom/welie/blessed/BluetoothPeripheral;Landroid/bluetooth/BluetoothGattCharacteristic;)V"); + (*env)->CallBooleanMethod(env, b, mid, g, c); +} + jint GetEnv(JavaVM *vm, JNIEnv **env, jint version) { return (*vm)->GetEnv(vm, (void **)env, version); @@ -90,10 +97,10 @@ Java_st_wow_git_ble_BlessedConnect_onScan(JNIEnv *env, jclass class, jstring jna } void -Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject gatt, jstring jid) { +Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject peripheral, jstring jid) { const char* id = (*env)->GetStringUTFChars(env, jid, NULL); - jobject ggatt = (*env)->NewGlobalRef(env, gatt); - goOnConnect(ggatt, id); + jobject gperipheral = (*env)->NewGlobalRef(env, peripheral); + goOnConnect(gperipheral, id); (*env)->ReleaseStringUTFChars(env, jid, id); } diff --git a/jni_android.h b/jni_android.h index 68cb480..d8977ec 100644 --- a/jni_android.h +++ b/jni_android.h @@ -7,6 +7,7 @@ void connect(JNIEnv *env, jobject b, jobject d); void disconnect(JNIEnv *env, jobject b, jobject g); void discoverServices(JNIEnv *env, jobject b, jobject p); void discoverCharacteristics(JNIEnv *env, jobject b, jobject g, jobject s); +void readCharacteristic(JNIEnv *env, jobject b, jobject g, jobject c); jboolean setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c); jint GetEnv(JavaVM *vm, JNIEnv **env, jint version); jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);