I have a project where i have to make an app (terminal) which he have to read measured values from ESP32 using bluetooth connection. I managed to built terminal and added buttons with some functions. The problem is when i click "List devices" botton, it lists all available bluetooth devices and when i click the ESP32 device to connect it crashes. I have all permissions in manifest but still have this problem.
package com.example.plavi_zub_2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
Button listen, send, listDevices;
ListView listView;
TextView msg_box, status;
EditText writeMsg;
BluetoothAdapter bluetoothAdapter;
BluetoothDevice[] btArray;
SendReceive sendReceive;
static final int STATE_LISTENING = 1;
static final int STATE_CONNECTING = 2;
static final int STATE_CONNECTED = 3;
static final int STATE_CONNECTION_FAILED = 4;
static final int STATE_MESSAGE_RECEIVED = 5;
int REQUEST_ENABLE_BLUETOOTH = 1;
private static final String APP_NAME = "BTChat";
private static final UUID MY_UUID = UUID.fromString("8ce255c0-223a-11e0-ac64-0803450c9a66");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewByIdes();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
return;
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.BLUETOOTH_CONNECT}, 44);
}
}
implementListeners();
}
private void implementListeners() {
listDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
Set<BluetoothDevice> bt = bluetoothAdapter.getBondedDevices();
String[] strings = new String[bt.size()];
btArray = new BluetoothDevice[bt.size()];
int index = 0;
if (bt.size() > 0) {
for (BluetoothDevice device : bt) {
btArray[index] = device;
strings[index] = device.getName();
index++;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
}
return;
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 44);
}
}
});
listen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ServerClass serverClass = new ServerClass();
serverClass.start();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ClientClass clientClass = new ClientClass(btArray[i]);
clientClass.start();
status.setText("Connecting");
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String string = String.valueOf(writeMsg.getText());
sendReceive.write(string.getBytes());
}
});
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case STATE_LISTENING:
status.setText("Listening");
break;
case STATE_CONNECTING:
status.setText("Connecting");
break;
case STATE_CONNECTED:
status.setText("Connected");
break;
case STATE_CONNECTION_FAILED:
status.setText("Connection Failed");
break;
case STATE_MESSAGE_RECEIVED:
byte[] readBuff = (byte[]) msg.obj;
String tempMsg = new String(readBuff, 0, msg.arg1);
msg_box.setText(tempMsg);
break;
}
return true;
}
});
private void findViewByIdes() {
listen = (Button) findViewById(R.id.listen);
send = (Button) findViewById(R.id.send);
listView = (ListView) findViewById(R.id.listview);
msg_box = (TextView) findViewById(R.id.msg);
status = (TextView) findViewById(R.id.status);
writeMsg = (EditText) findViewById(R.id.writemsg);
listDevices = (Button) findViewById(R.id.listDevices);
}
private class ServerClass extends Thread {
private BluetoothServerSocket serverSocket;
public ServerClass() {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(APP_NAME, MY_UUID);
return;
} else
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 44);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
BluetoothSocket socket = null;
while (socket == null) {
try {
Message message = Message.obtain();
message.what = STATE_CONNECTING;
handler.sendMessage(message);
socket = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_CONNECTION_FAILED;
handler.sendMessage(message);
}
if (socket != null) {
Message message = Message.obtain();
message.what = STATE_CONNECTED;
handler.sendMessage(message);
sendReceive = new SendReceive(socket);
sendReceive.start();
break;
}
}
}
}
private class ClientClass extends Thread {
private BluetoothDevice device;
private BluetoothSocket socket;
public ClientClass(BluetoothDevice device1) {
device = device1;
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
return;
} else
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 44);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
socket.connect();
Message message=Message.obtain();
message.what=STATE_CONNECTED;
handler.sendMessage(message);
sendReceive=new SendReceive(socket);
sendReceive.start();
return;
}else
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 44);
sendReceive=new SendReceive(socket);
sendReceive.start();
} catch (IOException e) {
e.printStackTrace();
Message message=Message.obtain();
message.what=STATE_CONNECTION_FAILED;
handler.sendMessage(message);
}
}
}
private class SendReceive extends Thread
{
private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;
public SendReceive (BluetoothSocket socket)
{
bluetoothSocket=socket;
InputStream tempIn=null;
OutputStream tempOut=null;
try {
tempIn=bluetoothSocket.getInputStream();
tempOut=bluetoothSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
inputStream=tempIn;
outputStream=tempOut;
}
public void run()
{
byte[] buffer=new byte[1024];
int bytes;
while (true)
{
try {
bytes=inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVED,bytes,-1,buffer).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write(byte[] bytes)
{
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And I have few errors that says:
Unable to open libpenguin.so: dlopen failed: library "libpenguin.so" not found.
FATAL EXCEPTION: Thread-3
Process: com.example.plavi_zub_2, PID: 18858
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream android.bluetooth.BluetoothSocket.getInputStream()' on a null object reference
at com.example.plavi_zub_2.MainActivity$SendReceive.<init>(MainActivity.java:331)
at com.example.plavi_zub_2.MainActivity$ClientClass.run(MainActivity.java:307)
What I have tried:
I've tried to find solution on offical android dev site but didn't solve any problem.