- API +16
- Suporte ao Android 6
Adicione ao seu projeto:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
} dependencies {
compile 'com.github.tiagohm:BlueDroid:VERSION'
}Adicione as permissões:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />Declare BlueDroid:
BlueDroid bt = new BlueDroid(Context, ConnectionDevice, ConnectionSecure);ConnectionDevice.ANDROIDConnectionDevice.OTHERVerificar se Bluetooth está disponível:
if(!bt.isAvailable()) {
finish();
...
}Verificar se está ativado e ativá-lo se necessário:
@Override
public void onStart() {
super.onStart();
if(!bt.isEnabled()) {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i, REQUEST_ENABLE_BT);
} else {
...
}
}Encerrá-lo ao fechar o aplicativo:
@Override
protected void onDestroy() {
super.onDestroy();
bt.stop();
}Procurar por dispositivos:
bt.doDiscovery(Activity);Adicionar o tratamento da requisão da permissão:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
bt.checkDiscoveryPermissionRequest(requestCode, permissions, grantResults);
}Exibir os dispositivos encontrados numa lista e conectar ao clicar em um item:
new BlueDiscoveryDialog(Context, bt).show();Para desconectar:
bt.disconnect();Para enviar:
String texto = "123456";
bt.send(texto.getBytes(Charset.forName("US-ASCII")), LineBreakType.UNIX);Receber dados:
bt.addDataReceivedListener(new BlueDroid.DataReceivedListener() {
@Override
public void onDataReceived(byte data) {
...
}
} );Outros eventos:
bt.addDiscoveryListener(new BlueDroid.DiscoveryListener()
{
@Override
public void onDiscoveryStarted()
{
}
@Override
public void onDiscoveryFinished()
{
}
@Override
public void onNoDevicesFound()
{
}
@Override
public void onDeviceFound(Device device)
{
}
@Override
public void onDiscoveryFailed()
{
}
});
bt.addConnectionListener(new BlueDroid.ConnectionListener()
{
@Override
public void onDeviceConnecting()
{
}
@Override
public void onDeviceConnected()
{
}
@Override
public void onDeviceDisconnected()
{
}
@Override
public void onDeviceConnectionFailed()
{
}
});string.xml
<string name="dialog_bluetooth_discovery_title">Dispositivos</string>
<string name="dialog_bluetooth_discovery_scan">Escanear</string>Copyright 2016-2017 tiagohm
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.


