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

Commit 6fb269f

Browse files
sbodmerpetreeftime
authored andcommitted
Fixed typo in BluetoothDevice.cxx Java_tinyb_BluetoothDevice_getTxPower
instead of getTXPower Fixed class name in helper.cxx get_bluetooth_type, "Ltinyb/BluetoothType" instead of "l" Added a method removeDevices() in BluetoothAdapter class to remove all the known devices (getDevices() will return an empty list after this call). Added startNearbyDiscovery() and stopNearbyDiscovery() in BlutoothManager main class. The start method will clear the internal device list and send a notification to a listener when a new device is found. The implementation is a simple thread which will poll each x miliseconds the list of known devices and compare it to an internal list of known devices, if the device is new, an event is fired to the passed listener. Signed-off-by: sbodmer <[email protected]>
1 parent 0cb6904 commit 6fb269f

File tree

6 files changed

+131
-11
lines changed

6 files changed

+131
-11
lines changed

api/tinyb/BluetoothAdapter.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ friend class tinyb::BluetoothNotificationHandler;
4848
private:
4949
Adapter1 *object;
5050

51-
/** Removes a device from the list of devices available on this adapter.
52-
* @param[in] arg_device The path of the device on DBus
53-
* @return TRUE if device was successfully removed
54-
*/
55-
bool remove_device (
56-
const std::string &arg_device
57-
);
58-
5951
protected:
6052
BluetoothAdapter(Adapter1 *object);
6153

@@ -97,6 +89,14 @@ friend class tinyb::BluetoothNotificationHandler;
9789

9890
/* D-Bus method calls: */
9991

92+
/** Removes a device from the list of devices available on this adapter.
93+
* @param[in] arg_device The path of the device on DBus
94+
* @return TRUE if device was successfully removed
95+
*/
96+
bool remove_device (
97+
const std::string &arg_device
98+
);
99+
100100
/** Turns on device discovery if it is disabled.
101101
* @return TRUE if discovery was successfully enabled
102102
*/

java/BluetoothAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public BluetoothDevice find(String name, String address) {
8787
*/
8888
public native List<BluetoothDevice> getDevices();
8989

90+
/**
91+
* Remove all the known devices
92+
*
93+
* @return The number of devices removed from internal list
94+
* @throws BluetoothException
95+
*/
96+
public native int removeDevices() throws BluetoothException;
97+
9098
/* D-Bus property accessors: */
9199
/** Returns the hardware address of this adapter.
92100
* @return The hardware address of this adapter.

java/BluetoothManager.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class BluetoothManager
3232
private long nativeInstance;
3333
private static BluetoothManager inst;
3434

35+
/**
36+
* The thread to watch for nearby devices
37+
*/
38+
private Thread nearbyThread = null;
39+
3540
static {
3641
try {
3742
System.loadLibrary("tinyb");
@@ -201,6 +206,83 @@ private native List<BluetoothObject> getObjects(int type, String name,
201206
*/
202207
public native boolean stopDiscovery() throws BluetoothException;
203208

209+
/**
210+
* When called, each new device found will fire an event to the passed
211+
* listener<p>
212+
*
213+
* This method will create a new thread to handle the discovery process
214+
* which is a simple polling of the current list (getDevices)
215+
* <p>
216+
*
217+
* The thread is stopped when the nearbyStopDiscovery is called<p>
218+
*
219+
* @param listener
220+
* @param pollingRate The polling rate is miliseconds (1000 = 1s)
221+
* @param onlyManufacturerFilled If true, then only if the manufacturer data is filled, the event will be fired
222+
*/
223+
public void startNearbyDiscovery(final BluetoothDeviceDiscoveryListener listener, final int pollingRate, final boolean onlyManufacturerFilled) {
224+
//--- If alreay started, does nothing
225+
if ((nearbyThread != null) && nearbyThread.isAlive()) return;
226+
227+
//--- Remove all devices to have a clean start
228+
getAdapters().get(0).removeDevices();
229+
230+
//--- Start the bluez discovery
231+
startDiscovery();
232+
233+
//--- Thread to poll the devices list
234+
nearbyThread = new Thread() {
235+
public void run() {
236+
//--- The key is the address
237+
HashMap<String, BluetoothDevice> discovered = new HashMap<String, BluetoothDevice>();
238+
try {
239+
while (Thread.interrupted() == false) {
240+
List<BluetoothDevice> list = getDevices();
241+
for (BluetoothDevice d:list) {
242+
if (!discovered.containsKey(d.getAddress())) {
243+
if (onlyManufacturerFilled) {
244+
if (!d.getManufacturerData().isEmpty()) {
245+
discovered.put(d.getAddress(), d);
246+
if (listener != null) listener.bluetoothDeviceDiscovered(d);
247+
}
248+
249+
} else {
250+
discovered.put(d.getAddress(), d);
251+
if (listener != null) listener.bluetoothDeviceDiscovered(d);
252+
}
253+
}
254+
}
255+
//--- Polling rate of 1 second
256+
sleep(pollingRate);
257+
}
258+
259+
} catch (InterruptedException ex) {
260+
//--- Stopped by user or jvm
261+
}
262+
stopDiscovery();
263+
264+
}
265+
};
266+
nearbyThread.start();
267+
268+
}
269+
270+
/**
271+
* Stop the nearby thread
272+
*/
273+
public void stopNearbyDiscovery() {
274+
if ((nearbyThread != null) && nearbyThread.isAlive()) nearbyThread.interrupt();
275+
276+
}
277+
278+
/**
279+
* Interface to implement to receive the device discovery event
280+
*/
281+
public interface BluetoothDeviceDiscoveryListener {
282+
283+
public void bluetoothDeviceDiscovered(BluetoothDevice device);
284+
}
285+
204286
private native void init() throws BluetoothException;
205287
private native void delete();
206288
private BluetoothManager()

java/jni/BluetoothAdapter.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ jobject Java_tinyb_BluetoothAdapter_getDevices(JNIEnv *env, jobject obj)
134134
return nullptr;
135135
}
136136

137+
jint Java_tinyb_BluetoothAdapter_removeDevices(JNIEnv *env, jobject obj)
138+
{
139+
try {
140+
BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
141+
std::vector<std::unique_ptr<tinyb::BluetoothDevice>> array = obj_adapter->get_devices();
142+
143+
for (unsigned int i =0;i<array.size();i++) {
144+
std::unique_ptr<tinyb::BluetoothDevice> *obj_device = &array.at(i);
145+
std::string path = obj_device->get()->get_object_path();
146+
// printf("PATH:%s\n", path.c_str());
147+
// fflush(stdout);
148+
obj_adapter->remove_device(path.c_str());
149+
150+
}
151+
return array.size();
152+
153+
} catch (std::bad_alloc &e) {
154+
raise_java_oom_exception(env, e);
155+
} catch (BluetoothException &e) {
156+
raise_java_bluetooth_exception(env, e);
157+
} catch (std::runtime_error &e) {
158+
raise_java_runtime_exception(env, e);
159+
} catch (std::invalid_argument &e) {
160+
raise_java_invalid_arg_exception(env, e);
161+
} catch (std::exception &e) {
162+
raise_java_exception(env, e);
163+
}
164+
return 0;
165+
}
166+
137167
jstring Java_tinyb_BluetoothAdapter_getAddress(JNIEnv *env, jobject obj)
138168
{
139169
try {

java/jni/BluetoothDevice.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
using namespace tinyb;
3636

37-
jobject Java_tinyb_BluetoothAdapter_getBluetoothType(JNIEnv *env, jobject obj)
37+
jobject Java_tinyb_BluetoothDevice_getBluetoothType(JNIEnv *env, jobject obj)
3838
{
3939
try {
4040
(void)obj;
@@ -1098,7 +1098,7 @@ void Java_tinyb_BluetoothDevice_disableServiceDataNotifications(JNIEnv *env, job
10981098

10991099

11001100

1101-
jshort Java_tinyb_BluetoothDevice_getTXPower(JNIEnv *env, jobject obj)
1101+
jshort Java_tinyb_BluetoothDevice_getTxPower(JNIEnv *env, jobject obj)
11021102
{
11031103
try {
11041104
BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj);

java/jni/helper.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
188188
{
189189
jclass b_type_enum = search_class(env, JAVA_PACKAGE "/BluetoothType");
190190

191-
jfieldID b_type_field = search_field(env, b_type_enum, field_name, "l", true);
191+
jfieldID b_type_field = search_field(env, b_type_enum, field_name, "L" JAVA_PACKAGE "/BluetoothType;", true);
192192

193193
jobject result = env->GetStaticObjectField(b_type_enum, b_type_field);
194194
return result;

0 commit comments

Comments
 (0)