안드로이드 스튜디오 블루투스 연결 질문
조회수 1107회
안드로이드 스튜디오로 아두이노와 블루투스 연결을 하려고 코드를 짜고 있는데 mainactivity.java의 bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() { 부분에서 볼드체로 처리한 부분에 계속 빨간 줄이 그어지네요. build.grade의 dependeny에 implementation 'com.akexorcist:bluetoothspp:1.0.0' 추가했고 manifects에 이 두 줄 추가했습니다. 제가 놓친 게 있는지 확인해주시면 감사하겠습니다. 아래는 mainactivity.java 코드입니다.
package com.example.myapplication;
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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);
public void onDataReceived(byte[] data, String message) {
temp.setText("체온: " + message + "'C");
}
});
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(); //블루투스 중지
}
댓글 입력