Skip to content

Repository files navigation

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-permissionandroid:name="android.permission.BLUETOOTH" />
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />

2. Define SmoothBluetooth instance

privateSmoothBluetoothmSmoothBluetooth;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
mSmoothBluetooth = newSmoothBluetooth(Contextcontext);
}

there are possible overrides:

mSmoothBluetooth = newSmoothBluetooth(Contextcontext, SmoothBluetooth.Listenerlistener);

or

mSmoothBluetooth = newSmoothBluetooth(Contextcontext, ConnectionToconnectionTo, Connectionconnection, SmoothBluetooth.Listenerlistener);

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

ConnectionTo.ANDROID_DEVICEConnectionTo.OTHER_DEVICE

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

Connection.SECUREConnection.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)

privateSmoothBluetooth.ListenermListener = newSmoothBluetooth.Listener() {
@OverridepublicvoidonBluetoothNotSupported() {
//device does not support bluetooth
}
@OverridepublicvoidonBluetoothNotEnabled() {
//bluetooth is disabled, probably call Intent request to enable bluetooth
}
@OverridepublicvoidonConnecting(Devicedevice) {
//called when connecting to particular device
}
@OverridepublicvoidonConnected(Devicedevice) {
//called when connected to particular device
}
@OverridepublicvoidonDisconnected() {
//called when disconnected from device
}
@OverridepublicvoidonConnectionFailed(Devicedevice) {
//called when connection failed to particular device
}
@OverridepublicvoidonDiscoveryStarted() {
//called when discovery is started
}
@OverridepublicvoidonDiscoveryFinished() {
//called when discovery is finished
}
@OverridepublicvoidonNoDevicesFound() {
//called when no devices found
}
@OverridepublicvoidonDevicesFound(finalList<Device> deviceList,
finalBluetoothHelper.ConnectionCallbackconnectionCallback) {
//receives discovered devices list and connection callback//you can filter devices list and connect to specific one//connectionCallback.connectTo(deviceList.get(position));
}
@OverridepublicvoidonDataReceived(intdata) {
//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, booleanCRLF);

or

mSmoothBluetooth.send(Stringdata, booleanCRLF);

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(Stringdata);

6. Disconnect

mSmoothBluetooth.disconnect();

7. Do not forget to stop

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

@OverrideprotectedvoidonDestroy() {
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.

About

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

Resources

Stars

195 stars

Watchers

17 watching

Forks

Releases

Packages

Contributors

Languages