Android: Set Bluetooth Discoverability Unbounded

前端 未结 6 1636
悲&欢浪女
悲&欢浪女 2021-01-06 23:57

I have spent the last couple of days trying to make an app that keeps my Samsung Galaxy S3 mini (Android 2.1.4) discoverable for an \"infinite\" amount of time. My code look

相关标签:
6条回答
  • 2021-01-07 00:04

    I've arrived to the conclusion that it can't be done, unless the user goes to Bluetooth > Settings > Visibility timeout and sets the timeout accordingly.

    Peace.

    0 讨论(0)
  • 2021-01-07 00:12

    Im not sure if this would suit your application or if theres some other reason why you don't want to do this but why not just discover for the default time then restart discovery when it times out? this way it will technically be an unlimited discovery, which will only trigger a VERY slight pause in between, I used this method in my application using a broadcast receiver.

    I put startDiscovery() in the onStart() method to trigger discovery on activity start, then listen to ACTION_DISCOVERY_FINISHED in your broadcast receiver's onReceive() method, here place another call to startDiscovery().

    this will loop the discovery forever, if you want to stop when you find a device then call cancelDiscovery() in you receiver's ACTION_FOUND listener, you can also place some checking here if you need to find a particular device - again in my case i was checking for a particular device's mac address so the discovery would continue until this was found.

    not sure if this is any use but if you need more detail let me know.

    0 讨论(0)
  • 2021-01-07 00:14

    above api level 14 EXTRA_DISCOVERABLE_DURATION 0 works with infinite limit but below this it works for max 1 hour.

    This code works for me

    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
    startActivityForResult(intent, Utils.REQUEST_DEVICE_DISCOVERABLE);
    
    0 讨论(0)
  • 2021-01-07 00:14

    According to Android documentation, the maximum time for being discoverable is capped at 300seconds. You cannot make the BT discoverable forever.

    Please see: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_REQUEST_DISCOVERABLE

    In order to get the 300second maximum period, you need to change one line of code as:

    MDisc.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300)
    
    0 讨论(0)
  • 2021-01-07 00:20

    Works to me.

    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    enableBtIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
    startActivityForResult(enableBtIntent, Utils.REQUEST_DEVICE_DISCOVERABLE);
    

    Disable manually your Bluetooth and then run the code. It will works, yes.

    0 讨论(0)
  • 2021-01-07 00:29

    I come to the same conclusion on three devices I have.

    • ANDROID v 4.3 and higher : EXTRA_DISCOVERABLE_DURATION 0 works no limit
    • ANDROIND v 4.1 : EXTRA_DISCOVERABLE_DURATION 0 is max 1 hour. Have to change manually to no limit in parameters.
    0 讨论(0)
提交回复
热议问题