蓝牙操作 Bluetooth (目前使用的最广泛的无线通讯协议 10m )
BluetbtoothAdapter 该类的对象代表了本地的蓝牙适配器
BluetoothDevice 代表了一个远程的Bluetooth的设备

模拟器不能模拟蓝牙
AndroidManifest.xml 声明蓝牙权限<users-permission android:name="android.permission.BLUETOOTH" />

扫描蓝牙设备步骤
获得BluetoothAdapter对象
判断当前设备中的时候拥有蓝牙设备
判断当前设备中的蓝牙设备时候已经打开
得到所有已经配到的蓝牙设备对象

//获得BluetoothAdapter对象
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
//判断BluetoothAdapter对象是否为空,如果是空,则表明本机没有蓝牙设备
if(adapter !=nulll){
System.out.println("本机拥有蓝牙设备");
//调用isEnabled()方法,判断当前蓝牙设备是否可用
if(!adapter.isEnabled()){
//创建一个intent对象,改对象用于启动一个activity,提示用户开启蓝牙设备
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//得到所有已经配到的蓝牙设备器对象
Set<BluetoothDevice> devices=adapter.getBondedDevices();
if(devices.size()>0){
for(Iterator iterattor=devices.iterator(); iterator.hasNext();){
BluetoothDevice bluetoothDevice=(BluetoothDevice) iterator.next();
//得到远程蓝牙设备的地址
System.out.println(bluetoothDevice.getAddress());
}
}
}else{
System.out.println("没有找到蓝牙设备器");
}


模拟器不能模拟蓝牙AndroidManifest.xml 修改蓝牙可见性
<users-permission android:name="android.permission.BLUETOOTH_ADMIN" />

设置可见性
//创建一个Intent对象,并设置action的值
Intent discoverableIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//将一个键值对存放在intent对象中,主要用于指定可见性状态的持续时间。设置可见性 时间500秒,但是最长是300秒 ,最终还是300秒
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverableIntent);

更多信息:http://www.eoeandroid.com/thread-18993-1-1.html

更多相关文章

  1. android launcher总体分析 .
  2. Android_Intent意图详解
  3. [Android]获取设备IP地址
  4. Android之传感器(一)
  5. Android之解析Json数据 .
  6. android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和
  7. Android(安卓)布局转化为View对象的两种方法
  8. Android(安卓)USB Host与HID通讯(二)
  9. android 里面的 Drawable 和 ConstantState

随机推荐

  1. Android(安卓)中文API (33) —— Checkable
  2. Android(安卓)Toolchain与Bionic Libc
  3. Android(安卓)OpenGL学习笔记(二)之----
  4. adroid app权限实现方式
  5. 【Android】- Android(安卓)Service的两
  6. Android中使用Handler引发的内存泄露
  7. android sax xml
  8. Android中Intent的各种常见作用。
  9. Android(安卓)横竖屏的锁定
  10. 管理Android库项目