SimpleBluetoothLibrary
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
````java
repositories {
....
maven {url "https://jitpack.io"}
}
dependencies {
....
compile 'com.github.DeveloperPaul123:SimpleBluetoothLibrary:1.4'
}
````
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);
}
@Override
public void onBluetoothEnabled() {
super.onBluetoothEnabled();
}
@Override
public void onDeviceSelected(String macAddress) {
super.onDeviceSelected(macAddress);
}
@Override
public void onBluetoothDataReceived(byte[] bytes, String data) {
}
@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;
private String curMacAddress;
@Override
protected void onResume() {
super.onResume();
simpleBluetooth = new SimpleBluetooth(this, this);
simpleBluetooth.initializeSimpleBluetooth();
simpleBluetooth.setSimpleBluetoothListener(new SimpleBluetoothListener() {
@Override
public void onBluetoothDataReceived(byte[] bytes, String data) {
}
@Override
public void onDeviceConnected(BluetoothDevice device) {
}
@Override
public void onDeviceDisconnected(BluetoothDevice device) {
}
@Override
public void onDiscoveryStarted() {
}
@Override
public void onDiscoveryFinished() {
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
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:
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 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.