您的位置:首页 > 产品设计 > UI/UE

Android深入浅出系列之Bluetooth—蓝牙操作(二)

2011-11-03 13:52 411 查看
  一:修改本机蓝牙设置的可见性

    每一个蓝牙设备都会有一个可见性的设置,什么叫可见性呢?你把你的蓝牙设备设置为可见,那么别人的蓝牙设备就可以扫描到你手机上的这个蓝牙设备,如果你把你的蓝牙设备设置为不可见,那么别人的蓝牙设备就无法扫描到你手机上的蓝牙设备的,一般的我们不会把蓝牙设备可见性设置为永久可见,它总会有一个时间段,比如蓝牙设备在未来300秒内是可见的,过了300秒又回归到不可见状态,这样做主要是为了考虑到手机里数据的安全性。

    1:通过”设置“来达到修改蓝牙可见性

    点击“蓝牙设置”选项,勾选“可检测”,如果不勾选那么现在手机上的蓝牙设备处于不可见的状态,也就是说别人扫描的时候是扫描不到我的蓝牙设备的,勾选“可检测”后,发现时间开始是117秒可检测到,刷新一下变成111秒可检测到,它在不断的倒计时也就是说手机上的这个蓝牙设备不是总是可见的,默认是120秒这个蓝牙是可见的,过了这120秒这个蓝牙设备又变成不可见状态了,之前说到是为了安全性的问题。

    


    


    


    2:通过代码来达到修改蓝牙可见性

      2.1:需要在AndroidMainfest.xml里声明蓝牙权限和蓝牙管理权限

        <uses-permission android:name="android.permission.BLUETOOTH" />

        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

      2.2:布局文件main.xml

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
       >
        <TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/hello"
       />
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="设置可见性"
            android:id="@+id/btnkejianxing"
        />
       <Button
             android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="开始扫描"
             android:id="@+id/btnsaomiao"
        />
        </LinearLayout>



      3:代码文件MainActivity.java       

        package com.szy.bluetooth2;

        import java.util.Iterator;
        import java.util.Set;
        import android.app.Activity;
        import android.bluetooth.BluetoothAdapter;
        import android.bluetooth.BluetoothDevice;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;

        public class MainActivity<BluetoothReceiver> extends Activity

        {

           private Button btn_saomiao = null;

          public void onCreate(Bundle savedInstanceState)

           {
          super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
          //得到可见性按钮
          btn_kejianxing = (Button)findViewById(R.id.btnkejianxing);
          //绑定可见性按钮监听器
           btn_kejianxing.setOnClickListener(new KeJianXingButtonListener());

          }

         //可见性按钮监听器,该监听器用于修改蓝牙设备可见性
        private class KeJianXingButtonListener implements OnClickListener
         {
          @Override
           public void onClick(View v)
           {
            //创建一个Intent对象,并且将其action的值设置为BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE也就是蓝牙设备设置为可见状态
            Intent kejianxingIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            //将一个键值对存放到Intent对象当中,主要用于指定可见状态的持续时间,大于300秒,就认为是300秒
            kejianxingIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
            //这个Activity实际上是安卓自带的一个Activity
            startActivity(kejianxingIntent);

          }
    }

       }

      4:通过代码设置手机可见性效果图

        我们先通过手机的设置关闭它的可检测性,然后运行代码后发现手机的可检测性打开了。

        


        


  二:扫描周围可用的且没配对的蓝牙设备

  在蓝牙操作(一)中我们讲到扫描已经配对的蓝牙设备,其实已经配对的蓝牙设备的信息都已经被存储在你的手机里面了,所以即使你不打开蓝牙适配器,你也可以得到你的这个手机以前配对过的那些蓝牙设备的那些信息。今天我们要讲的操作,只要是你的手机的蓝牙信号能够覆盖到的地方,我们都可以去扫描所有的可见的蓝牙设备,都可以得到这些蓝牙设备的基本信息,进行通讯还不行,需要先配对在通过一个协议来进行数据的交换,我们先实现第一步先找到周围这些蓝牙设备。

1:布局文件与上面的main.xml一致

2:代码文件MainActivity.java

package com.szy.bluetooth2;

      import java.util.Iterator;
      import java.util.Set;
      import android.app.Activity;
      import android.bluetooth.BluetoothAdapter;
      import android.bluetooth.BluetoothDevice;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;

      public class MainActivity<BluetoothReceiver> extends Activity

      {

        private BluetoothAdapter bluetoothAdapter = null;
private BluetoothReceiver bluetoothReceiver = null;

        public void onCreate(Bundle savedInstanceState)

        {
          super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
          //得到扫描周围蓝牙设备按钮

          btn_saomiao = (Button)findViewById(R.id.btnsaomiao);

          //绑定扫描周围蓝牙设备按钮监听器

          btn_saomiao.setOnClickListener(new SaoMiaoButtonListener());

//得到本机蓝牙设备
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND
//IntentFilter它是一个过滤器,只有符合过滤器的Intent才会被我们的BluetoothReceiver所接收
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//创建一个BluetoothReceiver对象
bluetoothReceiver = new BluetoothReceiver();
//注册广播接收器 注册完后每次发送广播后,BluetoothReceiver就可以接收到这个广播了
registerReceiver(bluetoothReceiver, intentFilter);

        }

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

//接收广播
private class BluetoothReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
//只要BluetoothReceiver接收到来自于系统的广播,这个广播是什么呢,是我找到了一个远程蓝牙设备
//Intent代表刚刚发现远程蓝牙设备适配器的对象,可以从收到的Intent对象取出一些信息
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("mytag",bluetoothDevice.getAddress());
}
}
}

     }

3:扫描周围可用的且没配对的蓝牙设备效果图

我们先关掉之前和蓝牙笔的配对,然后运行代码发现同样得到蓝牙笔的蓝牙适配器的地址(打开蓝牙笔)



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: