Courtesy: GitHub

Android BluetoothAdvertisements Sample

Sample demonstrating how to advertise small amounts of data using the Bluetooth Low Energy API. Also demonstrates how to scan for those Advertisements. (requires 2 devices to see full operation)

Introduction

This sample demonstrates use of the Bluetooth Low Energy (BLE) Advertisement and Scanning APIs. The functionality is split into two fragments - one for Advertising, one for Scanning.

ScannerFragment activates BLE Scanning for 5 seconds and displays a list of found devices which are advertising using this sample. It shows the device type, Bluetooth address, and when it was last seen. User can refresh to scan again and update the list.

AdvertiserFragment allows the user to toggle BLE Advertising of that device. It broadcasts basic information about the device along with a UUID specific to this app so the ScannerFragment on other devices can filter by it.

Note: A device cannot detect its own BLE advertisements. You will need two devices to see this sample in action.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-BluetoothAdvertisements

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:support-v13:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compileSdkVersion 26

minSdkVersion 21

targetSdkVersion 26

package com.example.android.bluetoothadvertisements

versionCode 1

versionName 1.0

uses-permission

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH

MainActivity

Setup display fragments and ensure the device supports Bluetooth.

AdvertiserService

Manages BLE Advertising independent of the main app. If the app goes off screen (or gets killed completely) advertising can continue because this Service is maintaining the necessary Callback in memory.

A global variable to let AdvertiserFragment check if the Service is running without needing to start or bind to it. This is the best practice method as defined here: https://groups.google.com/forum/#!topic/android-developers/jEvXMWgbgzE

Length of time to allow advertising before automatically shutting off. (10 minutes)

Note that onDestroy is not guaranteed to be called quickly or at all. Services exist at the whim of the system, and onDestroy can be delayed or skipped entirely if memory need is critical.

Required for extending service, but this will be a Started Service only, so no need for binding.

Get references to system Bluetooth objects if we don't have them already.

Starts a delayed Runnable that will cause the BLE Advertising to timeout and stop after a set amount of time.

Starts BLE Advertising.

Move service to the foreground, to avoid execution limits on background processes. * Callers should call stopForeground(true) when background work is complete.

Stops BLE Advertising.

Returns an AdvertiseData object which includes the Service UUID and Device Name.

Note: There is a strict limit of 31 Bytes on packets sent over BLE Advertisements. This includes everything put into AdvertiseData including UUIDs, device info, & arbitrary service or manufacturer data. Attempting to send packets over this limit will result in a failure with error code AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE. Catch this error in the onStartFailure() method of an AdvertiseCallback implementation.

Returns an AdvertiseSettings object set to use low power (to help preserve battery life) and disable the built-in timeout since this code uses its own timeout runnable.

Custom callback after Advertising succeeds or fails to start. Broadcasts the error code in an Intent to be picked up by AdvertiserFragment and stops this Service.

Builds and sends a broadcast intent indicating Advertising has failed. Includes the error code as an extra. This is intended to be picked up by the {@code AdvertiserFragment}.