Java Code Examples for javax.bluetooth.LocalDevice#getLocalDevice()

The following examples show how to use javax.bluetooth.LocalDevice#getLocalDevice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothServer.java    From ShootOFF with GNU General Public License v3.0 7 votes vote down vote up
private Optional<String> getLocalAddress() {
	try {
		final LocalDevice localDevice = LocalDevice.getLocalDevice();

		// Insert colons into the address because android needs them
		final StringBuilder addressBuilder = new StringBuilder();
		final String originalAddress = localDevice.getBluetoothAddress();
		for (int i = 0; i < originalAddress.length(); i++) {
			addressBuilder.append(originalAddress.charAt(i));
			if (i > 0 && i < originalAddress.length() - 1 && i % 2 != 0) addressBuilder.append(':');
		}

		return Optional.of(addressBuilder.toString());
	} catch (BluetoothStateException e) {
		logger.error("Failed to access local bluetooth device to fetch its address. Ensure the "
				+ "system's bluetooth service is started with \"sudo systemctl start bluetooth\" "
				+ "and the bluetooth stack is on in the system settings", e);
		return Optional.empty();
	}
}
 
Example 2
Source File: BluetoothDiscoveryUtil.java    From Ardulink-2 with Apache License 2.0 5 votes vote down vote up
private static LocalDevice getLocalDevice() {
	try {
		return LocalDevice.getLocalDevice();
	} catch (BluetoothStateException e) {
		throw propagate(e);
	}
}