Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino.

Overview

Android Smooth Bluetooth

Android Arsenal

Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino.

Getting Started

Add Gradle dependency:

dependencies {
   compile 'io.palaima:smoothbluetooth:0.1.0'
}

You can try the SNAPSHOT version:

dependencies {
   compile 'io.palaima:smoothbluetooth:0.2.0-SNAPSHOT'
}

Make sure to add the snapshot repository:

repositories {
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

Usage

1. Declare bluetooth permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

2. Define SmoothBluetooth instance

private SmoothBluetooth mSmoothBluetooth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSmoothBluetooth = new SmoothBluetooth(Context context);
}

there are possible overrides:

mSmoothBluetooth = new SmoothBluetooth(Context context, SmoothBluetooth.Listener listener);

or

mSmoothBluetooth = new SmoothBluetooth(Context context, ConnectionTo connectionTo, Connection connection, SmoothBluetooth.Listener listener);

ConnectionTo defines to what type of device to connect with (by default it is ConnectionTo.OTHER_DEVICE which means microcontrollers like Arduino)

ConnectionTo.ANDROID_DEVICE
ConnectionTo.OTHER_DEVICE

Connection defines what type of connection will be (be default it is Connection.SECURE)

Connection.SECURE
Connection.INSECURE

3. Define SmoothBluetooth.Listener

After that you must define SmoothBluetooth.Listener which catches all bluetooth related events and pass it to SmoothBluetooth constructor when defining its instance or if you already have SmoothBluetooth instance you can pass listener via setter setListener(SmoothBluetooth.Listener listener)

private SmoothBluetooth.Listener mListener = new SmoothBluetooth.Listener() {
    @Override
    public void onBluetoothNotSupported() {
        //device does not support bluetooth
    }

    @Override
    public void onBluetoothNotEnabled() {
        //bluetooth is disabled, probably call Intent request to enable bluetooth
    }

    @Override
    public void onConnecting(Device device) {
        //called when connecting to particular device
    }

    @Override
    public void onConnected(Device device) {
       //called when connected to particular device
    }

    @Override
    public void onDisconnected() {
        //called when disconnected from device
    }

    @Override
    public void onConnectionFailed(Device device) {
        //called when connection failed to particular device
    }

    @Override
    public void onDiscoveryStarted() {
        //called when discovery is started
    }

    @Override
    public void onDiscoveryFinished() {
        //called when discovery is finished
    }

    @Override
    public void onNoDevicesFound() {
        //called when no devices found
    }

    @Override
    public void onDevicesFound(final List<Device> deviceList,
            final BluetoothHelper.ConnectionCallback connectionCallback) {
        //receives discovered devices list and connection callback
        //you can filter devices list and connect to specific one
        //connectionCallback.connectTo(deviceList.get(position));
    }

    @Override
    public void onDataReceived(int data) {
        //receives all bytes
    }
};

4. Try to connect

After everything is set up and all is left to do is try to connect

mSmoothBluetooth.tryConnection();

tryConnection() is linked with SmoothBluetooth.Listener so all connection events will be passed to listener. By default if everything is ok, immediately returns all paired devices to SmoothBluetooth.Listener's onDevicesFound

5. Discovering

mSmoothBluetooth.doDiscovery();

Call doDiscovery() method which search for unpaired devices and returns them to SmoothBluetooth.Listener's onDevicesFound

6. Sending data

mSmoothBluetooth.send(byte[] data, boolean CRLF);

or

mSmoothBluetooth.send(String data, boolean CRLF);

boolean CRLF indicates if data is need to be send with ending by LF and CR or not. if you do not need CRLF at the end there are some overrides with CRLF = false

mSmoothBluetooth.send(byte[] data);
mSmoothBluetooth.send(String data);

6. Disconnect

mSmoothBluetooth.disconnect();

7. Do not forget to stop

For instance in your activity where SmoothBluetooth is defined you must call stop()

@Override
protected void onDestroy() {
    super.onDestroy();
    mSmoothBluetooth.stop();
}

Sample

You can clone the project and compile it yourself (it includes a sample). MainActivity

Contributing

Want to contribute? You are welcome! Note that all pull request should go to dev branch.

Developed By

Credits

Credit to Aidan Follestad's Material Dialogs library.

License

Copyright 2015 Mantas Palaima.

Licensed 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.
Comments
  • Added autoconnect method

    Added autoconnect method

    I am using my fork in a project so if you want to accept this pr just let me know and I will change the installation link in Readme file. Meanwhile, if someone wants to use this method see my fork.

    opened by yashx 0
  • In android arsenal DeviceAdapter raises a proble due to incorrect usage.

    In android arsenal DeviceAdapter raises a proble due to incorrect usage.

    Use it in this way might solve the issue.

    new MaterialDialog.Builder(this) .title(R.string.socialNetworks) // second parameter is an optional layout manager. Must be a LinearLayoutManager or GridLayoutManager. .adapter(new ButtonItemAdapter(this, R.array.socialNetworks), null) .show();

    opened by Justnowstarted 0
  • No way to connect to a specific device

    No way to connect to a specific device

    Not always do we want to let the device connect to just any device found around.

    Most of the applications would need to connect to specific devices around, may it be user's choice or preconfigured to control one specific device.

    opened by tfKamran 0
  • Problem to connect : no BluetoothService while connect method is called

    Problem to connect : no BluetoothService while connect method is called

    I read your source code to understand why it does not connect smoothly and I found that no BluetoothService is avalaible before you call connect(), but for the case of a server you don't have to need to search and try to pair to another device. The client has to connect to the server. Example : device1 is my server, device2 is my client. Device1 is visible, device2 tries to connect to device1 but it fails. We can repeat this operation several times it will always fail. But if device1 tries to connect to device2 it will work because both have called connect(), so both have already a BluetoothService instance. My solution is to add these lines

    if (!isServiceAvailable()) {
                setupService();
    }
    startService(mIsAndroid, mIsSecure); 
    

    in the end of the constructor, and it works correctly (I already tested it).

    Hope it will help LAcrym0

    opened by remi-ollivier 0
  • Failed to connect

    Failed to connect

    Hello sir, I am very new to android and I tried using your library. After selecting the desired bluetooth device I am getting the same result "Failed to connect to device". as shown in your documentation, I also tried to connect to an android phone by setting connectionto.Android devices in the constructor, but all in vain. Can you please help?

    opened by tanveer27 0
Owner
Mantas Palaima
Mantas Palaima
Kotlin Asynchronous Bluetooth Low Energy provides a simple Coroutines-powered API for interacting with Bluetooth Low Energy devices.

Kotlin Asynchronous Bluetooth Low Energy provides a simple Coroutines-powered API for interacting with Bluetooth Low Energy devices.

JUUL Labs 275 Sep 14, 2021
Android Bluetooth Helper Library, Bluetooth Device Finder

Bluetooth Helper Allows you to access the Bluetooth of your mobile device, manage turn-on - turn off, and discover bluetooth devices around you. Getti

Tolga Bolatcan 44 Jul 15, 2022
BluePass extracts two factor authentication codes (2FA) from SMS and sends them to a paired device via Bluetooth RFCOMM.

BluePass extracts two factor authentication codes (2FA) from SMS and sends them to a paired device via Bluetooth RFCOMM.

Manuel Huber 15 Dec 4, 2022
An Android Library for handling Bluetooth Low Energy on Android Easy

An Android Library for handling Bluetooth Low Energy on Android Easy

Leandro SQ 42 Jan 3, 2023
An Android library that solves a lot of Android's Bluetooth Low Energy problems

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.

Nordic Semiconductor 1.4k Jan 7, 2023
A reactive, interface-driven central role Bluetooth LE library for Android

RxCentralBle RxCentralBle provides a simple reactive paradigm for connecting to and communicating with Bluetooth LE peripherals from the central role.

Uber Open Source 198 Nov 29, 2022
A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Andrew Reed 220 Dec 28, 2022
BLESSED Coroutines, a Bluetooth Low Energy (BLE) library for Android using Kotlin Coroutines

BLESSED for Android with Coroutines - BLE made easy BLESSED is a very compact Bluetooth Low Energy (BLE) library for Android 8 and higher, that makes

Martijn van Welie 82 Jan 1, 2023
RxBle: Use Android Bluetooth API in Rx way

RxBle: Use Android Bluetooth API in Rx way A lightweight encapsulation of Android Bluetooth API. Use Android Bluetooth API in Rx way. Support multiple

null 3 Dec 2, 2022
This library allows for easy access to a Bluetooth LE device's AdRecord and RSSI value. It offers additional functionality for iBeacons.

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

Alexandros Schillings 843 Dec 13, 2022
Kotlin Asynchronous Bluetooth Low-Energy

Kable Kotlin Asynchronous Bluetooth Low Energy provides a simple Coroutines-powered API for interacting with Bluetooth Low Energy devices. Usage is de

JUUL Labs 493 Dec 25, 2022
A non-trivial Bluetooth LE app using Kable and app architecture best practices

kable_mvvm_demo The intention of this project is to demonstrate a non-trivial Bluetooth LE app using Kable and app architecture best practices. ⚠️ The

Chris Laplante 14 Aug 18, 2022
Open-source weight and body metrics tracker, with support for Bluetooth scales

Open-source weight and body metrics tracker, with support for Bluetooth scales

OliE 1.3k Jan 4, 2023
Simple bluetooth flutter project

bluetooth_simple Simple bluetooth implementation. Getting Started This project is a starting point for a Flutter application. A few resources to get y

Aleksey Vasiliev 0 Nov 25, 2021
The purpose is to share the Internet capability of one device to the entire Bluetooth LAN.

bluenet The purpose is to share the Internet capability of one device to the entire Bluetooth LAN. To make a prototype of a soft bus, or actually, I w

yunlong.wen 1 Jun 28, 2022
User-friendly Lightweight TPM Remote Attestation over Bluetooth

Ultrablue Ultrablue (User-friendly Lightweight TPM Remote Attestation over Bluetooth) is a solution to allow individual users to perform boot state at

ANSSI 32 Jan 2, 2023
implementation ClickListener via HOF

ClickListenerExample implementation ClickListener via HOF Simple project to represent how to change regular listeners callback to higher order functio

null 0 Nov 28, 2021
Allows Android apps to interact with BLE beacons

Android Beacon Library An Android library providing APIs to interact with beacons. Please visit the project website for how to use this library. IMPOR

AltBeacon 2.7k Dec 28, 2022
A simple, lightweight library intended to take away some of the cruft and tediousness of using the Android BLE.

Blueteeth What Is Blueteeth? Blueteeth is a simple, lightweight library intended to take away some of the cruft and tediousness of using the Android B

Robot Pajamas 103 Nov 26, 2022