Я использовал библиотеку BluetoothSPP:
https://github.com/akexorcist/Android-BluetoothSPPLibrary
Arduino успешно подключился к приложению android studio, но я не могу получить данные, я импортирую оба моих кода и, пожалуйста, кто-нибудь скажет мне, если я сделал что-то не так, из того, что я прочитал и понял, bluetooth. setOnDataReceivedListener - это функция для получения, и вы должны поместить bluetooth.print (data) в часть arduino, чтобы
Android:
открытый класс MainActivity расширяет AppCompatActivity {
final String ON = "1";
final String OFF = "0";
BluetoothSocket socket;
private BluetoothAdapter mAdapter;
BluetoothSPP bluetooth;
private BluetoothDevice remoteDevice;
TextView text;
private BluetoothServerSocket mmServerSocket;
String TAG = "TEST: ";
Button connect;
Button on;
Button off;
private OnDataReceivedListener mDataReceivedListener = null;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK)
bluetooth.connect(data);
} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bluetooth.setupService();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
public void setOnDataReceivedListener (OnDataReceivedListener listener) {
if (mDataReceivedListener == null)
mDataReceivedListener = listener;
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
}
public void onStart() {
super.onStart();
if (!bluetooth.isBluetoothEnabled()) {
bluetooth.enable();
} else {
if (!bluetooth.isServiceAvailable()) {
bluetooth.setupService();
bluetooth.startService(BluetoothState.DEVICE_OTHER);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetooth = new BluetoothSPP(this);
text = (TextView) findViewById(R.id.textView2);
connect = (Button) findViewById(R.id.connect);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
BluetoothSocket socket = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetooth.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext(), "Bluetooth is not available", Toast.LENGTH_SHORT).show();
finish();
}
bluetooth.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Log.d(TAG, "Start Reading");
/*Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();*/
Log.i("Check", "Message : " + message);
}
});
bluetooth.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
connect.setText("Connected to " + name);
}
public void onDeviceDisconnected() {
connect.setText("Connection lost");
}
public void onDeviceConnectionFailed() {
connect.setText("Unable to connect");
}
});
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetooth.getServiceState() == BluetoothState.STATE_CONNECTED) {
bluetooth.disconnect();
} else {
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Its on");
bluetooth.send(ON, true);
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Its off");
bluetooth.send(OFF, true);
}
});
}
Arduino часть: