控制蓝牙打开和关闭程序,在手机上运行时一打开就自动关闭,求大神帮忙

xyc112046 2015-12-01 07:34:26
package com.example.chong.bluetooth11;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private BluetoothAdapter bluetoothAdapter=null; //本地蓝牙适配器
private TextView statusText=null; //显示TEXTview组件对象
private Button openButton=null; //打开button组件对象
private Button closeButton=null; //关闭button组件对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
statusText=(TextView) findViewById(R.id.status_text);
openButton=(Button) findViewById(R.id.open_button);
closeButton=(Button) findViewById(R.id.close_button);
openButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openBluetooth(); //调用蓝牙开启方法
statusText.setText("蓝牙状态:开启");
}
});

closeButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
closeBluetooth();
statusText.setText("蓝牙状态:关闭");
}

});
//BluetoothAdapter()函数用于获取本地蓝牙适配器
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
Toast.makeText(this, "该设备不支持蓝牙", Toast.LENGTH_LONG).show();
finish();
return;
}
if(bluetoothAdapter.isEnabled()){
statusText.setText("蓝牙状态:开启");
}else{
statusText.setText("蓝牙状态:关闭");}
}
protected void openBluetooth() {
// TODO Auto-generated method stub
if(!bluetoothAdapter.isEnabled()){
Intent enableIntent=new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE); //open lanya
startActivity(enableIntent); //提示蓝牙正在开启
Toast.makeText(this, "蓝牙开启中。。。。。。", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show();
}
}
//关闭蓝牙
protected void closeBluetooth() {
// TODO Auto-generated method stub
if(bluetoothAdapter.isEnabled()){
bluetoothAdapter.disable();
Toast.makeText(this, "蓝牙已关闭。", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "蓝牙是关闭的", Toast.LENGTH_SHORT).show();
}
}
//打开蓝牙

}

布局文件
<?xml version="1.0" encoding="utf-8" ?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lanya" android:versionCode="1" android:versionName="1.0">
- <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
- <activity android:name=".MainActivity" android:label="@string/app_name">
- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="1" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
</manifest>
...全文
388 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangxiaohu_coder 2015-12-02
  • 打赏
  • 举报
回复
这种闪退一般都是有异常产生,你看看你的logcat打印,有什么exception的信息出来,然后在对症下药

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧