Android Smooth Bluetooth


Source link: https://github.com/palaima/AndroidSmoothBluetooth

Android Smooth Bluetooth

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. 

Resources

A delivery service for Android Wear. Courier uses Wearable.DataApi and Wearable.MessageApi to deliver objects between devices simply and cleanly.

Unofficial ThingSpeak API library for Android.

Features:

Android SQLite database manager.

Truth is a testing framework designed to make your tests and their error messages more readable and discoverable, while being extensible to new types of objects.

Truth adopts a fluent style for test propositions, is extensible in several ways, supports IDE completion/discovery of available propositions, and supports different responses to un-true propositions. Truth can be used to declare JUnit-style assumptions (which skip the test on failure), assertions (interrupt the test on failure), and expectations (continue the test, but collect errors and report failure at the end).

Use gradle tasks to run specific adb commands. You can use this plugin to do things such as:

  • Find all devices attached and get basic info about them
  • Select the first one that complies with a custom rule
  • Install a specific APK from the available build types + flavours
  • Clear preferences or do something related to the APK to prepare for tests
  • Run monkey for that specific APK on that specific device
  • Uninstall the APK

Bash script that exports content provider's data to raw and CSV format. It can be your own content provider or any other provider that android decided not to block with permissions.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes