diff options
author | Sven Gothel <[email protected]> | 2020-02-08 13:41:02 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-02-08 13:41:02 +0100 |
commit | 0c76e289ad011f1c616176a896da02e9b255275e (patch) | |
tree | 463bb306fff93c4d7908c8d539556729e7861eb6 /java/tinyb | |
parent | 6ef564e5874ce96f12e5d70df4e126b15fb01a29 (diff) |
Move java classes to package sub folder
Diffstat (limited to 'java/tinyb')
-rw-r--r-- | java/tinyb/BluetoothAdapter.java | 319 | ||||
-rw-r--r-- | java/tinyb/BluetoothCallback.java | 35 | ||||
-rw-r--r-- | java/tinyb/BluetoothDevice.java | 361 | ||||
-rw-r--r-- | java/tinyb/BluetoothEvent.java | 53 | ||||
-rw-r--r-- | java/tinyb/BluetoothException.java | 31 | ||||
-rw-r--r-- | java/tinyb/BluetoothGattCharacteristic.java | 141 | ||||
-rw-r--r-- | java/tinyb/BluetoothGattDescriptor.java | 89 | ||||
-rw-r--r-- | java/tinyb/BluetoothGattService.java | 99 | ||||
-rw-r--r-- | java/tinyb/BluetoothManager.java | 341 | ||||
-rw-r--r-- | java/tinyb/BluetoothNotification.java | 35 | ||||
-rw-r--r-- | java/tinyb/BluetoothObject.java | 93 | ||||
-rw-r--r-- | java/tinyb/BluetoothType.java | 34 | ||||
-rw-r--r-- | java/tinyb/ObjectArgCallback.java | 40 | ||||
-rw-r--r-- | java/tinyb/ObjectArrayArgCallback.java | 40 | ||||
-rw-r--r-- | java/tinyb/TransportType.java | 19 |
15 files changed, 1730 insertions, 0 deletions
diff --git a/java/tinyb/BluetoothAdapter.java b/java/tinyb/BluetoothAdapter.java new file mode 100644 index 00000000..8094ff23 --- /dev/null +++ b/java/tinyb/BluetoothAdapter.java @@ -0,0 +1,319 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; +import java.time.Duration; + +/** + * Provides access to Bluetooth adapters. Follows the BlueZ adapter API + * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/adapter-api.txt + */ +public class BluetoothAdapter extends BluetoothObject +{ + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + + static BluetoothType class_type() { return BluetoothType.ADAPTER; } + + /** Find a BluetoothDevice. If parameters name and address are not null, + * the returned object will have to match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter name optionally specify the name of the object you are + * waiting for + * @parameter address optionally specify the MAC address of the device you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the name and address or null if not found before + * timeout expires. + */ + public BluetoothDevice find(String name, String address, Duration duration) { + BluetoothManager manager = BluetoothManager.getBluetoothManager(); + return (BluetoothDevice) manager.find(BluetoothType.DEVICE, name, address, this, duration); + } + + /** Find a BluetoothDevice. If parameters name and address are not null, + * the returned object will have to match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter name optionally specify the name of the object you are + * waiting for + * @parameter address optionally specify the MAC address of the device you are + * waiting for + * @return An object matching the name and address. + */ + public BluetoothDevice find(String name, String address) { + return find(name, address, Duration.ZERO); + } + + /* D-Bus method calls: */ + /** Turns on device discovery if it is disabled. + * @return TRUE if discovery was successfully enabled + */ + public native boolean startDiscovery() throws BluetoothException; + + /** Turns off device discovery if it is enabled. + * @return TRUE if discovery was successfully disabled + */ + public native boolean stopDiscovery() throws BluetoothException; + + /** Returns a list of BluetoothDevices visible from this adapter. + * @return A list of BluetoothDevices visible on this adapter, + * NULL if an error occurred + */ + public native List<BluetoothDevice> getDevices(); + + /** + * Remove all the known devices + * + * @return The number of devices removed from internal list + * @throws BluetoothException + */ + public native int removeDevices() throws BluetoothException; + + /* D-Bus property accessors: */ + /** Returns the hardware address of this adapter. + * @return The hardware address of this adapter. + */ + public native String getAddress(); + + /** Returns the system name of this adapter. + * @return The system name of this adapter. + */ + public native String getName(); + + /** Returns the friendly name of this adapter. + * @return The friendly name of this adapter, or NULL if not set. + */ + public native String getAlias(); + + /** Sets the friendly name of this adapter. + */ + public native void setAlias(String value); + + /** Returns the Bluetooth class of the adapter. + * @return The Bluetooth class of the adapter. + */ + public native long getBluetoothClass(); + + /** Returns the power state the adapter. + * @return The power state of the adapter. + */ + public native boolean getPowered(); + + /** + * Enables notifications for the powered property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the powered + * property. + */ + public native void enablePoweredNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the powered property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disablePoweredNotifications(); + + /** Sets the power state the adapter. + */ + public native void setPowered(boolean value); + + /** Returns the discoverable state the adapter. + * @return The discoverable state of the adapter. + */ + public native boolean getDiscoverable(); + + /** + * Enables notifications for the discoverable property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the discoverable + * property. + */ + public native void enableDiscoverableNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the discoverable property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableDiscoverableNotifications(); + + /** Sets the discoverable state the adapter. + */ + public native void setDiscoverable(boolean value); + + /** Returns the discoverable timeout the adapter. + * @return The discoverable timeout of the adapter. + */ + public native long getDiscoverableTimeout(); + + /** Sets the discoverable timeout the adapter. A value of 0 disables + * the timeout. + */ + public native void setDiscoverableTimout(long value); + + /** + * This method connects to device without need of + * performing General Discovery. Connection mechanism is + * similar to Connect method from Device1 interface with + * exception that this method returns success when physical + * connection is established. After this method returns, + * services discovery will continue and any supported + * profile will be connected. There is no need for calling + * Connect on Device1 after this call. If connection was + * successful this method returns object path to created + * device object. + * + * @param address The Bluetooth device address of the remote + * device. This parameter is mandatory. + * @param addressType The Bluetooth device Address Type. This is + * address type that should be used for initial + * connection. If this parameter is not present + * BR/EDR device is created. + * Possible values: + * <ul> + * <li>{@code public} - Public address</li> + * <li>{@code random} - Random address</li> + * </ul> + */ + public native BluetoothDevice connectDevice(String address, String addressType); + + /** Returns the pairable state the adapter. + * @return The pairable state of the adapter. + */ + public native boolean getPairable(); + + /** + * Enables notifications for the pairable property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the pairable + * property. + */ + public native void enablePairableNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the pairable property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disablePairableNotifications(); + + /** Sets the discoverable state the adapter. + */ + public native void setPairable(boolean value); + + /** Returns the timeout in seconds after which pairable state turns off + * automatically, 0 means never. + * @return The pairable timeout of the adapter. + */ + public native long getPairableTimeout(); + + /** Sets the timeout after which pairable state turns off automatically, 0 means never. + */ + public native void setPairableTimeout(long value); + + /** Returns the discovering state the adapter. It can be modified through + * start_discovery/stop_discovery functions. + * @return The discovering state of the adapter. + */ + public native boolean getDiscovering(); + + /** + * Enables notifications for the discovering property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the discovering + * property. + */ + public native void enableDiscoveringNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the discovering property and unregisters the discovering + * object passed through the corresponding enable method. + */ + public native void disableDiscoveringNotifications(); + + /** Returns the UUIDs of the adapter. + * @return Array containing the UUIDs of the adapter, ends with NULL. + */ + public native String[] getUUIDs(); + + /** Returns the local ID of the adapter. + * @return The local ID of the adapter. + */ + public native String getModalias(); + + /** This method sets the device discovery filter for the caller. When this method is called + * with no filter parameter, filter is removed. + * <p> + * When a remote device is found that advertises any UUID from UUIDs, it will be reported if: + * <ul><li>Pathloss and RSSI are both empty.</li> + * <li>only Pathloss param is set, device advertise TX pwer, and computed pathloss is less than Pathloss param.</li> + * <li>only RSSI param is set, and received RSSI is higher than RSSI param.</li> + * </ul> + * <p> + * If one or more discovery filters have been set, the RSSI delta-threshold, + * that is imposed by StartDiscovery by default, will not be applied. + * <p> + * If "auto" transport is requested, scan will use LE, BREDR, or both, depending on what's + * currently enabled on the controller. + * + * @param uuids a list of device UUIDs + * @param rssi a rssi value + * @param pathloss a pathloss value + */ + public void setDiscoveryFilter(List<UUID> uuids, int rssi, int pathloss, TransportType transportType) { + List<String> uuidsFmt = new ArrayList<>(uuids.size()); + for (UUID uuid : uuids) { + uuidsFmt.add(uuid.toString()); + } + setDiscoveryFilter(uuidsFmt, rssi, pathloss, transportType.ordinal()); + } + + /** This method sets RSSI device discovery filter for the caller. When this method is called + * with 0, filter is removed. + * @param rssi a rssi value + */ + public void setRssiDiscoveryFilter(int rssi) { + setDiscoveryFilter(Collections.EMPTY_LIST, rssi, 0, TransportType.AUTO); + } + + /** Returns the interface name of the adapter. + * @return The interface name of the adapter. + */ + public String getInterfaceName() { + String[] path = getObjectPath().split("/"); + return path[path.length-1]; + } + + private native void delete(); + + private native void setDiscoveryFilter(List<String> uuids, int rssi, int pathloss, int transportType); + + private BluetoothAdapter(long instance) + { + super(instance); + } +} diff --git a/java/tinyb/BluetoothCallback.java b/java/tinyb/BluetoothCallback.java new file mode 100644 index 00000000..0cbe0ed0 --- /dev/null +++ b/java/tinyb/BluetoothCallback.java @@ -0,0 +1,35 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +public abstract class BluetoothCallback implements Runnable +{ + protected BluetoothObject bObj; + + /* + * public void run() is missing because it will be implemented + * in children classes + */ +} diff --git a/java/tinyb/BluetoothDevice.java b/java/tinyb/BluetoothDevice.java new file mode 100644 index 00000000..da077117 --- /dev/null +++ b/java/tinyb/BluetoothDevice.java @@ -0,0 +1,361 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; +import java.time.Duration; + +/** + * Provides access to Bluetooth devices. Follows the BlueZ adapter API + * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/device-api.txt + */ +public class BluetoothDevice extends BluetoothObject +{ + public native BluetoothType getBluetoothType(); + public native BluetoothDevice clone(); + + static BluetoothType class_type() { return BluetoothType.DEVICE; } + + /** Find a BluetoothGattService. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattService you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattService find(String UUID, Duration duration) { + BluetoothManager manager = BluetoothManager.getBluetoothManager(); + return (BluetoothGattService) manager.find(BluetoothType.GATT_SERVICE, + null, UUID, this, duration); + } + + /** Find a BluetoothGattService. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattService you are + * waiting for + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattService find(String UUID) { + return find(UUID, Duration.ZERO); + } + + /* D-Bus method calls: */ + /** The connection to this device is removed, removing all connected + * profiles. + * @return TRUE if the device disconnected + */ + public native boolean disconnect() throws BluetoothException; + + /** An asynchronous connection to this device is initiated, + * connecting each profile flagged as auto-connectable. + */ + public native void connectAsyncStart() throws BluetoothException; + + /** Completion of the initiated asynchronous connection. + * @return TRUE if the device connected + */ + public native boolean connectAsyncFinish() throws BluetoothException; + + /** A connection to this device is established, connecting each profile + * flagged as auto-connectable. + * @return TRUE if the device connected + */ + public native boolean connect() throws BluetoothException; + + /** Connects a specific profile available on the device, given by UUID + * @param arg_UUID The UUID of the profile to be connected + * @return TRUE if the profile connected successfully + */ + public native boolean connectProfile(String arg_UUID) throws BluetoothException; + + /** Disconnects a specific profile available on the device, given by UUID + * @param arg_UUID The UUID of the profile to be disconnected + * @return TRUE if the profile disconnected successfully + */ + public native boolean disconnectProfile(String arg_UUID) throws BluetoothException; + + /** A connection to this device is established, and the device is then + * paired. + * @return TRUE if the device connected and paired + */ + public native boolean pair() throws BluetoothException; + + /** Remove this device from the system (like an unpair). + * @return TRUE if the device has been removed + * @throws BluetoothException + */ + public native boolean remove() throws BluetoothException; + + /** Cancels an initiated pairing operation + * @return TRUE if the paring is cancelled successfully + */ + public native boolean cancelPairing() throws BluetoothException; + + /** Returns a list of BluetoothGattServices available on this device. + * @return A list of BluetoothGattServices available on this device, + * NULL if an error occurred + */ + public native List<BluetoothGattService> getServices(); + + /* D-Bus property accessors: */ + /** Returns the hardware address of this device. + * @return The hardware address of this device. + */ + public native String getAddress(); + + /** Returns the remote friendly name of this device. + * @return The remote friendly name of this device, or NULL if not set. + */ + public native String getName(); + + /** Returns an alternative friendly name of this device. + * @return The alternative friendly name of this device, or NULL if not set. + */ + public native String getAlias(); + + /** Sets an alternative friendly name of this device. + */ + public native void setAlias(String value); + + /** Returns the Bluetooth class of the device. + * @return The Bluetooth class of the device. + */ + public native int getBluetoothClass(); + + /** Returns the appearance of the device, as found by GAP service. + * @return The appearance of the device, as found by GAP service. + */ + public native short getAppearance(); + + /** Returns the proposed icon name of the device. + * @return The proposed icon name, or NULL if not set. + */ + public native String getIcon(); + + /** Returns the paired state the device. + * @return The paired state of the device. + */ + public native boolean getPaired(); + + /** + * Enables notifications for the paired property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the paired + * property. + */ + public native void enablePairedNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the paired property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disablePairedNotifications(); + + /** Returns the trusted state the device. + * @return The trusted state of the device. + */ + public native boolean getTrusted(); + + /** + * Enables notifications for the trusted property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the trusted + * property. + */ + public native void enableTrustedNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the trusted property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableTrustedNotifications(); + + /** Sets the trusted state the device. + */ + public native void setTrusted(boolean value); + + /** Returns the blocked state the device. + * @return The blocked state of the device. + */ + public native boolean getBlocked(); + + /** + * Enables notifications for the blocked property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the blocked + * property. + */ + public native void enableBlockedNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the blocked property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableBlockedNotifications(); + + /** Sets the blocked state the device. + */ + public native void setBlocked(boolean value); + + /** Returns if device uses only pre-Bluetooth 2.1 pairing mechanism. + * @return True if device uses only pre-Bluetooth 2.1 pairing mechanism. + */ + public native boolean getLegacyPairing(); + + /** Returns the Received Signal Strength Indicator of the device. + * @return The Received Signal Strength Indicator of the device. + */ + public native short getRSSI(); + + /** + * Enables notifications for the RSSI property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Short> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the RSSI + * property. + */ + public native void enableRSSINotifications(BluetoothNotification<Short> callback); + /** + * Disables notifications of the RSSI property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableRSSINotifications(); + + /** Returns the connected state of the device. + * @return The connected state of the device. + */ + public native boolean getConnected(); + + /** + * Enables notifications for the connected property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the connected + * property. + */ + public native void enableConnectedNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the connected property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableConnectedNotifications(); + + /** Returns the UUIDs of the device. + * @return Array containing the UUIDs of the device, ends with NULL. + */ + public native String[] getUUIDs(); + + /** Returns the local ID of the adapter. + * @return The local ID of the adapter. + */ + public native String getModalias(); + + /** Returns the adapter on which this device was discovered or + * connected. + * @return The adapter. + */ + public native BluetoothAdapter getAdapter(); + + /** Returns a map containing manufacturer specific advertisement data. + * An entry has a uint16_t key and an array of bytes. + * @return manufacturer specific advertisement data. + */ + public native Map<Short, byte[]> getManufacturerData(); + + /** + * Enables notifications for the manufacturer data property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Map<Short, byte[]> > object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the manufacturer data + * property. + */ + public native void enableManufacturerDataNotifications(BluetoothNotification<Map<Short, byte[]> > callback); + /** + * Disables notifications of the manufacturer data property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableManufacturerDataNotifications(); + + + /** Returns a map containing service advertisement data. + * An entry has a UUID string key and an array of bytes. + * @return service advertisement data. + */ + public native Map<String, byte[]> getServiceData(); + + /** + * Enables notifications for the service data property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Map<String, byte[]> > object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the service data + * property. + */ + public native void enableServiceDataNotifications(BluetoothNotification<Map<String, byte[]> > callback); + /** + * Disables notifications of the service data property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableServiceDataNotifications(); + + + /** Returns the transmission power level (0 means unknown). + * @return the transmission power level (0 means unknown). + */ + public native short getTxPower (); + + /** Returns true if service discovery has ended. + * @return true if the service discovery has ended. + */ + public native boolean getServicesResolved (); + + /** + * Enables notifications for the services resolved property and calls run function of the + * BluetoothNotification object. + * @param callback A BluetoothNotification<Boolean> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the services resolved + * property. + */ + public native void enableServicesResolvedNotifications(BluetoothNotification<Boolean> callback); + /** + * Disables notifications of the services resolved property and unregisters the callback + * object passed through the corresponding enable method. + */ + public native void disableServicesResolvedNotifications(); + + private native void delete(); + + private BluetoothDevice(long instance) + { + super(instance); + } + +} diff --git a/java/tinyb/BluetoothEvent.java b/java/tinyb/BluetoothEvent.java new file mode 100644 index 00000000..b2141131 --- /dev/null +++ b/java/tinyb/BluetoothEvent.java @@ -0,0 +1,53 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; + +public class BluetoothEvent +{ + private long nativeInstance; + + public native BluetoothType getType(); + public native String getName(); + public native String getIdentifier(); + public native boolean executeCallback(); + public native boolean hasCallback(); + + private native void init(BluetoothType type, String name, String identifier, + BluetoothObject parent, BluetoothCallback cb, Object data); + private native void delete(); + + public BluetoothEvent(BluetoothType type, String name, String identifier, + BluetoothObject parent, BluetoothCallback cb, Object data) + { + init(type, name, identifier, parent, cb, data); + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/tinyb/BluetoothException.java b/java/tinyb/BluetoothException.java new file mode 100644 index 00000000..d14b4903 --- /dev/null +++ b/java/tinyb/BluetoothException.java @@ -0,0 +1,31 @@ +/* + * Author: Petre Eftime <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +public class BluetoothException extends RuntimeException { + BluetoothException(String msg) { + super(msg); + } +} diff --git a/java/tinyb/BluetoothGattCharacteristic.java b/java/tinyb/BluetoothGattCharacteristic.java new file mode 100644 index 00000000..9e20fffc --- /dev/null +++ b/java/tinyb/BluetoothGattCharacteristic.java @@ -0,0 +1,141 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; +import java.time.Duration; + +/** + * Provides access to Bluetooth GATT characteristic. Follows the BlueZ adapter API + * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt + */ +public class BluetoothGattCharacteristic extends BluetoothObject +{ + public native BluetoothType getBluetoothType(); + public native BluetoothGattCharacteristic clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_CHARACTERISTIC; } + + /** Find a BluetoothGattDescriptor. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattDescriptor find(String UUID, Duration duration) { + BluetoothManager manager = BluetoothManager.getBluetoothManager(); + return (BluetoothGattDescriptor) manager.find(BluetoothType.GATT_DESCRIPTOR, + null, UUID, this, duration); + } + + /** Find a BluetoothGattDescriptor. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are + * waiting for + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattDescriptor find(String UUID) { + return find(UUID, Duration.ZERO); + } + + /* D-Bus method calls: */ + /** Reads the value of this characteristic. + * @return A std::vector<unsgined char> containing the value of this characteristic. + */ + public native byte[] readValue() throws BluetoothException; + + /** + * Enables notifications for the value and calls run function of the BluetoothNotification + * object. It enables notifications for this characteristic at BLE level. + * @param callback A BluetoothNotification<byte[]> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the value + * property. + */ + public native void enableValueNotifications(BluetoothNotification<byte[]> callback); + /** + * Disables notifications of the value and unregisters the callback object + * passed through the corresponding enable method. It disables notications + * at BLE level for this characteristic. + */ + public native void disableValueNotifications(); + + /** Writes the value of this characteristic. + * @param[in] arg_value The data as vector<uchar> + * to be written packed in a GBytes struct + * @return TRUE if value was written succesfully + */ + public native boolean writeValue(byte[] argValue) throws BluetoothException; + + + /* D-Bus property accessors: */ + /** Get the UUID of this characteristic. + * @return The 128 byte UUID of this characteristic, NULL if an error occurred + */ + public native String getUUID(); + + /** Returns the service to which this characteristic belongs to. + * @return The service. + */ + public native BluetoothGattService getService(); + + /** Returns the cached value of this characteristic, if any. + * @return The cached value of this characteristic. + */ + public native byte[] getValue(); + + /** Returns true if notification for changes of this characteristic are + * activated. + * @return True if notificatios are activated. + */ + public native boolean getNotifying(); + + /** Returns the flags this characterstic has. + * @return A list of flags for this characteristic. + */ + public native String[] getFlags(); + + /** Returns a list of BluetoothGattDescriptors this characteristic exposes. + * @return A list of BluetoothGattDescriptors exposed by this characteristic + * NULL if an error occurred + */ + public native List<BluetoothGattDescriptor> getDescriptors(); + + private native void init(BluetoothGattCharacteristic obj); + + private native void delete(); + + private BluetoothGattCharacteristic(long instance) + { + super(instance); + } +} diff --git a/java/tinyb/BluetoothGattDescriptor.java b/java/tinyb/BluetoothGattDescriptor.java new file mode 100644 index 00000000..d5911d23 --- /dev/null +++ b/java/tinyb/BluetoothGattDescriptor.java @@ -0,0 +1,89 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; + +/** + * Provides access to Bluetooth GATT descriptor. Follows the BlueZ adapter API + * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt + */ +public class BluetoothGattDescriptor extends BluetoothObject +{ + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_DESCRIPTOR; } + + /* D-Bus method calls: */ + /** Reads the value of this descriptor + * @return A vector<uchar> containing data from this descriptor + */ + public native byte[] readValue(); + + /** Writes the value of this descriptor. + * @param[in] arg_value The data as vector<uchar> + * to be written packed in a GBytes struct + * @return TRUE if value was written succesfully + */ + public native boolean writeValue(byte[] argValue) throws BluetoothException; + + /** + * Enables notifications for the value and calls run function of the BluetoothNotification + * object. + * @param callback A BluetoothNotification<byte[]> object. Its run function will be called + * when a notification is issued. The run function will deliver the new value of the value + * property. + */ + public native void enableValueNotifications(BluetoothNotification<byte[]> callback); + /** + * Disables notifications of the value and unregisters the callback object + * passed through the corresponding enable method. + */ + public native void disableValueNotifications(); + + /* D-Bus property accessors: */ + /** Get the UUID of this descriptor. + * @return The 128 byte UUID of this descriptor, NULL if an error occurred + */ + public native String getUUID(); + + /** Returns the characteristic to which this descriptor belongs to. + * @return The characteristic. + */ + public native BluetoothGattCharacteristic getCharacteristic(); + + /** Returns the cached value of this descriptor, if any. + * @return The cached value of this descriptor. + */ + public native byte[] getValue(); + + private native void delete(); + + private BluetoothGattDescriptor(long instance) + { + super(instance); + } +} diff --git a/java/tinyb/BluetoothGattService.java b/java/tinyb/BluetoothGattService.java new file mode 100644 index 00000000..49b18739 --- /dev/null +++ b/java/tinyb/BluetoothGattService.java @@ -0,0 +1,99 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; +import java.time.Duration; + +/** + * Provides access to Bluetooth GATT characteristic. Follows the BlueZ adapter API + * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt + */ +public class BluetoothGattService extends BluetoothObject +{ + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_SERVICE; } + + /** Find a BluetoothGattCharacteristic. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattCharacteristic you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattCharacteristic find(String UUID, Duration duration) { + BluetoothManager manager = BluetoothManager.getBluetoothManager(); + return (BluetoothGattCharacteristic) manager.find(BluetoothType.GATT_CHARACTERISTIC, + null, UUID, this, duration); + } + + /** Find a BluetoothGattCharacteristic. If parameter UUID is not null, + * the returned object will have to match it. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter UUID optionally specify the UUID of the BluetoothGattDescriptor you are + * waiting for + * @return An object matching the UUID or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothGattCharacteristic find(String UUID) { + return find(UUID, Duration.ZERO); + } + + /* D-Bus property accessors: */ + + /** Get the UUID of this service + * @return The 128 byte UUID of this service, NULL if an error occurred + */ + public native String getUUID(); + + /** Returns the device to which this service belongs to. + * @return The device. + */ + public native BluetoothDevice getDevice(); + + /** Returns true if this service is a primary service, false if secondary. + * @return true if this service is a primary service, false if secondary. + */ + public native boolean getPrimary(); + + /** Returns a list of BluetoothGattCharacteristics this service exposes. + * @return A list of BluetoothGattCharacteristics exposed by this service + */ + public native List<BluetoothGattCharacteristic> getCharacteristics(); + + private native void delete(); + + private BluetoothGattService(long instance) + { + super(instance); + } +} diff --git a/java/tinyb/BluetoothManager.java b/java/tinyb/BluetoothManager.java new file mode 100644 index 00000000..489ac3eb --- /dev/null +++ b/java/tinyb/BluetoothManager.java @@ -0,0 +1,341 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; +import java.time.Duration; + +public class BluetoothManager +{ + private long nativeInstance; + private static BluetoothManager inst; + + /** + * The thread to watch for nearby devices + */ + private Thread nearbyThread = null; + + static { + try { + System.loadLibrary("tinyb"); + System.loadLibrary("javatinyb"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load.\n" + e); + } + } + + private native static String getNativeAPIVersion(); + + public native BluetoothType getBluetoothType(); + + private native BluetoothObject find(int type, String name, String identifier, BluetoothObject parent, long milliseconds); + + /** Find a BluetoothObject of a type matching type. If parameters name, + * identifier and parent are not null, the returned object will have to + * match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter type specify the type of the object you are + * waiting for, NONE means anything. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the name, identifier, parent or null if not found before + * timeout expires or event is canceled. + */ + public BluetoothObject find(BluetoothType type, String name, String identifier, BluetoothObject parent, Duration timeout) { + return find(type.ordinal(), name, identifier, parent, timeout.toNanos() / 1000000); + } + + + /** Find a BluetoothObject of a type matching type. If parameters name, + * identifier and parent are not null, the returned object will have to + * match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter type specify the type of the object you are + * waiting for, NONE means anything. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @return An object matching the name, identifier and parent. + */ + public BluetoothObject find(BluetoothType type, String name, String identifier, BluetoothObject parent) { + return find(type, name, identifier, parent, Duration.ZERO); + } + + /** Find a BluetoothObject of type T. If parameters name, identifier and + * parent are not null, the returned object will have to match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @parameter timeout the function will return after timeout time, a + * value of zero means wait forever. If object is not found during this time null will be returned. + * @return An object matching the name, identifier, parent or null if not found before + * timeout expires or event is canceled. + */ + public <T extends BluetoothObject> T find(String name, String identifier, BluetoothObject parent, Duration timeout) { + return (T) find(T.class_type().ordinal(), name, identifier, parent, timeout.toNanos() / 1000000); + } + + /** Find a BluetoothObject of type T. If parameters name, identifier and + * parent are not null, the returned object will have to match them. + * It will first check for existing objects. It will not turn on discovery + * or connect to devices. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @return An object matching the name, identifier and parent. + */ + public <T extends BluetoothObject> T find(String name, String identifier, BluetoothObject parent) { + return (T) find(name, identifier, parent, Duration.ZERO); + } + + /** Return a BluetoothObject of a type matching type. If parameters name, + * identifier and parent are not null, the returned object will have to + * match them. Only objects which are already in the system will be returned. + * @parameter type specify the type of the object you are + * waiting for, NONE means anything. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @return An object matching the name, identifier, parent or null if not found. + */ + public BluetoothObject getObject(BluetoothType type, String name, + String identifier, BluetoothObject parent) { + return getObject(type.ordinal(), name, identifier, parent); + } + private native BluetoothObject getObject(int type, String name, + String identifier, BluetoothObject parent); + + /** Return a List of BluetoothObject of a type matching type. If parameters name, + * identifier and parent are not null, the returned object will have to + * match them. Only objects which are already in the system will be returned. + * @parameter type specify the type of the object you are + * waiting for, NONE means anything. + * @parameter name optionally specify the name of the object you are + * waiting for (for Adapter or Device) + * @parameter identifier optionally specify the identifier of the object you are + * waiting for (UUID for GattService, GattCharacteristic or GattDescriptor, address + * for Adapter or Device) + * @parameter parent optionally specify the parent of the object you are + * waiting for + * @return A vector of object matching the name, identifier, parent. + */ + public List<BluetoothObject> getObjects(BluetoothType type, String name, + String identifier, BluetoothObject parent) { + return getObjects(type.ordinal(), name, identifier, parent); + } + private native List<BluetoothObject> getObjects(int type, String name, + String identifier, BluetoothObject parent); + + /** Returns a list of BluetoothAdapters available in the system + * @return A list of BluetoothAdapters available in the system + */ + public native List<BluetoothAdapter> getAdapters(); + + /** Returns a list of discovered BluetoothDevices + * @return A list of discovered BluetoothDevices + */ + public native List<BluetoothDevice> getDevices(); + + /** Returns a list of available BluetoothGattServices + * @return A list of available BluetoothGattServices + */ + public native List<BluetoothGattService> getServices(); + + /** Sets a default adapter to use for discovery. + * @return TRUE if the device was set + */ + public native boolean setDefaultAdapter(BluetoothAdapter adapter); + + /** Gets the default adapter to use for discovery. + * <p> + * System default is the last detected adapter at initialisation. + * </p> + * @return the used default adapter + */ + public native BluetoothAdapter getDefaultAdapter(); + + /** Turns on device discovery on the default adapter if it is disabled. + * @return TRUE if discovery was successfully enabled + */ + public native boolean startDiscovery() throws BluetoothException; + + /** Turns off device discovery on the default adapter if it is enabled. + * @return TRUE if discovery was successfully disabled + */ + public native boolean stopDiscovery() throws BluetoothException; + + /** Returns if the discovers is running or not. + * @return TRUE if discovery is running + */ + public native boolean getDiscovering() throws BluetoothException; + + /** + * When called, each new device found will fire an event to the passed + * listener<p> + * + * This method will create a new thread to handle the discovery process + * which is a simple polling of the current list (getDevices) + * <p> + * + * The thread is stopped when the nearbyStopDiscovery is called<p> + * + * @param listener + * @param pollingRate The polling rate is miliseconds (1000 = 1s) + * @param onlyManufacturerFilled If true, then only if the manufacturer data is filled, the event will be fired + */ + public void startNearbyDiscovery(final BluetoothDeviceDiscoveryListener listener, final int pollingRate, final boolean onlyManufacturerFilled) { + //--- If alreay started, does nothing + if ((nearbyThread != null) && nearbyThread.isAlive()) return; + + //--- Remove all devices to have a clean start + getAdapters().get(0).removeDevices(); + + //--- Start the bluez discovery + startDiscovery(); + + //--- Thread to poll the devices list + nearbyThread = new Thread() { + public void run() { + //--- The key is the address + HashMap<String, BluetoothDevice> discovered = new HashMap<String, BluetoothDevice>(); + try { + while (Thread.interrupted() == false) { + List<BluetoothDevice> list = getDevices(); + for (BluetoothDevice d:list) { + if (!discovered.containsKey(d.getAddress())) { + if (onlyManufacturerFilled) { + if (!d.getManufacturerData().isEmpty()) { + discovered.put(d.getAddress(), d); + if (listener != null) listener.bluetoothDeviceDiscovered(d); + } + + } else { + discovered.put(d.getAddress(), d); + if (listener != null) listener.bluetoothDeviceDiscovered(d); + } + } + } + //--- Polling rate of 1 second + sleep(pollingRate); + } + + } catch (InterruptedException ex) { + //--- Stopped by user or jvm + } + stopDiscovery(); + + } + }; + nearbyThread.start(); + + } + + /** + * Stop the nearby thread + */ + public void stopNearbyDiscovery() { + if ((nearbyThread != null) && nearbyThread.isAlive()) nearbyThread.interrupt(); + + } + + /** + * Interface to implement to receive the device discovery event + */ + public interface BluetoothDeviceDiscoveryListener { + + public void bluetoothDeviceDiscovered(BluetoothDevice device); + } + + private native void init() throws BluetoothException; + private native void delete(); + private BluetoothManager() + { + init(); + } + + /** Returns an instance of BluetoothManager, to be used instead of constructor. + * @return An initialized BluetoothManager instance. + */ + public static synchronized BluetoothManager getBluetoothManager() throws RuntimeException, BluetoothException + { + if (inst == null) + { + String nativeAPIVersion = getNativeAPIVersion(); + String APIVersion = BluetoothManager.class.getPackage().getSpecificationVersion(); + if ( null != APIVersion && APIVersion.equals(nativeAPIVersion) == false) { + String[] nativeAPIVersionCode = nativeAPIVersion.split("\\D"); + String[] APIVersionCode = APIVersion.split("\\D"); + if (APIVersionCode[0].equals(nativeAPIVersionCode[0]) == false) { + if (Integer.valueOf(APIVersionCode[0]) < Integer.valueOf(nativeAPIVersionCode[0])) + throw new RuntimeException("Java library is out of date. Please update the Java library."); + else throw new RuntimeException("Native library is out of date. Please update the native library."); + } + else if (APIVersionCode[0].equals("0") == true) { + if (Integer.valueOf(APIVersionCode[1]) < Integer.valueOf(nativeAPIVersionCode[1])) + throw new RuntimeException("Java library is out of date. Please update the Java library."); + else throw new RuntimeException("Native library is out of date. Please update the native library."); + } + else if (Integer.valueOf(APIVersionCode[1]) < Integer.valueOf(nativeAPIVersionCode[1])) + System.err.println("Java library is out of date. Please update the Java library."); + else System.err.println("Native library is out of date. Please update the native library."); + } + inst = new BluetoothManager(); + inst.init(); + } + return inst; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/tinyb/BluetoothNotification.java b/java/tinyb/BluetoothNotification.java new file mode 100644 index 00000000..79fe800c --- /dev/null +++ b/java/tinyb/BluetoothNotification.java @@ -0,0 +1,35 @@ +/* + * Author: Petre Eftime <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +/* + * This interface is used by notifications. + * Implement a class inheriting BluetoothNotification and pass an object of that type + * to the enable*Notifications function. When a notification of that type is received, + * the run function of the class will be called. + */ +public interface BluetoothNotification<T> { + public void run(T value); +} diff --git a/java/tinyb/BluetoothObject.java b/java/tinyb/BluetoothObject.java new file mode 100644 index 00000000..2afb8c3c --- /dev/null +++ b/java/tinyb/BluetoothObject.java @@ -0,0 +1,93 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +import java.util.*; + +public abstract class BluetoothObject implements Cloneable,AutoCloseable +{ + protected long nativeInstance; + private boolean isValid; + + static { + try { + System.loadLibrary("javatinyb"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load.\n" + e); + } + } + + static BluetoothType class_type() { return BluetoothType.NONE; } + + /** Returns the BluetoothType of this object + * @return The BluetoothType of this object + */ + public native BluetoothType getBluetoothType(); + + /** Returns a clone of the BluetoothObject + * @return A clone of the BluetoothObject + */ + public native BluetoothObject clone(); + + private native void delete(); + private native boolean operatorEqual(BluetoothObject obj); + + protected BluetoothObject(long instance) + { + nativeInstance = instance; + isValid = true; + } + + protected void finalize() + { + close(); + } + + public boolean equals(Object obj) + { + if (obj == null || !(obj instanceof BluetoothObject)) + return false; + return operatorEqual((BluetoothObject)obj); + } + + protected native String getObjectPath(); + + public int hashCode() { + String objectPath = getObjectPath(); + return objectPath.hashCode(); + } + + /** + * Release the native memory associated with this object + * The object should not be used following a call to close + */ + @Override + public synchronized void close() { + if (!isValid) + return; + isValid = false; + delete(); + } +} diff --git a/java/tinyb/BluetoothType.java b/java/tinyb/BluetoothType.java new file mode 100644 index 00000000..ac2fb02e --- /dev/null +++ b/java/tinyb/BluetoothType.java @@ -0,0 +1,34 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +public enum BluetoothType +{ + NONE, ADAPTER, DEVICE, + GATT_SERVICE, GATT_CHARACTERISTIC, + GATT_DESCRIPTOR; + + private long nativeInstance; +} diff --git a/java/tinyb/ObjectArgCallback.java b/java/tinyb/ObjectArgCallback.java new file mode 100644 index 00000000..898c9cbc --- /dev/null +++ b/java/tinyb/ObjectArgCallback.java @@ -0,0 +1,40 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +public class ObjectArgCallback extends BluetoothCallback +{ + private Object callbackArg; + + public ObjectArgCallback(BluetoothObject bObj, Object callbackArg) + { + this.bObj = bObj; + this.callbackArg = callbackArg; + } + + public void run() + { + } +} diff --git a/java/tinyb/ObjectArrayArgCallback.java b/java/tinyb/ObjectArrayArgCallback.java new file mode 100644 index 00000000..b0d83410 --- /dev/null +++ b/java/tinyb/ObjectArrayArgCallback.java @@ -0,0 +1,40 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb; + +public class ObjectArrayArgCallback extends BluetoothCallback +{ + private Object[] callbackArg; + + public ObjectArrayArgCallback(BluetoothObject bObj, Object[] callbackArg) + { + this.bObj = bObj; + this.callbackArg = callbackArg; + } + + public void run() + { + } +} diff --git a/java/tinyb/TransportType.java b/java/tinyb/TransportType.java new file mode 100644 index 00000000..ab71a1cb --- /dev/null +++ b/java/tinyb/TransportType.java @@ -0,0 +1,19 @@ +package tinyb; + +/** + * TransportType determines type of bluetooth scan. + */ +public enum TransportType { + /** + * interleaved scan + */ + AUTO, + /** + * BR/EDR inquiry + */ + BREDR, + /** + * LE scan only + */ + LE +} |