2012-04-24 4 views
1

可能性の重複:
How to enable/disable bluetooth programmatically in androidアンドロイドでBluetoothを無効にするにはどうすればいいですか?

私は、Android開発の初心者です。私は自分のアプリでBluetoothを無効にすることはできません。ここではチェックボックスを使用しています。有効にするとブルートゥースが有効になりますが、無効にしている間は有効になります..何をしますか?

マイコード:

enable_chkbox=(CheckBox)findViewById(R.id.chkboxenable); 
enable_chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // TODO Auto-generated method stub 
     if(buttonView.isChecked()) 
     { 
      if (!mBluetoothAdapter.isEnabled()) { 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      } 
      else if(!buttonView.isChecked())//updated 
      { 
       mBluetoothAdapter.disable(); 
      //finish(); 
      } 
     } 
    } 
}); 

Androidのマニフェストファイルのパーミッション:あなたの他には見当違いであるよう

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
+0

[可能性の重複](http://stackoverflow.com/q/3806536/940096) – Praveenkumar

答えて

3

あなたelse ifコードは役に立ちません。 これを試してください。

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
    if(buttonView.isChecked()) 
    { 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 
    else 
    { 
      mBluetoothAdapter.disable(); 
      //finish(); 
    } 
+0

おかげでたくさんのDevが..あなたは私の問題を解決している..: –

+0

@DeepthiG:ああ、あなたは歓迎しているが。 。 – Bhavin

1

が見えます。それはする必要があります

if (buttonView.isChecked()) { 
    if (!mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
    } 
} 
else { 
    mBluetoothAdapter.disable(); 
    // finish(); 
} 

希望に役立ちます。コードの下

0

使用 -

enable_chkbox=(CheckBox)findViewById(R.id.chkboxenable); 
enable_chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    // TODO Auto-generated method stub 
    if(buttonView.isChecked()) 
    { 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
     if (!mBluetoothAdapter.isEnabled()) 
     { 
      // do something 
     }else 
     { 
      mBluetoothAdapter.disable(); 
     } 
     } 
    } 
});