<?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"/>

更多相关文章

  1. Android内核和驱动篇-Android内核介绍
  2. Android内核和驱动篇-Android内核介绍
  3. Android(安卓)控制闪光灯
  4. Spring for Android(安卓)探究
  5. Spring for Android(安卓)探究
  6. Android(安卓)内存泄漏优化汇总
  7. android的adb常用命令使用
  8. Android(安卓)常用 adb 命令总结
  9. android的adb常用命令使用

随机推荐

  1. SQL数据库日志已满解决方法
  2. SQL order by ID desc/asc加一个排序的字
  3. 使用xp_cmdshell注销Windows登录用户(终
  4. sql server字符串非空判断实现方法
  5. sqlldr装载数据实现代码
  6. Sql Server触发器的使用
  7. jdbc连接sql server数据库问题分析
  8. SQL Server高可用的常见问题分析
  9. sql处理数据库锁的存储过程分享
  10. SQLSERVER 本地查询更新操作远程数据库的