简体   繁体   中英

Android Bluetooth Connection between two devices programmatically Fails

I am trying to connect to devices via bluetooth. When I hit the search button everything is good. Also when I click the device from the Detected Devices list also they get paired correctly. But when I click the desired device from the Paired Devices list to connect, firstly the are connected but after ~5 seconds they disconnect and stay paired.Also the socket closes...This is what I get: 在此处输入图片说明

The code is : here

firstly the are connected but after ~5 seconds they disconnect and stay paired.

If your device is Bluetooth low energy device, well, it is what Bluetooth LE designed to be. Unlike classical Bluetooth, Bluetooth LE keep connection for very short period of time. After data exchange complete, current connection end immediately. Connection will be established again when there is data to exchange next time.

If the problem isn't caused by Bluetooth designing then it should be caused by your code itself. In your post we can see:

The application may be doing too much work on its main thread.

What this means is that your code is taking long to process and frames are being skipped because of it, It maybe because of some heavy processing that you are doing at the heart of your application or DB access or any other thing which causes the thread to stop for a while.

How to fix it?

The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. A comprehensive explanation is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM