parent
2edbeb8714
commit
6bad5ba08e
|
@ -2,13 +2,12 @@ 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;
|
||||||
|
@ -18,8 +17,6 @@ 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;
|
||||||
|
@ -45,11 +42,7 @@ 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);
|
||||||
}
|
}
|
||||||
Log.d("gio", "BlessedConnect: creating HandlerThread");
|
central = new BluetoothCentral(ctx, centralCallback, new Handler(ctx.getMainLooper()));
|
||||||
HandlerThread thread = new HandlerThread("Bluetooth Thread");
|
|
||||||
thread.start();
|
|
||||||
Handler handler = new Handler(thread.getLooper());
|
|
||||||
central = new BluetoothCentral(ctx, centralCallback, handler);
|
|
||||||
installComplete(this);
|
installComplete(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,9 +60,8 @@ thread.start();
|
||||||
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
|
||||||
|
@ -150,12 +142,7 @@ thread.start();
|
||||||
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()");
|
Log.d("gio", "onDescriptorRead(): " + descriptor.getUuid().toString());
|
||||||
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());
|
||||||
|
@ -170,9 +157,6 @@ thread.start();
|
||||||
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()));
|
||||||
}
|
}
|
||||||
|
@ -219,9 +203,6 @@ thread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ package ble
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
@ -154,6 +155,7 @@ 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)
|
||||||
})
|
})
|
||||||
|
@ -204,11 +206,13 @@ 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() {
|
||||||
|
time.Sleep(time.Second / 2)
|
||||||
runInJVM(func(env *C.JNIEnv) {
|
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)
|
||||||
//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
|
||||||
|
|
BIN
blessed-full.jar
BIN
blessed-full.jar
Binary file not shown.
|
@ -59,13 +59,6 @@ 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);
|
||||||
|
@ -97,10 +90,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 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);
|
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
|
||||||
jobject gperipheral = (*env)->NewGlobalRef(env, peripheral);
|
jobject ggatt = (*env)->NewGlobalRef(env, gatt);
|
||||||
goOnConnect(gperipheral, id);
|
goOnConnect(ggatt, id);
|
||||||
(*env)->ReleaseStringUTFChars(env, jid, id);
|
(*env)->ReleaseStringUTFChars(env, jid, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ 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);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user