Page 41 - MDP2020-2
P. 41
················································································ 명장양성프로젝트 【MDP】 과제발표회 자료집 Ⅱ | 35
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;
public class MainActivity extends AppCompatActivity {
private BluetoothSPP bt;
// private ConnectedBluetoothThread mThreadConnectedBluetooth;
Button btnEat;
@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);
@Override
public void onDataReceived(byte[] data, String message) {
//Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
String[] array = message.split(",");
temp.setText(array[0].concat("C"));
}
});
/* bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() { //데이터 수신
public void onDataReceived(byte[] data, String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}); */
bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() { //연
결됐을 때