Android开发之蓝牙操作

蓝牙操作
一、什么是蓝牙
1、Bluetooth广泛使用的一种无线通讯协议
2、主要针对短距离通讯(10m)
3、常用于耳机、鼠标、键盘等移动通讯设备
二、与蓝牙相关的API
1、BluetoothAdapter:代表本地蓝牙设备
2、BluetoothDevice:代表远程蓝牙设备
三、蓝牙需要的设置
需要在AndroidManifest中申明蓝牙操作的权限


四、扫描已经配对的蓝牙设备
1、获取BluetoothAdapter对象
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter()
2、判断当前设备中是否有蓝牙设备
    if(adapter != null){}
3、判断当前设备中的蓝牙设备是否已经打开
    使用adapter.isEnabled()方法
4、得到所有已经配对的蓝牙设备
    Set devices = adapter.getBondedDevices();
五、设置蓝牙设备的可见性
1、创建intent对象:
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
2、设置可见性的时间,最大300s,超过300s系统会自动设为300s
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
3、启动Android内置activity,将参数传入
    startActivity(discoverableIntent);
六、扫描周围可见的蓝牙
1、调用startDiscovery()来扫描周围可见设备
    BluetoothAdapter.getDefaultAdapter().startDiscovery();
2、每次扫描到系统就会发送广播
    A、创建一个过滤器,过滤广播ACTION_FOUND
       IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    B、创建一个广播接收器
       BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
    C、 注册接收器
       registerReceiver(bluetoothReceiver, intentFilter);
    D、在广播中取出数据
       String action = intent.getAction();
       //从intent取出远程蓝牙设备
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
七、源代码
MainActivity.java

点击(此处)折叠或打开

  1. package com.example.bluetooth;

  2. import java.util.Iterator;
  3. import java.util.Set;

  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.app.SearchManager.OnCancelListener;
  7. import android.bluetooth.BluetoothAdapter;
  8. import android.bluetooth.BluetoothDevice;
  9. import android.content.BroadcastReceiver;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.IntentFilter;
  13. import android.content.DialogInterface.OnClickListener;
  14. import android.content.Intent;
  15. import android.view.Menu;
  16. import android.view.View;
  17. import android.webkit.WebView.FindListener;
  18. import android.widget.Button;
  19. import android.widget.EditText;

  20. public class MainActivity extends Activity
  21. {

  22.     private Button findBtn = null;
  23.     private Button scanBtn = null;
  24.     private Button setBtn = null;
  25.     private EditText edit1 = null;
  26.     
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState)
  29.     {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.         //找到按钮
  33.         findBtn = (Button)findViewById(R.id.findBtn);
  34.         scanBtn = (Button)findViewById(R.id.scanBtn);
  35.         setBtn = (Button)findViewById(R.id.setBtn);
  36.         //设置监听器
  37.         findBtn.setOnClickListener(new btnOnclickListener());
  38.         scanBtn.setOnClickListener(new btnOnclickListener());
  39.         setBtn.setOnClickListener(new btnOnclickListener());
  40.         
  41.         edit1 = (EditText)findViewById(R.id.edit1);
  42.         //创建一个过滤器,过滤广播ACTION_FOUND
  43.         IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  44.         //创建一个广播接收器
  45.         BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
  46.         //注册接收器
  47.         registerReceiver(bluetoothReceiver, intentFilter);
  48.     }

  49.     class btnOnclickListener implements android.view.View.OnClickListener
  50.     {

  51.         @Override
  52.         public void onClick(View v)
  53.         {
  54.             // TODO Auto-generated method stub
  55.             switch(v.getId())
  56.             {
  57.                 case R.id.findBtn:
  58.                     //获取BluetoothAdapter对象
  59.                     BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  60.                     //如果得到了设备
  61.                     if(adapter != null)
  62.                     {
  63.                         //更新edittext,打印有设备
  64.                         edit1.append("have!"+'\n');
  65.                         //如果设备没有被打开
  66.                         if(!adapter.isEnabled())
  67.                         {
  68.                             //启动新的activity,打开蓝牙
  69.                             Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  70.                             startActivity(intent);
  71.                         }
  72.                         //获取所有已经配对的蓝牙,得到一个蓝牙设备集
  73.                         SetBluetoothDevice> devices = adapter.getBondedDevices();
  74.                         if(devices.size()>0)
  75.                         {
  76.                             //如果有已经配对的蓝牙,那么用迭代逐个打印出来
  77.                             for(Iterator iterator=devices.iterator(); iterator.hasNext();)
  78.                             {
  79.                                 BluetoothDevice bluetoothDevice = (BluetoothDevice)iterator.next();
  80.                                 edit1.append(bluetoothDevice.getAddress()+'\n');
  81.                             }
  82.                         }
  83.                     }
  84.                     else
  85.                     {
  86.                         edit1.append("no device!!");
  87.                     }
  88.                     break;
  89.                 case R.id.scanBtn:
  90.                     //调用startDiscovery()来扫描周围可见设备
  91.                     BluetoothAdapter.getDefaultAdapter().startDiscovery();
  92.                     break;
  93.                 case R.id.setBtn:
  94.                     //创建intent通过intent传递参数
  95.                     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  96.                     //设置可见状态的持续时间,如果超过300s,那么系统会自动设为300s
  97.                     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
  98.                     //启动activity,这是Android自带的
  99.                     startActivity(discoverableIntent);
  100.                     break;
  101.                 default:
  102.                     break;
  103.             }
  104.             
  105.         }
  106.     }
  107.     
  108.     private class BluetoothReceiver extends BroadcastReceiver
  109.     {

  110.         @Override
  111.         public void onReceive(Context context, Intent intent)
  112.         {
  113.             // TODO Auto-generated method stub
  114.             //接受intent
  115.             String action = intent.getAction();
  116.             //从intent取出远程蓝牙设备
  117.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  118.             edit1.append("find new device:"+device.getAddress()+"\n");
  119.         }
  120.         
  121.     }
  122.     @Override
  123.     public boolean onCreateOptionsMenu(Menu menu)
  124.     {
  125.         // Inflate the menu; this adds items to the action bar if it is present.
  126.         getMenuInflater().inflate(R.menu.main, menu);
  127.         return true;
  128.     }
  129.     
  130. }
布局文件

点击(此处)折叠或打开

  1. RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     Button
  11.         android:id="@+id/findBtn"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:text="扫描已经连接的蓝牙"
  15.         />
  16.     Button
  17.         android:id="@+id/setBtn"
  18.         android:layout_width="fill_parent"
  19.         android:layout_height="wrap_content"
  20.         android:layout_below="@+id/findBtn"
  21.         android:text="设置蓝牙可见性"
  22.         />
  23.     Button
  24.         android:id="@+id/scanBtn"
  25.         android:layout_width="fill_parent"
  26.         android:layout_height="wrap_content"
  27.         android:layout_below="@+id/setBtn"
  28.         android:text="扫描周围可见的蓝牙"
  29.         />
  30.     EditText
  31.         android:id="@+id/edit1"
  32.         android:layout_width="fill_parent"
  33.         android:layout_height="wrap_content"
  34.         android:layout_below="@+id/scanBtn"
  35.         />

  36. /RelativeLayout>


上一篇:玩转CocoaPods


下一篇:使用MoinMoin搭建运维知识库[上文]