Android 蓝牙的使用(待续)

本文介绍了Android系统中蓝牙的两种打开方式,包括授权打开和静默打开,并提供了扫描蓝牙设备的Demo1实例,强调了操作过程中的权限注意事项。
摘要由CSDN通过智能技术生成

一、蓝牙的打开方式(两种)

方式一(授权打开):

Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(intent);

方式二(静默打开):

    mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.enable();

注意权限:

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

二、扫描设备

Demo1

MainActivity

public class BlueToothDemoOne extends Activity {
    private Button mButtonBluetooth;
    private TextView mTextViewDeviceBonded;
    private TextView mTextViewDeviceSearch;

    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetoothone);
        mButtonBluetooth = (Button) findViewById(R.id.buttonbluetooth);
        mTextViewDeviceBonded = (TextView) findViewById(R.id.textview_bonded);
        mTextViewDeviceSearch = (TextView) findViewById(R.id.textview_search);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 获取之前配对过的设备
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

        if (devices != null) {
            for (BluetoothDevice device : devices) {
                mTextViewDeviceBonded.append("" + device.getName() + ":"
                        + device.getAddress());
            }
        }

        mBluetoothAdapter.enable();
        // 搜索到设备时发送广播
        IntentFilter intentfilter = new IntentFilter(
                BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, intentfilter);
        // 搜索完成时发送广播
        intentfilter = new IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, intentfilter);

    }

    public void click_start(View v) {
        // 正在搜索时点击按钮将停止搜索
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
            mButtonBluetooth.setText("开始搜索");

        } else {
            // 开启搜索设备
            mBluetoothAdapter.startDiscovery();
            mButtonBluetooth.setText("正在搜索");
        }

    }

    // 接收广播
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //根据不同的Intent.getAction进行分类
            if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
                //对搜索到的设备进行显示
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    mTextViewDeviceSearch.append(device.getName() + ":"
                            + device.getAddress() + "\n");
                }
            }
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent
                    .getAction())) {
                mTextViewDeviceSearch.append("搜索完毕");
                mButtonBluetooth.setText("开始搜索");
            }
        }

    };

}

布局:

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

    <Button
        android:id="@+id/buttonbluetooth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click_start"
        android:text="开始搜索" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="已配对的设备" />

    <TextView
        android:id="@+id/textview_bonded"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="#00f" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:text="搜索到的设备" />

    <TextView
        android:id="@+id/textview_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值