码迷,mamicode.com
首页 > 其他好文 > 详细

判断蓝牙是否打开并打开蓝牙

时间:2016-06-10 23:01:56      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1、简单布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hllive.learn.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="黄元浪DEMO_测试蓝牙是否打开"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开蓝牙"
        android:id="@+id/openBlue"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

技术分享

2、MainActivity.java

package com.example.hllive.learn;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ChikcBlue();
        Button bt = (Button) findViewById(R.id.openBlue);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChikcBlue();
            }
        });
    }
    public void ChikcBlue(){
        BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
        //支持蓝牙模块
        if (blueadapter!=null){
            if (blueadapter.isEnabled()){
                Toast tst = Toast.makeText(MainActivity.this, "蓝牙已经打开", Toast.LENGTH_SHORT);
                tst.show();
            }
            else {
                new AlertDialog.Builder(MainActivity.this).setTitle("蓝牙功能尚未打开,是否打开蓝牙")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (turnOnBluetooth()){
                                    Toast tst = Toast.makeText(MainActivity.this, "打开蓝牙成功", Toast.LENGTH_SHORT);
                                    tst.show();
                                }
                                else {
                                    Toast tst = Toast.makeText(MainActivity.this, "打开蓝牙失败!!", Toast.LENGTH_SHORT);
                                    tst.show();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 点击“返回”后的操作,这里不设置没有任何操作
                            }
                        }).show();
            }
        }else{//不支持蓝牙模块
            Toast tst = Toast.makeText(MainActivity.this, "该设备不支持蓝牙或没有蓝牙模块", Toast.LENGTH_SHORT);
            tst.show();
        }
    }
    /**
     * 强制开启当前 Android 设备的 Bluetooth
     * @return true:强制打开 Bluetooth 成功 false:强制打开 Bluetooth 失败
     */
    public static boolean turnOnBluetooth()
    {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter
                .getDefaultAdapter();

        if (bluetoothAdapter != null)
        {
            return bluetoothAdapter.enable();
        }

        return false;
    }
}

3、在Androidmanifest.xml里添加权限代码

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

 

判断蓝牙是否打开并打开蓝牙

标签:

原文地址:http://www.cnblogs.com/hllive/p/5574413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!