Android Studio, проблема конфигурации Bluetooth
Так что я новичок в Android до сих пор, но я работал над приложением, которое в основном я хочу, чтобы оно связывалось с Arduino через Bluetooth, но суть в том, что когда я пытаюсь настроить код Bluetooth, я получаю сообщение об ошибке в logcat, который:
java.lang.NullPointerException: попытка вызвать виртуальный метод 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream ()' для ссылки на нулевой объект "
и это действительно доставляло мне проблемы в течение недели, и я не могу найти решение, я уверен, что это не так уж сложно для опытного программиста, поэтому я очень признателен, если кто-то помог мне исправить это.
Вот мой код:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
TextView txtArduino, txtString, txtStringLength, sensorView0;
Handler bluetoothIn;
final int handlerState = 0; //used to identify handler message
private BluetoothAdapter btAdapter=null;
private BluetoothSocket btSocket = null;
private StringBuilder recDataString = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service - this should work for most devices
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// String for MAC address
private static String address = null;
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
public void onResume() {
super.onResume();
//Get MAC address from device_list via intent
Intent intent = getIntent();
//Get the MAC address from the DeviceListActivty via EXTRA
address = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
//create device and set the MAC address
//Log.i("ramiro", " FE:85:EE:7F:E7:6B : " + address);
if(address != null) {
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "La creacción del Socket fallo", Toast.LENGTH_LONG).show();
}
// Establish the Bluetooth socket connection.
try
{
btSocket.connect();
} catch (IOException e) {
try
{
btSocket.close();
} catch (IOException e2)
{
//insert code to deal with this
}
}}
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
//I send a character when resuming.beginning transmission to check device is connected
//If it is not an exception will be thrown in the write method and finish() will be called
mConnectedThread.write("x");
}
и вот где проблема должна быть:
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn=null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
частный BluetoothSocket createBluetoothSocket (устройство BluetoothDevice) выдает IOException {
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
}
https://i.stack.imgur.com/X4dY4.png вот мой logcat после добавления e.printstacktrace ()