蓝牙操作二

来源:互联网 发布:手机数据恢复 编辑:程序博客网 时间:2024/05/20 12:24

一:修改本机蓝牙设置的可见性
    每一个蓝牙设备都会有一个可见性的设置,什么叫可见性呢?你把你的蓝牙设备设置为可见,那么别人的蓝牙设备就可以扫描到你手机上的这个蓝牙设备,如果你把你的蓝牙设备设置为不可见,那么别人的蓝牙设备就无法扫描到你手机上的蓝牙设备的,一般的我们不会把蓝牙设备可见性设置为永久可见,它总会有一个时间段,比如蓝牙设备在未来300秒内是可见的,过了300秒又回归到不可见状态,这样做主要是为了考虑到手机里数据的安全性。
    1:通过”设置“来达到修改蓝牙可见性
    点击“蓝牙设置”选项,勾选“可检测”,如果不勾选那么现在手机上的蓝牙设备处于不可见的状态,也就是说别人扫描的时候是扫描不到我的蓝牙设备的,勾选“可检测”后,发现时间开始是117秒可检测到,刷新一下变成111秒可检测到,它在不断的倒计时也就是说手机上的这个蓝牙设备不是总是可见的,默认是120秒这个蓝牙是可见的,过了这120秒这个蓝牙设备又变成不可见状态了,之前说到是为了安全性的问题。
     8.jpg
2011-12-20 17:31 上传
下载附件(37.19 KB)
     7.jpg
2011-12-20 17:31 上传
下载附件(39.47 KB)
     6.jpg
2011-12-20 17:31 上传
下载附件(38.4 KB)
    2:通过代码来达到修改蓝牙可见性
      2.1:需要在AndroidMainfest.xml里声明蓝牙权限和蓝牙管理权限
  1. <uses-permission android:name="android.permission.BLUETOOTH" />
  2.         <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
复制代码
2.2:布局文件main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.                android:orientation="vertical"
  4.                android:layout_width="fill_parent"
  5.                android:layout_height="fill_parent"
  6.            >
  7.         <TextView  
  8.                 android:layout_width="fill_parent"
  9.                 android:layout_height="wrap_content"
  10.                 android:text="@string/hello"
  11.           />
  12.         <Button
  13.               android:layout_width="fill_parent"
  14.               android:layout_height="wrap_content"
  15.               android:text="设置可见性"
  16.               android:id="@+id/btnkejianxing"
  17.         />
  18.            <Button
  19.              android:layout_width="fill_parent"
  20.               android:layout_height="wrap_content"
  21.               android:text="开始扫描"
  22.              android:id="@+id/btnsaomiao"
  23.         />
  24.         </LinearLayout>
复制代码
    5.jpg
2011-12-20 17:31 上传
下载附件(23.5 KB)
      3:代码文件MainActivity.java       
  1. package com.szy.bluetooth2;
  2.         import java.util.Iterator;
  3.         import java.util.Set;
  4.         import android.app.Activity;
  5.         import android.bluetooth.BluetoothAdapter;
  6.         import android.bluetooth.BluetoothDevice;
  7.         import android.content.BroadcastReceiver;
  8.         import android.content.Context;
  9.         import android.content.Intent;
  10.         import android.content.IntentFilter;
  11.         import android.os.Bundle;
  12.         import android.util.Log;
  13.         import android.view.View;
  14.         import android.view.View.OnClickListener;
  15.         import android.widget.Button;
  16.         public class MainActivity<BluetoothReceiver> extends Activity
  17.         {
  18.            private Button btn_saomiao = null;
  19.           public void onCreate(Bundle savedInstanceState)
  20.            {
  21.                   super.onCreate(savedInstanceState);
  22.                   setContentView(R.layout.main);
  23.                   //得到可见性按钮
  24.                   btn_kejianxing = (Button)findViewById(R.id.btnkejianxing);
  25.                   //绑定可见性按钮监听器
  26.                   btn_kejianxing.setOnClickListener(new KeJianXingButtonListener());
  27.           }

  28.              //可见性按钮监听器,该监听器用于修改蓝牙设备可见性
  29.             private class KeJianXingButtonListener implements OnClickListener
  30.             {
  31.             @Override
  32.            public void onClick(View v)
  33.            {
  34.                //创建一个Intent对象,并且将其action的值设置为BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE也就是蓝牙设备设置为可见状态
  35.                Intent kejianxingIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  36.                //将一个键值对存放到Intent对象当中,主要用于指定可见状态的持续时间,大于300秒,就认为是300秒
  37.                kejianxingIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
  38.                //这个Activity实际上是安卓自带的一个Activity
  39.                startActivity(kejianxingIntent);
  40.    
  41.             }           
  42.                   }
  43.        }
复制代码
4:通过代码设置手机可见性效果图
        我们先通过手机的设置关闭它的可检测性,然后运行代码后发现手机的可检测性打开了。
         4.jpg
2011-12-20 17:30 上传
下载附件(57.98 KB)
         3.jpg
2011-12-20 17:30 上传
下载附件(61.47 KB)

  二:扫描周围可用的且没配对的蓝牙设备
  在蓝牙操作(一)中我们讲到扫描已经配对的蓝牙设备,其实已经配对的蓝牙设备的信息都已经被存储在你的手机里面了,所以即使你不打开蓝牙适配器,你也可以得到你的这个手机以前配对过的那些蓝牙设备的那些信息。今天我们要讲的操作,只要是你的手机的蓝牙信号能够覆盖到的地方,我们都可以去扫描所有的可见的蓝牙设备,都可以得到这些蓝牙设备的基本信息,进行通讯还不行,需要先配对在通过一个协议来进行数据的交换,我们先实现第一步先找到周围这些蓝牙设备。
           1:布局文件与上面的main.xml一致
           2:代码文件MainActivity.java
  1.    package com.szy.bluetooth2;
  2.         import java.util.Iterator;
  3.         import java.util.Set;
  4.       import android.app.Activity;
  5.       import android.bluetooth.BluetoothAdapter;
  6.       import android.bluetooth.BluetoothDevice;
  7.       import android.content.BroadcastReceiver;
  8.       import android.content.Context;
  9.       import android.content.Intent;
  10.       import android.content.IntentFilter;
  11.       import android.os.Bundle;
  12.       import android.util.Log;
  13.       import android.view.View;
  14.       import android.view.View.OnClickListener;
  15.       import android.widget.Button;
  16.       public class MainActivity<BluetoothReceiver> extends Activity
  17.       {
  18.         private BluetoothAdapter bluetoothAdapter = null;
  19.                       private BluetoothReceiver bluetoothReceiver = null;
  20.          public void onCreate(Bundle savedInstanceState)
  21.         {
  22.                   super.onCreate(savedInstanceState);
  23.                   setContentView(R.layout.main);
  24.                   //得到扫描周围蓝牙设备按钮
  25.                   btn_saomiao = (Button)findViewById(R.id.btnsaomiao);
  26.                   //绑定扫描周围蓝牙设备按钮监听器
  27.                  btn_saomiao.setOnClickListener(new SaoMiaoButtonListener());
  28.                                    //得到本机蓝牙设备
  29.                                    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  30.                                    //创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND
  31.                                    //IntentFilter它是一个过滤器,只有符合过滤器的Intent才会被我们的BluetoothReceiver所接收
  32.                                    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  33.                                   //创建一个BluetoothReceiver对象
  34.                                   bluetoothReceiver = new BluetoothReceiver();
  35.                                   //注册广播接收器 注册完后每次发送广播后,BluetoothReceiver就可以接收到这个广播了
  36.                                   registerReceiver(bluetoothReceiver, intentFilter);
  37.         }

  38.          //扫描周围的蓝牙设备按钮监听器
  39.                       private class SaoMiaoButtonListener implements OnClickListener
  40.                       {
  41.                                 @Override
  42.                                 public void onClick(View v)
  43.                                 {   
  44.                                            //扫描周围的可见的蓝牙设备一次要消耗12秒,废电池电量
  45.                                            //扫描到了后结果我们怎么接收呢,扫描周围的蓝牙设备每扫描到一个蓝牙设备就会发送一个广播,我们就需要BroadcastReceiver来接收这个广播,这个函数是异步的调用,并不是扫描12之后才返回结果的,只要一调用这个函数马上返回,不会等12秒
  46.                                             bluetoothAdapter.startDiscovery();
  47.                                  }           
  48.                         }

  49.                    //接收广播
  50.                    private class BluetoothReceiver extends BroadcastReceiver
  51.                    {
  52.                                  public void onReceive(Context context, Intent intent)
  53.                                 {
  54.                                              String action = intent.getAction();
  55.                                              if(BluetoothDevice.ACTION_FOUND.equals(action))
  56.                                              {   
  57.                                                         //只要BluetoothReceiver接收到来自于系统的广播,这个广播是什么呢,是我找到了一个远程蓝牙设备
  58.                                                         //Intent代表刚刚发现远程蓝牙设备适配器的对象,可以从收到的Intent对象取出一些信息
  59.                                                         BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  60.                                                         Log.d("mytag",bluetoothDevice.getAddress());
  61.                                               }
  62.                                  }
  63.                       }
  64.      }
复制代码
3:扫描周围可用的且没配对的蓝牙设备效果图
              我们先关掉之前和蓝牙笔的配对,然后运行代码发现同样得到蓝牙笔的蓝牙适配器的地址(打开蓝牙笔)
               2.jpg
2011-12-20 17:30 上传
下载附件(62.76 KB)
               1.jpg
2011-12-20 17:30 上传
下载附件(29.44 KB)

原创粉丝点击