Add discoverCharacteristics() for Android.

This commit is contained in:
Greg 2019-11-22 23:33:45 -05:00
parent 86e724cc8c
commit 99bcd1f4fb
4 changed files with 105 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -119,7 +120,7 @@ public class BleConnect extends Fragment {
} }
public void onServicesDiscovered(BluetoothGatt gatt, int status) { public void onServicesDiscovered(BluetoothGatt gatt, int status) {
for (BluetoothGattService serv : gatt.getServices()) { for (BluetoothGattService serv : gatt.getServices()) {
onDiscoverService(device.getAddress(), serv.getUuid().toString()); onDiscoverService(device.getAddress(), serv.getUuid().toString(), serv);
} }
} }
}; };
@ -154,6 +155,17 @@ public class BleConnect extends Fragment {
bluetoothGatt.discoverServices(); bluetoothGatt.discoverServices();
} }
public void discoverCharacteristics(BluetoothGattService serv) {
Log.d("gio","BleConnect: discoverCharacteristics()");
if (bluetoothGatt == null || device == null) {
return;
}
for (BluetoothGattCharacteristic chr : serv.getCharacteristics()) {
Log.d("gio","BleConnect: -- " + chr.getUuid().toString());
onDiscoverCharacteristic(device.getAddress(), serv.getUuid().toString(), serv, chr.getUuid().toString(), chr);
}
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("gio", "BleConnect: onActivityResult()"); Log.d("gio", "BleConnect: onActivityResult()");
if (requestCode == REQUEST_ENABLE_BT) { if (requestCode == REQUEST_ENABLE_BT) {
@ -175,6 +187,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(String id); static private native void onConnect(String id);
static private native void onDiscoverService(String id, String uuid); 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);
} }

View File

@ -36,8 +36,15 @@ type Peripheral struct {
device C.jobject device C.jobject
} }
type Service string type Service struct {
type Characteristic string UUID string
service C.jobject
}
type Characteristic struct {
UUID string
characteristic C.jobject
}
// Internal global variables // Internal global variables
@ -168,6 +175,16 @@ func (x Peripheral) DiscoverServices() {
//DiscoverCharacteristics asks a Peripheral for the Characteristics related //DiscoverCharacteristics asks a Peripheral for the Characteristics related
//to a Service //to a Service
func (p Peripheral) DiscoverCharacteristics(serv Service) { func (p Peripheral) DiscoverCharacteristics(serv Service) {
//launch a goroutine because this function calls back directly
//from the same thread.
go func() {
connect()
log.Printf("discovering characteristics")
runInJVM(func(env *C.JNIEnv) {
C.discoverCharacteristics(env, gBLE.handle.BleConnect, serv.service)
})
log.Printf("discovering characteristics done")
}()
} }
//SetNotifyValue subscribes to a characteristic //SetNotifyValue subscribes to a characteristic
@ -267,7 +284,7 @@ func goOnConnect(cid *C.char) {
} }
//export goOnDiscoverService //export goOnDiscoverService
func goOnDiscoverService(cid *C.char, cuuid *C.char) { func goOnDiscoverService(cid, cuuid *C.char, serv C.jobject) {
id := C.GoString(cid) id := C.GoString(cid)
uuid := C.GoString(cuuid) uuid := C.GoString(cuuid)
@ -288,8 +305,55 @@ func goOnDiscoverService(cid *C.char, cuuid *C.char) {
log.Printf("Go: peripheral not found!") log.Printf("Go: peripheral not found!")
} }
service := Service{
UUID: uuid,
service: serv,
}
gBLE.events <- DiscoverServiceEvent{ gBLE.events <- DiscoverServiceEvent{
Peripheral: peripheral, Peripheral: peripheral,
Gatt: gatt.Service{uuid}, Gatt: gatt.Service{uuid},
Service: service,
}
}
//export goOnDiscoverCharacteristic
func goOnDiscoverCharacteristic(cid, csuuid *C.char, serv C.jobject, ccuuid *C.char, char C.jobject) {
id := C.GoString(cid)
suuid := C.GoString(csuuid)
cuuid := C.GoString(ccuuid)
log.Printf("goOnDiscoverCharacteristic: %s", cuuid)
var peripheral Peripheral
found := false
gBLE.peripherals.Lock()
for _, item := range gBLE.peripherals.items {
if item.p.Identifier == id {
peripheral = item.p
found = true
break
}
}
gBLE.peripherals.Unlock()
if !found {
log.Printf("Go: peripheral not found!")
}
service := Service{
UUID: suuid,
service: serv,
}
characteristic := Characteristic{
UUID: cuuid,
characteristic: char,
}
gBLE.events <- DiscoverCharacteristicEvent{
Peripheral: peripheral,
Service: service,
Characteristic: characteristic,
Gatt: gatt.Characteristic{cuuid},
} }
} }

View File

@ -45,6 +45,13 @@ discoverServices(JNIEnv *env, jobject b, jobject p) {
(*env)->CallVoidMethod(env, b, mid, p); (*env)->CallVoidMethod(env, b, mid, p);
} }
void
discoverCharacteristics(JNIEnv *env, jobject b, jobject s) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "discoverCharacteristics", "(Landroid/bluetooth/BluetoothGattService;)V");
(*env)->CallVoidMethod(env, b, mid, s);
}
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);
@ -83,10 +90,23 @@ Java_st_wow_git_ble_BleConnect_onConnect(JNIEnv *env, jclass class, jstring jid)
} }
void void
Java_st_wow_git_ble_BleConnect_onDiscoverService(JNIEnv *env, jclass class, jstring jid, jstring juuid) { Java_st_wow_git_ble_BleConnect_onDiscoverService(JNIEnv *env, jclass class, jstring jid, jstring juuid, jobject serv) {
char* id = (*env)->GetStringUTFChars(env, jid, NULL); char* id = (*env)->GetStringUTFChars(env, jid, NULL);
char* uuid = (*env)->GetStringUTFChars(env, juuid, NULL); char* uuid = (*env)->GetStringUTFChars(env, juuid, NULL);
goOnDiscoverService(id, uuid); jobject gserv = (*env)->NewGlobalRef(env, serv);
goOnDiscoverService(id, uuid, gserv);
(*env)->ReleaseStringUTFChars(env, jid, id); (*env)->ReleaseStringUTFChars(env, jid, id);
(*env)->ReleaseStringUTFChars(env, juuid, uuid); (*env)->ReleaseStringUTFChars(env, juuid, uuid);
} }
void
Java_st_wow_git_ble_BleConnect_onDiscoverCharacteristic(JNIEnv *env, jclass class, jstring jid, jstring jsuuid, jobject serv, jstring jcuuid, jobject chr) {
char* id = (*env)->GetStringUTFChars(env, jid, NULL);
char* suuid = (*env)->GetStringUTFChars(env, jsuuid, NULL);
char* cuuid = (*env)->GetStringUTFChars(env, jcuuid, NULL);
jobject gchr = (*env)->NewGlobalRef(env, chr);
goOnDiscoverCharacteristic(id, suuid, serv, cuuid, gchr);
(*env)->ReleaseStringUTFChars(env, jid, id);
(*env)->ReleaseStringUTFChars(env, jsuuid, suuid);
}

View File

@ -6,6 +6,7 @@ void stopScan(JNIEnv *env, jobject b);
void connect(JNIEnv *env, jobject b, jobject d); void connect(JNIEnv *env, jobject b, jobject d);
void disconnect(JNIEnv *env, jobject b); void disconnect(JNIEnv *env, jobject b);
void discoverServices(JNIEnv *env, jobject b, jobject p); void discoverServices(JNIEnv *env, jobject b, jobject p);
void discoverCharacteristics(JNIEnv *env, jobject b, jobject s);
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);
jint DetachCurrentThread(JavaVM *vm); jint DetachCurrentThread(JavaVM *vm);