android 蓝牙bluetooth的简单使用
来源:互联网 发布:jdk 7u55 linux x64 编辑:程序博客网 时间:2024/04/30 07:22
布局文件:
<<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >>
<<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="open"
android:text="打开蓝牙" />
<<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="close"
android:text="关闭蓝牙" />>
<<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="findMatch"
android:text="查找已配对的蓝牙设备" />>
<<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="findVisible"
android:text="查找可见的蓝牙设备" />> <</LinearLayout>>
代码
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//打开蓝牙
public void open(View view) {
// 获取本机蓝牙设备
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
//不支持蓝牙设备
Toast.makeText(this,"不支持蓝牙设备", 1).show();
return;
}
if (!adapter.isEnabled()){
//若蓝牙设备不可用时,打开蓝牙
//第一种:不提示用户,直接打开蓝牙
adapter.enable();
////第二种:提示用户是否打开蓝牙设备,设置蓝牙为可用
//IntentenableBtIntent = new Intent(
//BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBtIntent,1);
//第三种:打开蓝牙,并设置可见时间
IntentdiscoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(discoverableIntent);
}
}
//关闭蓝牙
public void close(View view) {
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
if(adapter.isEnabled()) {
//禁用蓝牙设备
adapter.disable();
}
} else {
Toast.makeText(this,"不支持蓝牙设备", 1).show();
}
}
//查找周围已配对的蓝牙设备
public void findMatch(View view){
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
Set bondedDevices =adapter.getBondedDevices();
for(BluetoothDevicedevice:bondedDevices){
System.out.println("名称:"+device.getName()+"--地址:"+device.getAddress());
}
}
//查找可见的蓝牙设备
public void findVisible(View view){
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
if(adapter == null){
//不支持蓝牙设备
return;
}
adapter.startDiscovery();//开始查找周围可见的蓝牙设备,最多10米
IntentFilter filter = newIntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, filter);
}
//广播
private BroadcastReceiver broadcastReceiver = newBroadcastReceiver() {
@Override
public void onReceive(Contextcontext, Intent intent) {
String action= intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//发现蓝牙设备
BluetoothDevicedevice =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println("发现设备:"+device.getName()+"--地址:"+device.getAddress());
}
}
};
@Override
protected void onDestroy() {
//取消注册广播
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
权限必须在清单文件中进行声明: <<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>>
<<uses-permissionandroid:name="android.permission.BLUETOOTH"/>>
- android 蓝牙bluetooth的简单使用
- Android的蓝牙串口(Bluetooth SPP)使用
- Bluetooth--- android -- 蓝牙 bluetooth
- Android中的Bluetooth(蓝牙)使用步骤
- Bluetooth---如何使用Android蓝牙开发
- Bluetooth---如何使用Android蓝牙开发
- Bluetooth---Android蓝牙4.0的数据通讯
- Bluetooth---Android的蓝牙聊天示例应用程序
- android Bluetooth的实现蓝牙操作
- 有关Android Bluetooth/蓝牙的介绍
- BlueTooth蓝牙的使用场景(二)
- android bluetooth蓝牙移植
- Android BlueTooth蓝牙通信
- android -- 蓝牙 bluetooth 入门
- [Android]蓝牙bluetooth
- android 蓝牙 通信 bluetooth
- android -- 蓝牙 bluetooth解读
- android 蓝牙 通信 bluetooth
- andoid自定义dialog实现登陆界面
- 自定义DialogFragment如何与打开它…
- android notification通知的…
- codeforces 382C
- 2498 AOE网上的关键路径
- android 蓝牙bluetooth的简单使用
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
- android wifi的打开关闭以及…
- Android Sdk Manager 无法下载资源
- android 画廊控件Gallery的使用
- tabHost选项卡的简单使用
- Activity com.example.weibo.…
- nginx超详细讲解之location,rewrite,反向代理及负载均衡
- 开源项目SlidingMenu滑动菜单栏的…