• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android library for simplifying bluetooth usage.

SimpleBluetoothLibrary

Android Arsenal

Description

This library makes it easy for you to implement bluetooth in your Android app. The SimpleBlueooth class handles all the hard work for you and all you have to do is make a few method calls.

Dependency

repositories {
    ....
    maven {url "https://jitpack.io"}
}
dependencies {
    ....
    compile  'com.github.DeveloperPaul123:SimpleBluetoothLibrary:1.5.1'
}

Requirements

Min SDK Level is 14 or Android IceCreamSandwich

Usage

This library provides a BaseBluetoothActivity that you inherit from to easily take care of:

  • Enabling bluetooth
  • Scanning for new devices.
  • Connecting to a device.
  • Receiving data from the device.
  • Sending data to the device.

As an example:

public class TestActivity extends BaseBluetoothActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set your content view here. 
    }

    @Override
    public void onBluetoothEnabled() {
        super.onBluetoothEnabled();
        //this is always called, when bluetooth is enabled.
        //by default the activity will start a scan. If you don't want this
        //delete the super call. 
    }

    @Override
    public void onDeviceSelected(String macAddress) {
        super.onDeviceSelected(macAddress);
        //this is called when a device is selected from the scan activity. By default,
        //the selected device will be connected to. If you don't want this, delete the super call.
    }

    @Override
    public void onBluetoothDataReceived(byte[] bytes, String data) {
       //called when data is received from the device. 
    }

    @Override
    public void onDeviceConnected(BluetoothDevice device) {

    }

    @Override
    public void onDeviceDisconnected(BluetoothDevice device) {

    }

    @Override
    public void onDiscoveryStarted() {

    }

    @Override
    public void onDiscoveryFinished() {

    }
}

Alternatively you can use the SimpleBluetooth class yourself:

public class MainActivity extends Activity {

    private SimpleBluetooth simpleBluetooth;
    private static final int SCAN_REQUEST = 119;
    private static final int CHOOSE_SERVER_REQUEST = 120;
    //...other code....//
    private String curMacAddress;

    @Override
    protected void onResume() {
        super.onResume();
        simpleBluetooth = new SimpleBluetooth(this, new SimpleBluetoothListener() {

            @Override
            public void onBluetoothDataReceived(byte[] bytes, String data) {
                //read the data coming in.
            }

            @Override
            public void onDeviceConnected(BluetoothDevice device) {
                //a device is connected so you can now send stuff to it
                
            }

            @Override
            public void onDeviceDisconnected(BluetoothDevice device) {
                // device was disconnected so connect it again?
               
            }

            @Override
            public void onDiscoveryStarted() {

            }

            @Override
            public void onDiscoveryFinished() {

            }
        });
        simpleBluetooth.initializeSimpleBluetooth();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //...other code...//
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        } else if (id == R.id.scan) {
            simpleBluetooth.scan(SCAN_REQUEST);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == SCAN_REQUEST || requestCode == CHOOSE_SERVER_REQUEST) {

            if(resultCode == RESULT_OK) {

                curMacAddress = data.getStringExtra(DeviceDialog.DEVICE_DIALOG_DEVICE_ADDRESS_EXTRA);
                if(requestCode == SCAN_REQUEST) {
                    simpleBluetooth.connectToBluetoothDevice(curMacAddress);
                } else {
                    simpleBluetooth.connectToBluetoothServer(curMacAddress);
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        simpleBluetooth.endSimpleBluetooth();
    }
}

Finally, the library allows for an alternative way to receive data. Say you're using an Arduino or something similar that is sending out a newline character at the end of every loop. You can read this data using a buffered input type through the following line:

simpleBluetooth.setInputStreamType(BluetoothUtility.InputStreamType.BUFFERED);

This will case the input type to change and you should receieve the data line by line.

Additionally, this library allows for the creation and connection to a bluetooth server. Simply call:

simpleBluetooth.createBluetoothServerConnection();

and on the connecting device call:

//curMacAddress is the address of the server device.
simpleBluetooth.connectToBluetoothServer(curMacAddress);

Finally, the library can handle A2DP protocols, although this has not yet been tested.

Developed By

Paul T

Credit to afollestad for his material-dialog library

License

Copyright 2014 - 2017 Paul T

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.

More Repositories

1

thread-pool

A modern, fast, lightweight thread pool library based on C++20
C++
323
star
2

eventbus

A simple, header only event bus library written in modern C++17.
C++
100
star
3

FilePickerLibrary

Simple library that allows for the picking of files and/or directories.
Java
79
star
4

MaterialLibrary

This is a simple library that provides some unique components for aiding in making apps comply with the material design guidelines.
Java
44
star
5

CMakeInstallExample

Installation example for a C++ project (Windows) with Cmake.
CMake
30
star
6

periodic-function

Small header only library to call a function at a specific time interval.
CMake
28
star
7

AnalogStickLibrary

A simple library that provides an anlog stick to use for on screen controls.
Java
23
star
8

rayray

Rayray is a baby ray tracer written in C++.
C++
18
star
9

genetic

A performant and flexible genetic algorithm implemented in C++20/23.
C++
15
star
10

Optimum

Accessible and modern implementations of common optimization algorithms.
C++
14
star
11

StarView

๐ŸŒŸSimple custom star view for Android๐ŸŒŸ
Kotlin
9
star
12

delaunaypp

A fluent and easy to use implementation of Delaunay triangulation in C++.
C++
8
star
13

GettingStartedWithQt5Cmake-Tutorial

C++
7
star
14

modern-cpp-challenge

Code implementations for various C++ challenges.
C++
3
star
15

Teensy-Quadcopter

Teensy based quadcopter project.
Arduino
3
star
16

feline-facts

Simple flutter app that shows cat facts.
Dart
3
star
17

CMakeQt5Example

CMake
2
star
18

DeveloperPaul123

2
star
19

CUDA

CUDA Samples and HW from Udacity CS344 Class
C
2
star
20

lyric-analysis

Simple word analysis of music lyrics of a given artist.
Python
1
star
21

CMakeWizard

CMake configuration tool.
C++
1
star