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"/>>


 

0 0
原创粉丝点击