How to prevent Android bluetooth RFCOMM connection from dying immediately after .connect()?
Try to change your code for creating RfcommSocket:
sock = zee.createRfcommSocketToServiceRecord(
UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));
for this code:
Method m = zee.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, 1);
Also try to change argument value in range 1-3 in this m.invoke(device, 1);
When connection will be Connected, but aborted when you try reading, call in some loop your method test() again.
As simple:
for(int i=0;i<3;i++){ if(!testDone) test(); }
Bluetooth RFCOMM connection couldn't be established consecutively on Android 4.2
I have finally figured out the solution.
It turns out that it's not a bug in the new Bluetooth driver. It's a corner case not considered in Bluetooth sample application, more specifically, BluetoothChat project, provided by Android SDK.
In the Bluetooth service code inside BluetoothChat sample project, when connection is failed or lost, it always starts the service over to restart listening mode. It's no problem when the phone is used as server, however, in some scenarios when the phone is used as client, once it goes into listening mode, you can't connect to other servers. Because in "connect" function, it doesn't cancel (In)SecureAcceptThread. So essentially you are listening to other connections as server while trying to connect to other servers as client. It's conflicting.
The way to solve this issue is, if you are sure your phone will not be used as server listening to incoming connection, then simply remove the code to restart listening mode once connection is failed or stopped.
Android Bluetooth SPP connection to seems to go dead after a couple of seconds
The Tab and (in my experience with 6 different devices) all Samsung devices have TERRIBLE Bluetooth SPP implementations. I'd suggest choosing a different device.
Also, I would check that you are calling flush
on the OutputStream, particularly if you are not sending line separators.
Related Topics
How to Get the APK of an Installed App Without Root Access
Listview Scroll to the End of the List After Updating the List
Why Does the Google Play Store Say My Android App Is Incompatible with My Own Device
About "_Id" Field in Android SQLite
How to Compile the Android Aosp Kernel and Test It with the Android Emulator
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ
Adjustpan Not Preventing Keyboard from Covering Edittext
How to Get the List of Running Applications
Android Studio Says "Cannot Resolve Symbol" But Project Compiles
Difference Between Oncreateview and Onviewcreated in Fragment
Android: How to Make a Clickable Map Image with Each Country Producing a Different Action
Android Startcamera Gives Me Null Intent and ... Does It Destroy My Global Variable
How to Find Android Source Code Online
"Native Typeface Cannot Be Made" Only for Some People
Searchview in Listview Having a Custom Adapter
Static Variable Null When Returning to the App