安卓Android蓝牙基本操作

来源:互联网 发布:自由现金流知乎 编辑:程序博客网 时间:2024/05/17 21:41
蓝牙是一种支持设备间近距离传输数据的无线电技术,支持有蓝牙功能的设备一般有:手机,笔记本电脑,无线蓝牙耳机等。该小例子就要是获取手机蓝牙基本信息及打开关闭蓝牙操作。
public class MainActivity extends Activity implements View.OnClickListener {    private static final int OPEN_REQUEST = 11;    private TextView enable_tv;    private TextView name_tv;    private TextView state_tv;    private Button operator_btn;    /**     * BluetoothAdapter代表了本设备(手机、电脑等)的蓝牙适配器对象,通过它可以对蓝牙设备进行如下功能操作:     * 1、开关蓝牙设备     * 2、扫描蓝牙设备     * 3、设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等     */    private BluetoothAdapter mBluetoothAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();        initBluetoothAdapter();    }    /**     * @description 初始化蓝牙适配器对象     * @author ldm     * @time 2017/4/7 16:54     */    private void initBluetoothAdapter() {        //通过getDefaultAdapter获取BluetoothAdapter实例对象        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();        if (null == mBluetoothAdapter) {//当前手机不支持蓝牙功能            enable_tv.setText("当前手机不支持蓝牙功能");            name_tv.setText("蓝牙设备:无");            operator_btn.setVisibility(View.GONE);        } else {            enable_tv.setText("当前手机支持蓝牙功能");            //BluetoothAdapter.getName()方法获取蓝牙名称            name_tv.setText("蓝牙设备:" + mBluetoothAdapter.getName());            operator_btn.setVisibility(View.VISIBLE);        }        if (mBluetoothAdapter.isEnabled()) {//当前蓝牙处理打开状态            operator_btn.setText("关闭蓝牙");        } else {            operator_btn.setText("打开蓝牙");        }        /**         * 蓝牙状态如:         * BluetoothAdapter.STATE_ON:蓝牙已经打开         * BluetoothAdapter.STATE_TURNING_ON:蓝牙正在打开         * BluetoothAdapter.STATE_TURNING_OFF:蓝牙正在关闭         * BluetoothAdapter.STATE_OFF:蓝牙已经关闭         * 其它更多状态         */        int state = mBluetoothAdapter.getState();//蓝牙状态    }    private void initViews() {        this.enable_tv = (TextView) findViewById(R.id.enable_tv);        this.name_tv = (TextView) findViewById(R.id.name_tv);        this.state_tv = (TextView) findViewById(R.id.state_tv);        this.operator_btn = (Button) findViewById(R.id.operator_btn);        this.operator_btn.setOnClickListener(this);    }    @Override    public void onClick(View view) {        if (view.getId() == R.id.operator_btn) {            if (!mBluetoothAdapter.isEnabled()) {//如果蓝牙未打开                //打开蓝牙方式1:调用enable()方法                mBluetoothAdapter.enable();                //打开蓝牙方式2:调用系统API打开//                Intent openIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);//                startActivityForResult(openIntent, OPEN_REQUEST);                operator_btn.setText("关闭蓝牙");            } else {                //关闭蓝牙操作                mBluetoothAdapter.disable();                operator_btn.setText("打开蓝牙");            }        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (OPEN_REQUEST == requestCode) {            if (resultCode == RESULT_OK) {                Toast.makeText(MainActivity.this, "打开成功", Toast.LENGTH_LONG).show();            } else {                Toast.makeText(MainActivity.this, "打开失败", Toast.LENGTH_LONG).show();            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout        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"        tools:context="com.ldm.bluetoothdemo.MainActivity"        android:orientation="vertical"        android:padding="16dp">    <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Android本地蓝牙设备信息"            android:textSize="16sp"            android:layout_gravity="center"            />    <TextView            android:id="@+id/enable_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:textSize="16sp"            />    <TextView            android:id="@+id/name_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:textSize="16sp"            />    <TextView            android:id="@+id/state_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:textSize="16sp"            />    <Button            android:id="@+id/operator_btn"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@null"            android:layout_marginTop="10dp"            android:textSize="16sp"            android:gravity="center"            /></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

最后不要忘记添加权限:

<!--配置蓝牙操作权限-->    <uses-permission android:name="android.permission.BLUETOOTH"/>    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
在Android6.0以上系统中,这两个权限也属于普通权限,不用做动态权限处理。
0 0
原创粉丝点击