开发者

Enable Disable Bluetooth in Android

I want to enable and disable bluetooth in android programmatically..Please help me how to do this..Tha开发者_如何学运维nks in advance.


I know this has been answered but just in case someone else wants this info. There are two ways to enable bluetooth one is to use the Intents and send a user request

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableIntent, someIntegerValue);

second is to just call the enable method on the adapter, only use this method if user input is not needed or wanted.

BluetoothAdapter blue = BluetoothAdapter.getDefaultAdapter();
if (!blue.isEnabled())
    blue.enable();

to disable you just call disable method on adapter.


You might want to read the Android documentation to find the answer yourself: http://developer.android.com/guide/topics/wireless/bluetooth.html


You can directly open Bluetooth by calling function turnOn() on click of button(on):

void turnOn()
{
    if (bluetoothAdapter == null)
    {
        status.setText("BlueTooth adapter not found");
    }
    else if (bluetoothAdapter.isEnabled())
    {
        Toast.makeText(MainActivity.this, "Bluetooth is already on.", Toast.LENGTH_SHORT).show();
    }
    else
        bluetoothAdapter.enable();
}


for Bluetooth enable

Intent blintent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivity(blintent);

for Bluetooth disable BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.disable();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜