안녕하세요 버튼을 이용하여 블루투스를 앱상에서 on/off 시키려니 토글버튼이 제일 낳은것 같았는데요.
자꾸 어플이 죽네요...
한번만 봐주셨음 좋겠습니다....
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final BluetoothAdapter mBluetoothAdapter = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ImageButton bluetooth=(ImageButton) findViewById(R.id.bluetoothbtn);
//bluetooth.setOnClickListener((OnClickListener) this);
//if(btService ==null){
//btService=new Bluetooth(this,mHandler);
//}
//bluetooth.setSelected(true);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final boolean hasBluetooth = (mBluetoothAdapter == null);
final ToggleButton bluetooth = (ToggleButton) findViewById(R.id.bluetoothbtn);
final ToggleButton earphone=(ToggleButton) findViewById(R.id.earphonnbtn);
final ToggleButton phonemic=(ToggleButton) findViewById(R.id.phonemic);
bluetooth.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks
if (bluetooth.isChecked())
{
if (hasBluetooth && !mBluetoothAdapter.isEnabled())
{
// prompt the user to turn BlueTooth on
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
else
{
if (hasBluetooth && mBluetoothAdapter.isEnabled())
{
boolean isDisabling = mBluetoothAdapter.disable();
if (!isDisabling)
{
}
}
}
}
});
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == RESULT_OK))
{
boolean isEnabling = mBluetoothAdapter.enable();
if (!isEnabling)
{
// an immediate error occurred - perhaps the bluetooth is already on?
}
else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON)
{
}
}
}
학생 여기 아니야