<?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() > ) {
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. [VBS脚本]定时提示并关闭计算机
  2. 兜转数年,老跳成了卖过软件开过店写过APP的电脑老师
  3. npm包管理器相关知识
  4. Swift运行时简介
  5. JNI实战(一):JNI HelloWorld
  6. Python函数式编程(一):高级函数
  7. Go语言模拟实现简单的区块链
  8. 线段树(lazy)-hdu1689
  9. PHP实现 手机号码归属地查询
  10. transient关键字详解
  11. WebService远程调用技术
  12. spring4.0之六:Generic Qualifier(泛型限定)
  13. grep命令的常用选项
  14. 【MySQL】查询时强制区分大小写的方法
  15. Android开发——JVM、Dalvik以及ART的区别【转帖】
  16. UML相关说明
  17. jQuery实际案例⑥——图片跟随鼠标、五角星评分案例
  18. BZOJ5335:[TJOI2018]智力竞赛——题解
  19. [GLSL]着色器周记02——火焰特效 【转】
  20. Java并发编程(三):并发模拟(工具和Java代码介绍)

热门文章

  1. 论文笔记之:DeepCAMP: Deep Convolutional Action &amp; Attribute Mid-Level Patterns
  2. MVC View中获取Url参数的值
  3. unity 合并skinnedMeshRenderer中遇到的一个大坑
  4. Advancing The Realtime Web With RethinkDB
  5. Linux -RAID
  6. python数据类型之 set
  7. flex html 用flex展示html
  8. Redis学习手册(List数据类型)
  9. oracle 行转列的例子
  10. MYSQL 分组排名