Nick van K
Nick van K

Reputation: 15

Android restart bluetooth socket after crash?

I'm working on my first app for a school project. It receives and proccesses data received over bluetooth (PC with terminal and bluetooth module).
Everything works as it should but if something happens that wasn't expected, the app could crash. this is not a big deal but the Bluetooth connection remains connected although my program doesn't know that and can't work. **Is there a function to request the active bluetooth connection (like restarting a socket with the adress or something?)
I cant connect it the default way because the bluetooth discvery cant find the device anymore.
If i restart the phone everything works fine again. I'm using the default bluetooth threads from the Android develeoper website. If anyone needs to see the code I can put it here.

Upvotes: 2

Views: 1201

Answers (1)

Pim
Pim

Reputation: 433

When you are working with BluetoothSockets you can use yourBluetoothSocket.getRemoteDevice(); This wil get the BluetoothDevice this socket is connected to.

If there is no device connected, you can hardcode the MAC-adres of the device you want to connect to.

BluetoothDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MAC-ADDRESS);

after this you can try to reconnect.

Upvotes: 2

Related Questions