android 连接蓝牙

打开本地的蓝牙设备
package com.yifei.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private Button btn;
    private BluetoothAdapter mbluetoothAdapter; //获取本地蓝牙的适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.textView);
        btn = findViewById(R.id.btn);

        //获取本地蓝牙的适配器
        mbluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        判断蓝牙功能是否存在
        if (mbluetoothAdapter == null) {
            showToast("不支持蓝牙功能");
            return;//不支持蓝牙功能之间返回
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开本地的蓝牙设备
                //判断蓝牙是否已经打开
                if(mbluetoothAdapter.isEnabled()){
                    showToast("蓝牙已经处于打开状态");
                }else {
                    //蓝牙处于关闭的状态 打开蓝牙
                    boolean isOpen = mbluetoothAdapter.enable();
                    showToast("蓝牙的状态"+isOpen);

                }
            }
        });


    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

利用系统的API打开蓝牙

package com.yifei.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private Button btn;
    private BluetoothAdapter mbluetoothAdapter; //获取本地蓝牙的适配器
    private int requestCode=1002;//请求打开蓝牙
    private String TAG ="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.textView);
        btn = findViewById(R.id.btn);

        //获取本地蓝牙的适配器
        mbluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        判断蓝牙功能是否存在
        if (mbluetoothAdapter == null) {
            showToast("不支持蓝牙功能");
            return;//不支持蓝牙功能之间返回
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开本地的蓝牙设备
                //判断蓝牙是否已经打开
                if(mbluetoothAdapter.isEnabled()){
                    showToast("蓝牙已经处于打开状态");
                }else {
                    //蓝牙处于关闭的状态 打开蓝牙
                    //调用系统的API 打开蓝牙
                    Intent open = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(open,requestCode);
                    showToast("蓝牙的状态");

                }
            }
        });


    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==requestCode){

            Log.d(TAG, "onActivityResult: "+data);
            if(resultCode ==RESULT_CANCELED){ //判断用户是否有有打开蓝牙
                showToast("您取消了打开蓝牙");
            }else if(resultCode==RESULT_OK){
                showToast("蓝牙已经打开了");
            }
        }
    }
}

关闭蓝牙

 mbluetoothAdapter.disable();
//获取蓝牙的名字 MAC 地址
 //获取蓝牙的名字 MAC 地址
        String name = mbluetoothAdapter.getName();
        String address = mbluetoothAdapter.getAddress();

查询蓝牙的状态

int state = mbluetoothAdapter.getState();
        switch (state) {
            case BluetoothAdapter.STATE_TURNING_ON://正在打开
                showToast("正在打开");
                break;
            case BluetoothAdapter.STATE_TURNING_OFF://正在关闭
                showToast("正在关闭");
                break;
            case BluetoothAdapter.STATE_OFF:
                showToast("已经关闭");
                break;
            case BluetoothAdapter.STATE_ON:
                showToast("已经打开");
                break;
            default:
                break;
        }

设置toast的位置

package com.yifei.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private Button btn;
    private BluetoothAdapter mbluetoothAdapter; //获取本地蓝牙的适配器
    private int requestCode = 1002;//请求打开蓝牙
    private String TAG = "MainActivity1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.textView);
        btn = findViewById(R.id.btn);

        //获取本地蓝牙的适配器
        mbluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        判断蓝牙功能是否存在
        if (mbluetoothAdapter == null) {
            showToast("不支持蓝牙功能");
            return;//不支持蓝牙功能之间返回
        }

        //获取蓝牙的名字 MAC 地址
        String name = mbluetoothAdapter.getName();
        String address = mbluetoothAdapter.getAddress();

        //获取当前蓝牙的状态

        int state = mbluetoothAdapter.getState();
        switch (state) {
            case BluetoothAdapter.STATE_TURNING_ON://正在打开
                showToast("正在打开");
                break;
            case BluetoothAdapter.STATE_TURNING_OFF://正在关闭
                showToast("正在关闭");
                break;
            case BluetoothAdapter.STATE_OFF:
                showToast("已经关闭");
                break;
            case BluetoothAdapter.STATE_ON:
                showToast("已经打开");
                break;
            default:
                break;
        }


        Log.d(TAG, "onCreate: name --" + name + "--adress" + address);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开本地的蓝牙设备
                //判断蓝牙是否已经打开
                if (mbluetoothAdapter.isEnabled()) {
                    showToast("蓝牙已经处于打开状态"); //蓝牙已经打开,就将其关闭
                    mbluetoothAdapter.disable();
                } else {
                    //蓝牙处于关闭的状态 打开蓝牙
                    //调用系统的API 打开蓝牙
                    Intent open = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(open, requestCode);
                    showToast("蓝牙的状态");

                }
            }
        });


    }

    public void showToast(String msg) {
        Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
        toast.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == requestCode) {

            Log.d(TAG, "onActivityResult: " + data);
            if (resultCode == RESULT_CANCELED) { //判断用户是否有有打开蓝牙
                showToast("您取消了打开蓝牙");
            } else if (resultCode == RESULT_OK) {
                showToast("蓝牙已经打开了");
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值