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


    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" />
  • 相关阅读:
    程序员必备的代码审查(Code Review)清单
    Laravel 在homestead 平台上命令
    Laravel5.5执行 npm run dev时报错,提示cross-env找不到(not found)的解决办法
    Laravel 的Artisan 命令学习
    github常见操作和常见错误!错误提示:fatal: remote origin already exists.
    Sublime如何设置背景透明
    jquery判断滚动条是否到底部
    mysql的数据恢复
    MySQL体系结构
    mysql-trigger-触发器
  • 原文地址:https://www.cnblogs.com/hllive/p/5574413.html
Copyright © 2020-2023  润新知