蓝牙操作 Bluetooth
16lz
2021-01-26
蓝牙操作 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
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
更多相关文章
- android launcher总体分析 .
- Android_Intent意图详解
- [Android]获取设备IP地址
- Android之传感器(一)
- Android之解析Json数据 .
- android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和
- Android(安卓)布局转化为View对象的两种方法
- Android(安卓)USB Host与HID通讯(二)
- android 里面的 Drawable 和 ConstantState