Reputation: 533
I have developed an android application on Samsung S6 mobile (Android Version:- Lollipop). The application reads data on Bluetooth port and displays on screen.
However, the Bluetooth connection is lost after sometime. After that, the Bluetooth gets connected-disconnected continuously.
The connection log is:-
03-05 15:34:57.071: E/bt-btm(6152): Reset sec_bd_name and name flag. (BR/EDR link)
03-05 15:34:57.071: E/bt-btm(6152): btm_sec_disconnected - Clearing Pending flag
invalid rfc slot id: 91
03-05 16:37:42.490: W/bt-btif(6113): dm_pm_timer expires
03-05 16:37:42.490: W/bt-btif(6113): dm_pm_timer expires 0
03-05 16:37:42.490: W/bt-btif(6113): proc dm_pm_timer expires
When i restart the mobile, everything work fine. But problem reappears again after sometime.
Why does this happen ?? How do i maintain the connection without getting disconnected ??
Edit:- I have tested my application on multiple versions of android devices and it works fine on all other devices except Samsung S6.
Upvotes: 1
Views: 451
Reputation: 24211
Bluetooth will be disconnected automatically after a fixed period of idle time. You've nothing to do with it if you're using a regular bluetooth peripheral.
To maintain a continuous connection with bluetooth you need to check for idle time in your application and send a garbage data over your OutputStream
after a fixed period of time. This is not a very good approach though, but works fine to maintain a continuous connection with bluetooth.
You can go for another approach which might freeze your application sometimes, which is creating a rfcommSocket communication each time when the bluetooth gets disconnected. You'll get a disconnection intent when your BluetoothAdapter
is registered to listen bluetooth communication status. In this approach you need to know the MAC address of your bluetooth device. Here's an example how you would create the rfcommSocket communication:
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
In your BroadcastReceiver
for Bluetooth intent filters:
BluetoothDevice mDevice;
BluetoothSocket bs;
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getAddress().equals(YOUR_BT_MAC_ADDRESS)) {
mDevice = device;
bs = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
mBluetoothAdapter.cancelDiscovery();
}
}
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
try {
bs = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {}
}
If this is anything about memory concern, check your InputStream
and OutputStream
. Read from InputStream
until its not cleared.
Upvotes: 1