-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetooth.java
More file actions
91 lines (76 loc) · 2.58 KB
/
Bluetooth.java
File metadata and controls
91 lines (76 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.example.atilla.peixealimentador;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
/**
* Created by Atilla on 01-Aug-17.
*/
public class Bluetooth{
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice mmDevice;
private BluetoothSocket mmSocket;
private OutputStream outStream;
private InputStream inputStream;
private final int REQUEST_ENABLE_BT = 1;
/**
*/
public Bluetooth(){
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
/**
* Verifica se o dispositivo possui bluetooth e também se ele está ativado, caso não esteja
* ativado ele pede permissão ao usuário para ativar
*/
public int checkBlueToothState() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
return 1;
}
if (!bluetoothAdapter.isEnabled())
return 2;
else
return 0;
}
/**
* Encontra os dispositivos PAREADOS com o seu dispositivo que possue o nome igual ao passado
* no construtor
*/
public void pairDevice(String deviceName) throws IOException {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(deviceName)) {
mmDevice = device;
break;
}
}
}
openBT();
}
public void writeData(String data) {
try {
outStream = mmSocket.getOutputStream();
} catch (IOException e) {
Log.d("", "Bug BEFORE Sending stuff", e);
}
String message = data;
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.d("", "Bug while sending stuff", e);
}
}
private void openBT() throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard //SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
Log.d("Status Conexao", "Bluetooth Conectado!!");
}
}