zoukankan      html  css  js  c++  java
  • android之蓝牙设备的使用01

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="hello"
        />
    <Button 
        android:id="@+id/scanButtonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="扫描周围的蓝牙设备"
        />
    </LinearLayout>
    private class ButtonListener implements OnClickListener {
            @Override
            public void onClick(View v) {
                //得到BluetoothAdapter对象
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                //判断BluetoothAdapter对象是否为空,如果为空,则没有蓝牙设备
                if (adapter != null) {
                    System.out.println("本机拥有蓝牙设备");
                    if (!adapter.isEnabled()) {
                        //如果蓝牙不可用,启动蓝牙
                        System.out.println("提示启动蓝牙");
                        Intent intent = new Intent(
                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivity(intent);
                    }
                    //得到所有已经配对的蓝牙适配器对象(没有打开蓝牙不能搜索到已经匹配的设备)
                    Set<BluetoothDevice> devices = adapter.getBondedDevices();
                    if (devices.size() > 0) {
                        for (Iterator iterator = devices.iterator(); iterator
                                .hasNext();) {
                            BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator
                                    .next();
                            //得到远程蓝牙设备的地址
                            System.out.println(bluetoothDevice.getAddress());
                        }
                    }
                } else {
                    System.out.println("不存在蓝牙设备");
                }
            }
        }
    <uses-permission android:name="android.permission.BLUETOOTH"/>
  • 相关阅读:
    git和svn
    [Luogu] P1144 最短路计数
    [Luogu] CF280C Game on Tree
    LCA的一种优秀实现方式(倍增+dfs序)
    [Luogu] P1131 [ZJOI2007]时态同步
    [Luogu] P2285 [HNOI2004]打鼹鼠
    背包相关问题总结
    【笔记】模拟梯度下降法的实现
    【笔记】梯度下降法的简单了解
    【笔记】线性回归的可解性和更多思考及线性回归总结
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3684359.html
Copyright © 2011-2022 走看看