Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino.
Add Gradle dependency:
dependencies {
implementation 'com.github.yashx:AndroidSmoothBluetooth:master-SNAPSHOT'
}
Make sure to add the snapshot repository:
repositories {
maven {url "https://jitpack.io"}
}
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
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
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
}
};
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
or
mSmoothBluetooth.autoconnect("MAC ID HERE");
mSmoothBluetooth.doDiscovery();
Call doDiscovery()
method which search for unpaired devices and returns them to SmoothBluetooth.Listener
's onDevicesFound
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);
mSmoothBluetooth.disconnect();
For instance in your activity where SmoothBluetooth
is defined you must call stop()
@Override
protected void onDestroy() {
super.onDestroy();
mSmoothBluetooth.stop();
}
You can clone the project and compile it yourself (it includes a sample). MainActivity
Want to contribute? You are welcome!
Note that all pull request should go to dev
branch.
- Mantas Palaima - palaima.mantas@gmail.com
Credit to Aidan Follestad's Material Dialogs library.
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.