Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit bbe7933

Browse files
vkolotovpetreeftime
authored andcommitted
Implementing a generic method to set discover filters (UUIDs, rssi, pathloss, type)
Signed-off-by: Vlad Kolotov <[email protected]>
1 parent fd4fdb0 commit bbe7933

File tree

7 files changed

+83
-10
lines changed

7 files changed

+83
-10
lines changed

api/tinyb/BluetoothAdapter.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ typedef struct _Object Object;
3434
struct _Adapter1;
3535
typedef struct _Adapter1 Adapter1;
3636

37-
enum class TransportType {
38-
AUTO,
39-
BREDR,
40-
LE
41-
};
42-
4337
/**
4438
* Provides access to Bluetooth adapters. Follows the BlueZ adapter API
4539
* available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/adapter-api.txt

api/tinyb/BluetoothObject.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ enum class BluetoothType {
3838
GATT_CHARACTERISTIC,
3939
GATT_DESCRIPTOR
4040
};
41+
enum class TransportType {
42+
AUTO,
43+
BREDR,
44+
LE
45+
};
4146

4247
class BluetoothEvent;
4348
class BluetoothEventManager;

java/BluetoothAdapter.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,36 @@ public BluetoothDevice find(String name, String address) {
239239
*/
240240
public native String getModalias();
241241

242+
/** This method sets the device discovery filter for the caller. When this method is called
243+
* with no filter parameter, filter is removed.
244+
* <p>
245+
* When a remote device is found that advertises any UUID from UUIDs, it will be reported if:
246+
* <ul><li>Pathloss and RSSI are both empty.</li>
247+
* <li>only Pathloss param is set, device advertise TX pwer, and computed pathloss is less than Pathloss param.</li>
248+
* <li>only RSSI param is set, and received RSSI is higher than RSSI param.</li>
249+
* </ul>
250+
* <p>
251+
* If one or more discovery filters have been set, the RSSI delta-threshold,
252+
* that is imposed by StartDiscovery by default, will not be applied.
253+
* <p>
254+
* If "auto" transport is requested, scan will use LE, BREDR, or both, depending on what's
255+
* currently enabled on the controller.
256+
*
257+
* @param uuids a list of device UUIDs
258+
* @param rssi a rssi value
259+
* @param pathloss a pathloss value
260+
*/
261+
public void setDiscoveryFilter(List<Integer> uuids, int rssi, int pathloss, TransportType transportType) {
262+
setDiscoveryFilter(uuids, rssi, pathloss, transportType.ordinal());
263+
}
264+
242265
/** This method sets RSSI device discovery filter for the caller. When this method is called
243266
* with 0, filter is removed.
244267
* @param rssi a rssi value
245268
*/
246-
public native void setRssiDiscoveryFilter(int rssi);
269+
public void setRssiDiscoveryFilter(int rssi) {
270+
setDiscoveryFilter(Collections.EMPTY_LIST, rssi, 0, TransportType.AUTO);
271+
}
247272

248273
/** Returns the interface name of the adapter.
249274
* @return The interface name of the adapter.
@@ -255,6 +280,8 @@ public String getInterfaceName() {
255280

256281
private native void delete();
257282

283+
private native void setDiscoveryFilter(List<Integer> uuids, int rssi, int pathloss, int transportType);
284+
258285
private BluetoothAdapter(long instance)
259286
{
260287
super(instance);

java/TransportType.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tinyb;
2+
3+
/**
4+
* TransportType determines type of bluetooth scan.
5+
*/
6+
public enum TransportType {
7+
/**
8+
* interleaved scan
9+
*/
10+
AUTO,
11+
/**
12+
* BR/EDR inquiry
13+
*/
14+
BREDR,
15+
/**
16+
* LE scan only
17+
*/
18+
LE
19+
}

java/jni/BluetoothAdapter.cxx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,16 +787,18 @@ void Java_tinyb_BluetoothAdapter_delete(JNIEnv *env, jobject obj)
787787
}
788788
}
789789

790-
void Java_tinyb_BluetoothAdapter_setRssiDiscoveryFilter(JNIEnv *env, jobject obj, jint rssi)
790+
void Java_tinyb_BluetoothAdapter_setDiscoveryFilter(JNIEnv *env, jobject obj, jobject uuids, jint rssi, jint pathloss, jint transportType)
791791
{
792792
try {
793793
BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
794794

795795
std::vector<BluetoothUUID> native_uuids;
796796
native_uuids.reserve(0);
797797

798-
obj_adapter->set_discovery_filter(native_uuids, (int16_t) rssi, 0, TransportType::AUTO);
799-
} catch (std::bad_alloc &e) {
798+
TransportType t_type = from_int_to_transport_type((int) transportType);
799+
800+
obj_adapter->set_discovery_filter(native_uuids, (int16_t) rssi, (uint16_t) pathloss, t_type);
801+
} catch (std::bad_alloc &e) {
800802
raise_java_oom_exception(env, e);
801803
} catch (BluetoothException &e) {
802804
raise_java_bluetooth_exception(env, e);

java/jni/helper.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,31 @@ tinyb::BluetoothType from_int_to_btype(int type)
183183
return result;
184184
}
185185

186+
tinyb::TransportType from_int_to_transport_type(int type)
187+
{
188+
tinyb::TransportType result = tinyb::TransportType::AUTO;
189+
190+
switch (type)
191+
{
192+
case 0:
193+
result = tinyb::TransportType::AUTO;
194+
break;
195+
196+
case 1:
197+
result = tinyb::TransportType::BREDR;
198+
break;
199+
200+
case 2:
201+
result = tinyb::TransportType::LE;
202+
break;
203+
204+
default:
205+
result = tinyb::TransportType::AUTO;
206+
break;
207+
}
208+
209+
return result;
210+
}
186211

187212
jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
188213
{

java/jni/helper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ std::string from_jstring_to_string(JNIEnv *env, jstring str);
4242
tinyb::BluetoothType from_int_to_btype(int type);
4343
jobject get_bluetooth_type(JNIEnv *env, const char *field_name);
4444
jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add);
45+
tinyb::TransportType from_int_to_transport_type(int type);
4546

4647
template <typename T>
4748
T *getInstance(JNIEnv *env, jobject obj)

0 commit comments

Comments
 (0)