Further debuging version.

This commit is contained in:
Greg 2019-12-04 16:09:54 -05:00
parent 4db9b4a5ed
commit fc0cab1ee8
6 changed files with 40 additions and 17 deletions

BIN
Ble.jar

Binary file not shown.

View File

@ -2,12 +2,13 @@ package st.wow.git.ble;
import java.lang.String; import java.lang.String;
import java.util.List; import java.util.List;
import java.util.UUID;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.os.Handler;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanResult;
@ -17,6 +18,8 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.Manifest; import android.Manifest;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
import com.welie.blessed.BluetoothCentral; 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) { if (ctx.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST); 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); installComplete(this);
} }
@ -60,8 +67,9 @@ public class BlessedConnect extends Fragment {
Context ctx = getContext(); Context ctx = getContext();
//The Blessed library does not expose this functionality, so //The Blessed library does not expose this functionality, so
//we need to get our own BluetoothAdapter and check its status. //we need to get our own BluetoothAdapter and check its status.
BluetoothAdapter adapter = ((BluetoothManager)ctx.getSystemService(ctx.BLUETOOTH_SERVICE)).getAdapter(); //BluetoothAdapter adapter = ((BluetoothManager)ctx.getSystemService(ctx.BLUETOOTH_SERVICE)).getAdapter();
return (adapter != null && adapter.isEnabled()); //return (adapter != null && adapter.isEnabled());
return true;
} }
@Override @Override
@ -135,7 +143,12 @@ public class BlessedConnect extends Fragment {
Log.d("gio", "onCharacteristicWrite(): " + characteristic.getUuid().toString()); Log.d("gio", "onCharacteristicWrite(): " + characteristic.getUuid().toString());
} }
public void onDescriptorRead(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor) { 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) { public void onDescriptorWrite(BluetoothPeripheral peripheral, byte[] value, android.bluetooth.BluetoothGattDescriptor descriptor, int status) {
Log.d("gio", "onDescriptorWrite(): " + descriptor.getUuid().toString()); Log.d("gio", "onDescriptorWrite(): " + descriptor.getUuid().toString());
@ -150,6 +163,9 @@ public class BlessedConnect extends Fragment {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
if(peripheral.isNotifying(characteristic)) { if(peripheral.isNotifying(characteristic)) {
Log.i("gio", String.format("BlessedConnect: Notify set to 'on' for %s", characteristic.getUuid())); 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 { } else {
Log.i("gio", String.format("BlessedConnect: Notify set to 'off' for %s", characteristic.getUuid())); 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) { public boolean setCharacteristicNotification(BluetoothPeripheral peripheral, BluetoothGattCharacteristic chr) {
return peripheral.setNotify(chr, true); return peripheral.setNotify(chr, true);
} }

View File

@ -7,7 +7,6 @@ package ble
import ( import (
"log" "log"
"time"
"sync" "sync"
"unsafe" "unsafe"
@ -155,7 +154,6 @@ func (b *BLE) stopScan() {
//connectPeripheral attempts to connect to a Peripheral //connectPeripheral attempts to connect to a Peripheral
func (b *BLE) connectPeripheral(x Peripheral) { func (b *BLE) connectPeripheral(x Peripheral) {
connect()
runInJVM(func(env *C.JNIEnv) { runInJVM(func(env *C.JNIEnv) {
C.connect(env, b.handle.BleConnect, x.peripheral) C.connect(env, b.handle.BleConnect, x.peripheral)
}) })
@ -206,13 +204,11 @@ func (p Peripheral) DiscoverCharacteristics(serv Service) {
//SetNotifyValue subscribes to a characteristic //SetNotifyValue subscribes to a characteristic
func (p Peripheral) SetNotifyValue(c Characteristic) { func (p Peripheral) SetNotifyValue(c Characteristic) {
go func() { runInJVM(func(env *C.JNIEnv) {
time.Sleep(time.Second / 2) C.readCharacteristic(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic)
runInJVM(func(env *C.JNIEnv) { //result := (C.setCharacteristicNotification(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic) == C.JNI_TRUE)
result := (C.setCharacteristicNotification(env, gBLE.handle.BleConnect, p.peripheral, c.characteristic) == C.JNI_TRUE) //log.Printf("setCharacteristicNotification: %s = %t", c.UUID, result)
log.Printf("setCharacteristicNotification: %s = %t", c.UUID, result) })
})
}()
} }
//NewBLE returns a pointer to a BLE struct after setting up the OS //NewBLE returns a pointer to a BLE struct after setting up the OS

Binary file not shown.

View File

@ -59,6 +59,13 @@ setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c) {
return (*env)->CallBooleanMethod(env, b, mid, g, 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 jint
GetEnv(JavaVM *vm, JNIEnv **env, jint version) { GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
return (*vm)->GetEnv(vm, (void **)env, 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 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); const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject ggatt = (*env)->NewGlobalRef(env, gatt); jobject gperipheral = (*env)->NewGlobalRef(env, peripheral);
goOnConnect(ggatt, id); goOnConnect(gperipheral, id);
(*env)->ReleaseStringUTFChars(env, jid, id); (*env)->ReleaseStringUTFChars(env, jid, id);
} }

View File

@ -7,6 +7,7 @@ void connect(JNIEnv *env, jobject b, jobject d);
void disconnect(JNIEnv *env, jobject b, jobject g); void disconnect(JNIEnv *env, jobject b, jobject g);
void discoverServices(JNIEnv *env, jobject b, jobject p); void discoverServices(JNIEnv *env, jobject b, jobject p);
void discoverCharacteristics(JNIEnv *env, jobject b, jobject g, jobject s); 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); jboolean setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c);
jint GetEnv(JavaVM *vm, JNIEnv **env, jint version); jint GetEnv(JavaVM *vm, JNIEnv **env, jint version);
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args); jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);