蓝牙操作 Bluetooth

蓝牙操作 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);

更多信息:[url][u]http://www.eoeandroid.com/thread-18993-1-1.html[/u][/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值