• This repository has been archived on 27/Jan/2021
  • Stars
    star
    1,694
  • Rank 26,434 (Top 0.6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth

WARNING: This project is no longer being maintained

Build Status Android-BluetoothSPPLibrary

BluetoothSPP Library

Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller or android device via bluetooth.

This libraly include all important methods for serial port profile on bluetooth communication. It has built-in bluetooth device list.

Feature

• It's very easy to use

• Solve the lack of data like as "abcdefg" which divided to "abc" and "defg" when receive these data

• Auto add LF (0x0A) and CR (0x0D) when send data to connection device

• No need to create layout for bluetooth device list to select device for connection. You can use built-in layout in this library and you can customize layout if you want

• Auto connection supported

• Listener for receive data from connection device

Download

Maven

<dependency>
  <groupId>com.akexorcist</groupId>
  <artifactId>bluetoothspp</artifactId>
  <version>1.0.0</version>
</dependency>

Gradle

compile 'com.akexorcist:bluetoothspp:1.0.0'

Simple Usage

• Import this library to your workspace and include in to your android project For Eclipse ADT : Download this library and import into your workspace and include this library to your project For Android Studio : Use Gradle to download this library from Maven

• Declare permission for library

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

• Declare BluetoothSPP like this

BluetoothSPP bt = new BluetoothSPP(Context);

• Check if bluetooth is now available

if(!bt.isBluetoothAvailable()) {
    // any command for bluetooth is not available
}

• Check if bluetooth is not enable when activity is onStart

public void onStart() {
    super.onStart();
    if(!bt.isBluetoothEnable()) {
        // Do somthing if bluetooth is disable
    } else {
        // Do something if bluetooth is already enable
    }
}

• if bluetooth is ready call this method to start service

For connection with android device

bt.startService(BluetoothState.DEVICE_ANDROID);

Communicate with android

For connection with any microcontroller which communication with bluetooth serial port profile module

bt.startService(BluetoothState.DEVICE_OTHER);

Communicate with microcontroller

Bluetooth module with SPP

• Stop service with

bt.stopService();

• Intent to choose device activity

Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);

don't forget declare library activty like this

<activity android:name="app.akexorcist.bluetoothspp.DeviceList" />

• After intent to choose device activity and finish that activity. You need to check result data on onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
        if(resultCode == Activity.RESULT_OK)
            bt.connect(data);
    } else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
        if(resultCode == Activity.RESULT_OK) {
            bt.setupService();
            bt.startService(BluetoothState.DEVICE_ANDROID);
            setup();
        } else {
            // Do something if user doesn't choose any device (Pressed back)
        }
    }
}

• If you want to send any data. boolean parameter is mean that data will send with ending by LF and CR or not. If yes your data will added by LF & CR

bt.send("Message", true);

or

bt.send(new byte[] { 0x30, 0x38, ....}, false);

• Listener for data receiving

bt.setOnDataReceivedListener(new OnDataReceivedListener() {
    public void onDataReceived(byte[] data, String message) {
        // Do something when data incoming
    }
});

• Listener for bluetooth connection atatus

bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {
    public void onDeviceConnected(String name, String address) {
        // Do something when successfully connected
    }

    public void onDeviceDisconnected() {
        // Do something when connection was disconnected
    }

    public void onDeviceConnectionFailed() {
        // Do something when connection failed
    }
});

• Listener when bluetooth connection has changed

bt.setBluetoothStateListener(new BluetoothStateListener() {                
    public void onServiceStateChanged(int state) {
        if(state == BluetoothState.STATE_CONNECTED)
            // Do something when successfully connected
        else if(state == BluetoothState.STATE_CONNECTING)
            // Do something while connecting
        else if(state == BluetoothState.STATE_LISTEN)
            // Do something when device is waiting for connection
        else if(state == BluetoothState.STATE_NONE)
            // Do something when device don't have any connection
    }
});

• Using auto connection

bt.autoConnect("Keyword for filter paired device");

• Listener for auto connection

bt.setAutoConnectionListener(new AutoConnectionListener() {
    public void onNewConnection(String name, String address) {
        // Do something when earching for new connection device
    }
            
    public void onAutoConnectionStarted() {
        // Do something when auto connection has started
    }
});

• Customize device list's layout by create layout which include

list view with id name = "list_devices"

button with id name = "button_scan"

Example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FDE182" >

    <ListView
        android:id="@+id/list_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:smoothScrollbar="true" />
        
    <Button
        android:id="@+id/button_scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:padding="20dp"
        android:background="#FFC600"
        android:text="SCAN"
        android:textSize="25sp"
        android:textColor="#7A481B"
        android:textStyle="bold" />
        
</RelativeLayout>

Custom Device List Layout

But if you don't need to create layout file. You just want to change only text on device list layout. You can use bundle to change text on device list

Custom Device List Text

Custom Device List Text

Custom Device List Text

Custom Device List Text

Intent intent = new Intent(getApplicationContext(), DeviceList.class);
intent.putExtra("bluetooth_devices", "Bluetooth devices");
intent.putExtra("no_devices_found", "No device");
intent.putExtra("scanning", "กำลังทำการค้นหา");
intent.putExtra("scan_for_devices", "Search");
intent.putExtra("select_device", "Select");
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);

Custom Device List Text

What's next?

  • Connection Dialog
  • Add Insecure Connection
  • Fix bug on this issue #21
  • Merge the code from #14 for a problem of auto connection
  • Human Readable Log #19

License

Copyright (c) 2014 Akexorcist

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

RoundCornerProgressBar

[Android] Round Corner Progress Bar Library for Android
Kotlin
2,443
star
2

Localization

[Android] In-app language changing library
Kotlin
979
star
3

GoogleDirectionLibrary

[Android] Library for Google Direction API for Google Maps Android API v2
Java
574
star
4

ScreenshotDetection

[Android] Screenshot detection while user using your app
Kotlin
155
star
5

SnapTimePicker

[Android] Another Material Time Picker
Kotlin
127
star
6

GoogleDirectionAndPlaceLibrary

[Android] Library for Google Direction API and Google Place API for Google Maps Android API v2
Java
117
star
7

IOIO-CameraRobot

[Android] IOIO Robot Car with Real-time Camera from Android Device that Controlled with Another Android Device via WiFi
Java
74
star
8

Droid2JoyStick

[Android] Using android device as a gamepad to your PC or another android device.
Java
56
star
9

ConcatAdapterMultipleLayoutManager

[Android] Using ConcatAdapter with multiple LayoutManager in single RecyclerView
Kotlin
37
star
10

Glassmorphism

[Android] Glassmorphism UI experiment project
Kotlin
25
star
11

DeviceInformation

[Android] Collect android information for developer
Java
22
star
12

FileWriterCompat

[Android] File writing helper library for API Level 21+
Kotlin
18
star
13

ScreenOrientationHelper

[Android] Screen orientation event listener helper for activity in Android
Java
17
star
14

photo-on-cover-for-galaxy-z-flip5

[Android] Put your favorite photo on your cover screen and give your phone a personal touch.
Kotlin
15
star
15

SimpleTCPLibrary

[UNMAINTAINED][Android] TCP Library for android to communicate with other android device or any embedded board via TCP protocol
Java
13
star
16

Android-Sensor-Light

[Android] Using light sensor in Android devices
Java
11
star
17

INEX-RFIDReader

Communicate with Serial RFID reader via USB Host on Android
Java
10
star
18

RecyclerView-DashLine

[Android] How to implement the RecyclerView with dash line between item
Kotlin
10
star
19

CameraX-Sample

[Android] Example of CameraX in Android Jetpack Library
Kotlin
10
star
20

workstation-diagram

[Kotlin Multiplatform] My workstation's interactive diagram
Kotlin
10
star
21

ScreenChecker

[Android] Screen Checker application for development information
Kotlin
9
star
22

Android-CameraAutoFocus

Java
9
star
23

backdrop

Video and audio projection app for your streaming content on macOS
Kotlin
9
star
24

GroupFocusable

Android Custom View for prevent the view behind on-screen keyboard when edit text is focused
Kotlin
9
star
25

KnoxActivator

[Android] Samsung Knox Standard activation helper library for Android
Java
8
star
26

Android-Sensor-Gyroscope

[Android] Using gyroscope in Android devices
Java
8
star
27

Example-SamsungSDK

[Android] Example of Samsung SDK and KNOX SDK in Android
Java
7
star
28

Android-DeviceInformation

[Android] Device information for developer
Java
7
star
29

RecyclerView-ListAdapter

[Android] ListAdapter in RecyclerView
Kotlin
7
star
30

HandleStateChangesInCustomView

[Android] Handle state changes in custom view and inherited custom view
Kotlin
7
star
31

ImageResize

Effective image resizing in Android with BitmapFactory - Benchmark included
Kotlin
6
star
32

ArchitectureComponents-Repository

[Android] Repository in Android Architecture Components
Java
6
star
33

DialogExperiment

[Android] Best practice for Dialog creation in Android
Java
6
star
34

CameraSample

[Android] Example of Camera API v1 and v2 implementation
Kotlin
6
star
35

Android-AutoHideMenu

Auto Hide Menu Bar like as Google+ and Facebook
Java
6
star
36

Android-BluetoothSPP

Bluetooth Serial Port Profile library
Java
5
star
37

SleepingForLess

[Android] Sleeping For Less Reader for Android
5
star
38

coordinator-layout-catalogue

[Android] Example of coordinator layout implementation in different condition
Kotlin
5
star
39

AndroidOreo-Features

[Android] Android 8.0 Oreo Features
Java
5
star
40

Android-SplashScreen

Real splash screen for application
Java
4
star
41

ComplexRecyclerView

[Android] How I solve the problem when we have to handle very complex recycler view in Android
Kotlin
4
star
42

InstantDialog

[Android] Because I'm so boring about the Android dialog's boilerplate code
Kotlin
4
star
43

RootCheckerForSamsung

[Android] Root checking and running on Samsung devices
Java
4
star
44

LovelyRecyclerView

[Android] RecyclerView with complex layout and data
Java
4
star
45

Dagger2-Sample

[Android] Dagger 2 in Android Project with AAC
Kotlin
4
star
46

NewMvvmAac

[Android] New MVVM pattern with AAC that crystallize from iosched
Kotlin
3
star
47

Android-Sensor-Accelerometer

[Android] Using accelerometer in Android devices
Java
3
star
48

FirebaseAndroidCodelabs

[Android] Firebase project showcase in I/O Extended Thailand 2016
Java
3
star
49

RecyclerBackgroundSupportView

[Android] Add Image View behind RecyclerView with scrollable support
Kotlin
3
star
50

deep-link-generator

[React] Feel hard to test the deep link in your app? Yes, me too!
JavaScript
3
star
51

IntroToAndroidDevelopment

Android Course by Skooldio
Kotlin
3
star
52

ProgressNotification

[Android] Example of progress notification when do some asynchronous things
Kotlin
3
star
53

Android-ArchComponents

[Android] Example of Android Architecture Components
Java
2
star
54

BookApp

[Android] Sample application for communication with web service
Kotlin
2
star
55

Android-TextToSpeechThai

Example for Text to Speech with Thai language
Java
2
star
56

Android-BluetoothChat

Java
2
star
57

Android-GoogleAdMob

Using admob with google play service on android
Java
2
star
58

IOIO-Camera360

[Android] Change your IOIO board to 360 degree rotation photo capture - http://www.youtube.com/watch?v=js_ZpQCx-o8
Java
2
star
59

Shaky-FirebaseDevDay2018

[Android] Shaking game in Firebase Dev Day 2018 Bangkok
Kotlin
2
star
60

android-test-ui-hierarchy-to-json

[React] Make UI Hierarchy from Android testing more readable by parsing to JSON format
JavaScript
1
star
61

TextureViewVideoScaler

[Android] Very lightweight helper class to resize the video in texture view to fit center or crop center
Java
1
star
62

IOIO-TakeSnapshot

Java
1
star
63

sleepingforless-article-indexing-blogger

[NodeJS] Indexing the articles in Sleeping For Less by Blogger - https://www.akexorcist.com
JavaScript
1
star
64

WiFlyTCP

[Arduino] Simple TCP Communication Library for WiFly RN-XV
Arduino
1
star
65

Android-Assist

Assist action on Android
Java
1
star
66

MotoXLED

[Android] Enable a hidden LED on Moto X - Rooted required
Java
1
star
67

Android-AnimationSimple

Java
1
star
68

NextzyMVP

[Android] MVP Project Structure that I use in my company project
Java
1
star
69

CodeBattle-Android

[Android] "Code Battle" for android in Firebase Dev Day
1
star
70

IOIO-Bot

Java
1
star
71

Android-ScreenshotDialog

Screenshot app screen with Dialog layout
Java
1
star
72

Simple-MVP

[Android] Simple code for MVP pattern
Java
1
star
73

DevDeviceInfo

[Android] Developer Device Information
Java
1
star
74

Example-NestedViewPager

Java
1
star
75

Example-CloudVision

Using Cloud Vision API in Android
Java
1
star
76

RecyclerView-ItemTouchHelper

[Android] ItemTouchHelper in RecyclerView
Kotlin
1
star
77

SimpleCustomView-Basic

[Android] Custom view with all required code
Java
1
star
78

Android-CustomListView

Java
1
star
79

FlexyStepIndicator

[Android] Step Indicator view with flexibility customization
Java
1
star
80

IOIO-LCDController

Java
1
star
81

SlidingPaneLayout-Sample

[Android] Example of AndroidX SlidingPaneLayout implementation
Kotlin
1
star
82

EvenOdd

[Android] Example android app for Android CI with GitHub Actions and Firebase Test Lab
Kotlin
1
star
83

RecyclerVIew-With-Loading-Task

The answer for some guy's question on android dev facebook group at https://goo.gl/kLNlnC
Java
1
star
84

CurveSeekBar

[React] Custom your own curve seek bar in React - https://blog.nextzy.me/custom-curve-seek-bar-in-react-e1f213e87f8a
JavaScript
1
star