diff --git a/BlessedConnect.java b/BlessedConnect.java index 79f9e22..7b62c69 100644 --- a/BlessedConnect.java +++ b/BlessedConnect.java @@ -2,13 +2,12 @@ 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; @@ -18,8 +17,6 @@ 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; @@ -45,11 +42,7 @@ 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); } - 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); + central = new BluetoothCentral(ctx, centralCallback, new Handler(ctx.getMainLooper())); installComplete(this); } @@ -67,9 +60,8 @@ thread.start(); 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()); - return true; + BluetoothAdapter adapter = ((BluetoothManager)ctx.getSystemService(ctx.BLUETOOTH_SERVICE)).getAdapter(); + return (adapter != null && adapter.isEnabled()); } @Override @@ -150,12 +142,7 @@ thread.start(); Log.d("gio", "onCharacteristicWrite(): " + characteristic.getUuid().toString()); } public void onDescriptorRead(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor) { - 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"); - } + Log.d("gio", "onDescriptorRead(): " + descriptor.getUuid().toString()); } public void onDescriptorWrite(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor, int status) { Log.d("gio", "onDescriptorWrite(): " + descriptor.getUuid().toString()); @@ -170,9 +157,6 @@ thread.start(); 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())); } @@ -219,9 +203,6 @@ thread.start(); } } - 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 d9db660..681dfdb 100644 --- a/ble_android.go +++ b/ble_android.go @@ -7,6 +7,7 @@ package ble import ( "log" + "time" "sync" "unsafe" @@ -154,6 +155,7 @@ 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) }) @@ -204,11 +206,13 @@ func (p Peripheral) DiscoverCharacteristics(serv Service) { //SetNotifyValue subscribes to a characteristic func (p Peripheral) SetNotifyValue(c Characteristic) { - 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) - }) + 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) + }) + }() } //NewBLE returns a pointer to a BLE struct after setting up the OS diff --git a/blessed-full.jar b/blessed-full.jar index cb9affa..e6cd12a 100644 Binary files a/blessed-full.jar and b/blessed-full.jar differ diff --git a/jni_android.c b/jni_android.c index e50c218..8652c5f 100644 --- a/jni_android.c +++ b/jni_android.c @@ -59,13 +59,6 @@ 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); @@ -97,10 +90,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 peripheral, jstring jid) { +Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject gatt, jstring jid) { const char* id = (*env)->GetStringUTFChars(env, jid, NULL); - jobject gperipheral = (*env)->NewGlobalRef(env, peripheral); - goOnConnect(gperipheral, id); + jobject ggatt = (*env)->NewGlobalRef(env, gatt); + goOnConnect(ggatt, id); (*env)->ReleaseStringUTFChars(env, jid, id); } diff --git a/jni_android.h b/jni_android.h index d8977ec..68cb480 100644 --- a/jni_android.h +++ b/jni_android.h @@ -7,7 +7,6 @@ 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);