安卓开发实现蓝牙打开和关闭

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="hard.practice.MainActivity">

    <Button
        android:id="@+id/open"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="OPEN" />

    <Button
        android:id="@+id/visible"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Visible" />

    <Button
        android:id="@+id/discover"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Discover" />

    <Button
        android:id="@+id/off"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="OFF" />
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>


MainActivity:
 
package hard.practice;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

import static hard.practice.R.id.lv;

public class MainActivity extends AppCompatActivity{

    private Button open, visible, discover, off;
    private BluetoothAdapter BA;
    private Set<BluetoothDevice> SBD;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        open = (Button) findViewById(R.id.open);
        visible = (Button) findViewById(R.id.visible);
        discover = (Button) findViewById(R.id.discover);
        off = (Button) findViewById(R.id.off);
        listView= (ListView) findViewById(lv);
        BA = BluetoothAdapter.getDefaultAdapter();

        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BA.isEnabled()){
                    Intent open = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(open,0);
                }
            }
        });
        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BA.disable();
            }
        });

        visible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent visible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(visible,0);
            }
        });

        discover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SBD=BA.getBondedDevices();
                ArrayList list = new ArrayList();
                for (BluetoothDevice bt :SBD) list.add(bt.getName());
                final ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
                listView.setAdapter(adapter);

            }
        });
    }
}
权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值