Android get Bluetooth connected device

Source: Internet
Author: User

If Bluetooth is paired and connected manually, get the connected device:

1. Check the connection status:

Java code
    1. int A2DP = Bluetoothadapter.getprofileconnectionstate (BLUETOOTHPROFILE.A2DP);
    2. int headset = bluetoothadapter.getprofileconnectionstate (Bluetoothprofile.headset);
    3. int health = bluetoothadapter.getprofileconnectionstate (bluetoothprofile.health);

2, depending on whether there is a connection to obtain the connected device:

Java code
  1. int flag =-1;
  2. if (A2DP = = bluetoothprofile.state_connected) {
  3. flag = A2DP;
  4. } Else if (headset = = bluetoothprofile.state_connected) {
  5. flag = headset;
  6. } Else if (health = = bluetoothprofile.state_connected) {
  7. Flag = health;
  8. }
  9. if (flag! =-1) {
  10. Bluetoothadapter.getprofileproxy (mainactivity. This, new Servicelistener () {
  11. @Override
  12. public void onservicedisconnected (int. profile) {
  13. //TODO auto-generated method stub
  14. }
  15. @Override
  16. public void onserviceconnected (int. profile, Bluetoothprofile proxy) {
  17. //TODO auto-generated method stub
  18. list<bluetoothdevice> mdevices = Proxy.getconnecteddevices ();
  19. if (mdevices! = null && mdevices.size () > 0) {
  20. For (Bluetoothdevice device:mdevices) {
  21. LOG.I ("W", "Device name:" + device.getname ());
  22. }
  23. } Else {
  24. LOG.I ("W", "mdevices is null");
  25. }
  26. }
  27. }, flag);
  28. }

Android get Bluetooth connected device

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.