1

I am in an Android project which uses Bluetooth sockets. The app is the client socket. But the app crashes when working. Can anyone help me in solving the issue.

here is the code

public class READ extends AppCompatActivity {
    private static String btAdress = "00:10:60:D1:95:CD";
    BluetoothSocket mmSocket;
    BluetoothDevice mmDevice;
    BluetoothAdapter btAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
    }
    public void ConnectThread(View v){
        BluetoothDevice device;
        device = btAdapter.getRemoteDevice(btAdress);
        ConnectT(device);
    }
    public void ConnectT(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        mmDevice = device;
        BluetoothSocket tmp = null;
        String uuid = "a60f35f0-b93a-11de-8a39-08002009c666";
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
        } catch (IOException e) { }
        mmSocket = tmp;
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException nullException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
        }
    }
}

Thanks in Advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Post the stacktrace from the logcat. There you will see the exception that causes the crash. – greenapps Aug 20 '18 at 11:31
  • `catch (IOException e) { }` Oh oh you are doing nothing! You should at least have `e.printStackTrace()` there. And a log statements that prints `e.getMessage()`. And further you should take action if you catch an exception. You now continue with your code as if nothing has happened. Better return. Adapt all your catch blocks. – greenapps Aug 20 '18 at 11:32
  • `public void ConnectThread(View v)` ?? A public function in an Activity? Oh oh Where and how are you calling it? Or is it assigned to a button in xml? – greenapps Aug 20 '18 at 11:46

0 Answers0