package com.example.btmonitor;
import android.bluetooth.BluetoothSocket;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import androidx.core.app.ComponentActivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ReceiveThread extends Thread{
private BluetoothSocket socket;
private InputStream inputS;
private OutputStream outputS;
public static String message;
private TextView massCount;
private TextView volumeCount;
private byte[] rBuffer;
public ReceiveThread(BluetoothSocket socket){
this.socket = socket;
try{
inputS = socket.getInputStream();
} catch (IOException e){
}
try{
outputS = socket.getOutputStream();
} catch (IOException e){
}
}
@Override
public void run() {
rBuffer = new byte[2];
massCount = MainActivity.massCount;
while(true){
try{
int size = inputS.read(rBuffer);
message = new String(rBuffer, 0, size);
//Log.d("MyLog","Message: " + message);
//massCount.setText(message+"г");
Thread.sleep(2000);
} catch (IOException e){
break;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void sendMessage(byte[] byteArray){
try{
outputS.write(byteArray);
}catch (IOException e){
}
}
}
Это поток
package com.example.btmonitor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private MenuItem menuItem;
private BluetoothAdapter btAdapter;
private final int ENABLE_REQUEST = 15;
private SharedPreferences pref;
private BtConnection btConnection;
private Button bA, bB;
public Button startButton;
public static TextView massCount;
public static TextView volumeCount;
public EditText densityText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bA = findViewById(R.id.buttonA);
bB = findViewById(R.id.buttonB);
densityText = findViewById(R.id.densityText);
massCount = findViewById(R.id.masscount);
volumeCount = findViewById(R.id.volumecount);
init();
bA.setOnClickListener(v -> {
btConnection.sendMessage("A");
});
bB.setOnClickListener(v -> {
btConnection.sendMessage("B");
});
startButton = findViewById(R.id.StartButton);
startButton.setOnClickListener( view -> {
btConnection.sendMessage("Start");
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menuItem = menu.findItem(R.id.id_bt_button);
setBtIcon();
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.id_bt_button){
if(!btAdapter.isEnabled()){
enableBt();
} else {
btAdapter.disable();
menuItem.setIcon(R.drawable.ic_bt_enable);
}
} else if(item.getItemId() == R.id.id_menu){
if(btAdapter.isEnabled()){
Intent i = new Intent(MainActivity.this, BtListActivity.class);
startActivity(i);
} else {
Toast.makeText(this, "Включите блютуз для перехода на этот экран!", Toast.LENGTH_SHORT).show();
}
} else if(item.getItemId() == R.id.id_connect){
btConnection.connect();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ENABLE_REQUEST){
if(resultCode == RESULT_OK){
setBtIcon();
}
}
}
private void setBtIcon(){
if(btAdapter.isEnabled()){
menuItem.setIcon(R.drawable.ic_bt_disable);
} else {
menuItem.setIcon(R.drawable.ic_bt_enable);
}
}
private void init(){
btAdapter = BluetoothAdapter.getDefaultAdapter();
pref = getSharedPreferences(BtConst.MY_PREF, Context.MODE_PRIVATE);
btConnection = new BtConnection(this);
}
private void enableBt(){
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i, ENABLE_REQUEST);
}
}