Home

Awesome

Bluetooth LE Library for Android

This library allows for easy access to a Bluetooth LE device's Advertisement Records. It also offers:

This will only work on devices with Android 4.3 (API Level 18) and above.

<a href='https://play.google.com/store/apps/details?id=uk.co.alt236.btlescan&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1'> <img alt='Get it on Google Play' height=100 src='https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png'/> </a>

Including the Library in Your Project

This project is available as an artifact for use with Gradle. To use that, add the following blocks to your build.gradle file:

	repositories {
		maven {
			url "https://dl.bintray.com/alt236/maven"
		}
	}

	dependencies {
		compile 'uk.co.alt236:bluetooth-le-library-android:1.0.0'
	}

If you really need a Jar file, fork the project and execute ./gradlew clean build generateRelease at the root of the project. This will create a zip file under <PROJECT_ROOT>/library/build/ the Jar can be found inside.

Using the Library

In the onLeScan() method of your BluetoothAdapter.LeScanCallback() create a new BluetoothLeDevice with the given information.

For example:

	private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

		@Override
		public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

			final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, scanRecord, System.currentTimeMillis());

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDeviceStore.addDevice(deviceLe);
					mLeDeviceListAdapter.replaceData(mDeviceStore.getDeviceList());
				}

			});
		}
	};

Device Properties

Once you have created a device, you can access the following methods:

Note: The Running Average RSSI is not updated automatically (i.e. the library does not monitor on its own in the background). To add another measurement, you need to call updateRssiReading(long timestamp, int rssiReading).

Accessing the Advertisement (Ad) Records

Once you've created a BluetoothLe device, you can access the AdRecord store via the leDevice.getAdRecordStore(). Once you have the AdRecordStore you can use the following methods:

Note: Record numbers are declared in the Bluetooth 4 spec which can be found here. They are also declared as constants in AdRecord.java.

Fun with iBeacons

You can check if a device is an iBeacon by using BeaconUtils.getBeaconType(BluetootLeDevice device). Once you have confirmed that it is, you can create a new IBeaconDevice via the IBeaconDevice constructor.

Example Flow:

	final BluetoothLeDevice device = ... // A generic BLE device

	if (BeaconUtils.getBeaconType(device) == BeaconType.IBEACON) {
		final IBeaconDevice iBeacon = new IBeaconDevice(device);
		// DO STUFF
	}

An IBeaconDevice extends BluetoothLeDevice, so you still have access to the same methods as before. In addition you can do the following:

Lookup Functions

You can also lookup values and convert them to human friendly strings:

Note: The data can be found as ODS (Open Office Spreadsheets) in the documents folder.

Library Changelog

Sample Application Changelog

Permission Explanation

You will need the following permissions to access the Bluetooth Hardware

In addition one of the following is needed from API 23 and above to scan for BT LE devices:

TODO

Links

Credits

Author: Alexandros Schillings.

All logos are the property of their respective owners.

The code in this project is licensed under the Apache Software License 2.0.

Copyright (c) 2014-2017 Alexandros Schillings.