๋ง์ ๋ถ๋ค์ด ์๋ฐ ์ ์ฒด ์์ค์ฝ๋๋ฅผ ์ํ์ ์ ์ด๋ ๊ฒ๋ผ๋ ์ฌ๋ ค๋ด ๋๋ค.
์ ๊ฐ ํ๋ก์ ํธ๋ฅผ ๋ค ์ญ์ ํด์ ์ ๊น ์ฝ๋๋ก ์ ์ด๋๊ณ ํ ์คํธ๋ ํ์ง ์์์ต๋๋ค.
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;
public class MainActivity extends AppCompatActivity {
private BluetoothSPP bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = new BluetoothSPP(this); //Initializing
if (!bt.isBluetoothAvailable()) { //๋ธ๋ฃจํฌ์ค ์ฌ์ฉ ๋ถ๊ฐ
Toast.makeText(getApplicationContext()
, "Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
// ------------------------------ ๋ฐ์ดํฐ ์์ ๋ถ ----------------------------- //
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() { //๋ฐ์ดํฐ ์์
TextView temp = findViewById(R.id.temp);
TextView humd = findViewById(R.id.humd);
public void onDataReceived(byte[] data, String message) {
String[] array = message.split(",");
temp.setText(array[0].concat("C"));
humd.setText(array[1].concat("%") );
}
});
// ------------------------------ ๋ฐ์ดํฐ ์์ ๋ถ ----------------------------- //
bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() { //์ฐ๊ฒฐ๋์ ๋
public void onDeviceConnected(String name, String address) {
Toast.makeText(getApplicationContext()
, "Connected to " + name + "\n" + address
, Toast.LENGTH_SHORT).show();
}
public void onDeviceDisconnected() { //์ฐ๊ฒฐํด์
Toast.makeText(getApplicationContext()
, "Connection lost", Toast.LENGTH_SHORT).show();
}
public void onDeviceConnectionFailed() { //์ฐ๊ฒฐ์คํจ
Toast.makeText(getApplicationContext()
, "Unable to connect", Toast.LENGTH_SHORT).show();
}
});
Button btnConnect = findViewById(R.id.btnConnect); //์ฐ๊ฒฐ์๋
btnConnect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();
} else {
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
bt.stopService(); //๋ธ๋ฃจํฌ์ค ์ค์ง
}
public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) { //
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER); //DEVICE_ANDROID๋ ์๋๋ก์ด๋ ๊ธฐ๊ธฐ ๋ผ๋ฆฌ
setup();
}
}
}
public void setup() {
Button btnSend = findViewById(R.id.btnSend); //๋ฐ์ดํฐ ์ ์ก
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bt.send("Text", true);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK)
bt.connect(data);
} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
728x90
๋ฐ์ํ