0

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: enter image description here

The code is : here

1 Answers1

0

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.

Community
  • 1
  • 1
Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • While they are connected for a while I can't do anything. My app freezes... – Χρήστος Γεωργακίδης Aug 03 '16 at 10:02
  • @ΧρήστοςΓεωργακίδης Your app freezes because of doing too much work on its main thread. I update the answer and hope it is helpful for you. – Rita Han Aug 04 '16 at 01:37
  • I didn't noticed that problem at first. I solve it by writing again the fragment's code from the begin. But still my main problem remains... It doesn't connect. When I run it with debugger adding some breakpoints I noticed that my program doesn't run my code insert my onItemClickListener. It steps over. Any idea? – Χρήστος Γεωργακίδης Aug 04 '16 at 22:46