How to request Android user to enable Bluetooth through a click?

前端 未结 2 1469
北海茫月
北海茫月 2021-02-14 11:12

from http://developer.android.com/guide/topics/connectivity/bluetooth.html i know that i need to the following to request the user to enable his BT:

if (!mBlueto         


        
相关标签:
2条回答
  • 2021-02-14 11:24

    Did you set proper permissions in your AndroidManifest.xml file? For sure, you will need BLUETOOTH permission.

    <manifest ... >
      <uses-permission android:name="android.permission.BLUETOOTH" />
      ...
    </manifest>
    

    In addition, as documentation says:

    If you want your app to initiate device discovery or manipulate Bluetooth settings, you must also declare the BLUETOOTH_ADMIN permission.

    If you want to enable one of these features you will need the following code:

    <manifest ... >
      <uses-permission android:name="android.permission.BLUETOOTH" />
      <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
      ...
    </manifest>
    
    0 讨论(0)
  • 2021-02-14 11:26

    Before you call the following method

    !mBluetoothAdapter.isEnabled()
    

    on the adapter, you have to make sure mBluetoothAdapter is not null. In your case it must be null and crashing. And if mBluetoothAdapter is null, the android documentation says that the device doesn't support Bluetooth.

    0 讨论(0)
提交回复
热议问题