参考:http://www.cnblogs.com/shuaiwen/archive/2013/07/18/3198385.html

http://www.yiibai.com/android/android_bluetooth.html

http://blog.csdn.net/centralperk/article/details/8080908

http://blog.csdn.net/xubin341719/article/details/40393285

在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据。 Android平台包含了蓝牙框架,使设备以无线方式与其他蓝牙设备进行数据交换的支持。

Android提供蓝牙API来执行这些不同的操作。


  1. 扫描其他蓝牙设备



  2. 获取配对设备列表



  3. 连接到通过服务发现其他设备


Android提供BluetoothAdapter类蓝牙通信。通过调用创建的对象的静态方法getDefaultAdapter()。其语法如下给出。

private BluetoothAdapter BA;BA = BluetoothAdapter.getDefaultAdapter();

为了使用设备的蓝牙,调用下列蓝牙ACTION_REQUEST_ENABLE的意图。其语法如下:

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(turnOn, 0);

除了这个常量,有提供其它的API,支持不同任务的其他常数。它们在下面列出。

Sr.No 常数说明
1 ACTION_REQUEST_DISCOVERABLE
此常数用于开启蓝牙的发现
2 ACTION_STATE_CHANGED
此常量将通知蓝牙状态已经改变
3 ACTION_FOUND
此常数用于接收关于所发现的每个设备的信息

启用了蓝牙功能之后,可以通过调用 getBondedDevices()方法来获取配对设备列表。它返回一组的蓝牙设备。其语法如下:

private SetpairedDevices;pairedDevices = BA.getBondedDevices();

除了配对的设备,还有API,让更多蓝牙控制权等方法。它们在下面列出。


Sr.No 方法及说明
1 enable()
这种方法使适配器,如果未启用
2 isEnabled()
如果适配器已启用此方法返回true
3 disable()
该方法禁用适配器
4 getName()
此方法返回的蓝牙适配器的名称
5 setName(String name)
此方法更改蓝牙名称
6 getState()
此方法返回蓝牙适配器的当前状态
7 startDiscovery()
此方法开始蓝牙120秒的发现过程。

2、蓝牙的搜索和连接





Android对于蓝牙开发从2.0版本的sdk才开始支持,而且模拟器不支持,测试至少需要两部手机,所以制约了很多技术人员的开发。

      1.  首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限

  // 管理蓝牙设备的权限  

 // 使用蓝牙设备的权限  

2.打开蓝牙
获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。


  1. // 检查设备是否支持蓝牙      

  2. adapter = BluetoothAdapter.getDefaultAdapter();     

  3. if (adapter == null)     

  4. {     

  5.     // 设备不支持蓝牙      

  6. }     

  7. // 打开蓝牙      

  8. if (!adapter.isEnabled())     

  9. {     

  10.     Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);     

  11.     // 设置蓝牙可见性,最多300秒      

  12.     intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);     

  13.     context.startActivity(intent);     

  14. }    

3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice)
首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。


  1. BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();     

  2. Set devices = adapter.getBondedDevices();     

  3. for(int i=0; i

  4. {     

  5.     BluetoothDevice device = (BluetoothDevice) devices.iterator().next();     

  6.    System.out.println(device.getName());     

  7. }    

4.搜索周围的蓝牙设备
适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。


  1. // 设置广播信息过滤      

  2. IntentFilter intentFilter = new IntentFilter();     

  3. intentFilter.addAction(BluetoothDevice.ACTION_FOUND);     

  4. intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);     

  5. intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);     

  6. intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);     

  7. // 注册广播接收器,接收并处理搜索结果      

  8. context.registerReceiver(receiver, intentFilter);     

  9. // 寻找蓝牙设备,android会将查找到的设备以广播形式发出去      

  10. adapter.startDiscovery();    

自定义广播类

[java] view plain copy

  1. private BroadcastReceiver receiver = new BroadcastReceiver() {     

  2.    @Override     

  3.   public void onReceive(Context context, Intent intent) {     

  4.        String action = intent.getAction();     

  5.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {     

  6.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);     

  7.             System.out.println(device.getName());     

  8.        }     

  9.    }     

  10. }  

5.蓝牙设备的配对和状态监视

[java] view plain copy

  1. private BroadcastReceiver receiver = new BroadcastReceiver() {     

  2.     @Override     

  3.     public void onReceive(Context context, Intent intent) {     

  4.         String action = intent.getAction();     

  5.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {     

  6.             // 获取查找到的蓝牙设备      

  7.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);     

  8.             System.out.println(device.getName());     

  9.             // 如果查找到的设备符合要连接的设备,处理      

  10.             if (device.getName().equalsIgnoreCase(name)) {     

  11.                 // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索      

  12.                 adapter.cancelDiscovery();     

  13.                 // 获取蓝牙设备的连接状态      

  14.                 connectState = device.getBondState();     

  15.                 switch (connectState) {     

  16.                     // 未配对      

  17.                     case BluetoothDevice.BOND_NONE:     

  18.                         // 配对      

  19.                         try {     

  20.                             Method createBondMethod = BluetoothDevice.class.getMethod("createBond");     

  21.                             createBondMethod.invoke(device);     

  22.                         } catch (Exception e) {      

  23.                             e.printStackTrace();     

  24.                         }     

  25.                         break;     

  26.                     // 已配对      

  27.                     case BluetoothDevice.BOND_BONDED:     

  28.                         try {     

  29.                             // 连接      

  30.                            connect(device);     

  31.                         } catch (IOException e) {     

  32.                             e.printStackTrace();     

  33.                         }     

  34.                         break;     

  35.                 }     

  36.             }     

  37.        } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {     

  38.             // 状态改变的广播      

  39.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);     

  40.             if (device.getName().equalsIgnoreCase(name)) {      

  41.                 connectState = device.getBondState();     

  42.                 switch (connectState) {     

  43.                     case BluetoothDevice.BOND_NONE:     

  44.                         break;     

  45.                     case BluetoothDevice.BOND_BONDING:     

  46.                         break;     

  47.                     case BluetoothDevice.BOND_BONDED:     

  48.                         try {     

  49.                             // 连接      

  50.                             connect(device);     

  51.                         } catch (IOException e) {     

  52.                             e.printStackTrace();     

  53.                         }     

  54.                         break;     

  55.                 }     

  56.             }     

  57.         }     

  58.     }     

  59. }    

6.蓝牙设备的连接

[java] view plain copy

  1. private void connect(BluetoothDevice device) throws IOException {     

  2.     // 固定的UUID      

  3.     final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";     

  4.     UUID uuid = UUID.fromString(SPP_UUID);     

  5.     BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);     

  6.     socket.connect();     

  7. }    

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-12/48374.htm

 

1.BluetoothAdapter 顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它

      BluetoothAdapter里的方法很多,常用的有以下几个:

      cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索

      disable()关闭蓝牙

      enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:

Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);//同startActivity(enabler);

      getAddress()获取本地蓝牙地址

      getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter

      getName()获取本地蓝牙名称

      getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备

      getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)

      isDiscovering()判断当前是否正在查找设备,是返回true

      isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

     listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步

      startDiscovery()开始搜索,这是搜索的第一步

    2.BluetoothDevice看名字就知道,这个类描述了一个蓝牙设备

       createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket


       这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket


       这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter

    3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,


这个类一种只有三个方法


两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!


还有一点需要注意的是,这两个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接

      close()这个就不用说了吧,翻译一下——关闭!

    4.BluetoothSocket,跟BluetoothServerSocket相对,是客户端


一共5个方法,不出意外,都会用到

      close(),关闭

      connect()连接

      getInptuStream()获取输入流

      getOutputStream()获取输出流

      getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备

2、蓝牙的连接

在做android蓝牙串口连接的时候一般会使用

  BluetoothSocket tmp = null;
            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
              tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
           } catch (IOException e) {
                Log.e(TAG, "create() failed", e);
          }

然后是tmp赋给BluetoothSocket,接着调用connect方法进行蓝牙设备的连接。

可是 BluetoothSocket 的connect方法本身就会报很多异常错误。

以下根据对蓝牙开发的一点研究可通过以下方法解决:

方法1.先进行蓝牙自动配对,配对成功,通过UUID获得BluetoothSocket,然后执行connect()方法。

方法2.通过UUID获得BluetoothSocket,然后先根据mDevice.getBondState()进行判断是否需要配对,最后执行connnect()方法。

  

    /**
     * 

     * 蓝牙连接线程      * 

     *       * @author lsw      *       */     private class ConnectThread extends Thread {         String macAddress = "";        public ConnectThread(String mac) {             macAddress = mac;         }        public void run() {             connecting = true;             connected = false;            if(mBluetoothAdapter == null){                 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();             }             mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress);             mBluetoothAdapter.cancelDiscovery();            //initSocket();             try {                 socket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);                              } catch (IOException e) {                // TODO Auto-generated catch block                //e.printStackTrace();                 Log.e(TAG, "Socket", e);             }                          //adapter.cancelDiscovery();             while (!connected && connetTime <= 10) {                                 connectDevice();             }            // 重置ConnectThread              //synchronized (BluetoothService.this) {               //ConnectThread = null;            //}        }        public void cancel() {            try {                 socket.close();                 socket = null;             } catch (Exception e) {                 e.printStackTrace();             } finally {                 connecting = false;             }         }     }

  接下来是调用的连接设备方法connectDevice():

  

    protected void connectDevice() {  
        try {  
            // 连接建立之前的先配对  
            if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {  
                Method creMethod = BluetoothDevice.class  
                        .getMethod("createBond");  
                Log.e("TAG", "开始配对");  
                creMethod.invoke(mBluetoothDevice);  
            } else {  
            }  
        } catch (Exception e) {  
            // TODO: handle exception  
            //DisplayMessage("无法配对!");              e.printStackTrace();  
        }  
        mBluetoothAdapter.cancelDiscovery();  
        try {  
            socket.connect();  
            //DisplayMessage("连接成功!"); 
            //connetTime++;
            connected = true;
        } catch (IOException e) {  
            // TODO: handle exception  
            //DisplayMessage("连接失败!");
            connetTime++;
            connected = false;            try {  
                socket.close();
                socket = null;
            } catch (IOException e2) {  
                // TODO: handle exception  
                Log.e(TAG, "Cannot close connection when connection failed");  
            }  
        } finally {
            connecting = false;
        }  
    }

 

方法3.利用反射通过端口获得BluetoothSocket,然后执行connect()方法。

  

    /**
     * 

     * 蓝牙连接线程      * 

     *       * @author lsw      *       */     private class ConnectThread extends Thread {         String macAddress = "";        public ConnectThread(String mac) {             macAddress = mac;         }        public void run() {             connecting = true;             connected = false;            if(mBluetoothAdapter == null){                 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();             }             mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress);             mBluetoothAdapter.cancelDiscovery();             initSocket();                                      //adapter.cancelDiscovery();             while (!connected && connetTime <= 10) {                try {                     socket.connect();                     connected = true;                 } catch (IOException e1) {                     connetTime++;                     connected = false;                    // 关闭 socket                     try {                         socket.close();                         socket = null;                     } catch (IOException e2) {                        //TODO: handle exception                           Log.e(TAG, "Socket", e2);                     }                 } finally {                     connecting = false;                 }                //connectDevice();            }            // 重置ConnectThread              //synchronized (BluetoothService.this) {               //ConnectThread = null;            //}        }        public void cancel() {            try {                 socket.close();                 socket = null;             } catch (Exception e) {                 e.printStackTrace();             } finally {                 connecting = false;             }         }     }

  接下来是初始化并得到BluetoothSocket的方法

  

    /**
     * 取得BluetoothSocket     */
    private void initSocket() {
        BluetoothSocket temp = null;        try {            
            Method m = mBluetoothDevice.getClass().getMethod(                    "createRfcommSocket", new Class[] { int.class });
            temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);//这里端口为1            
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        socket = temp;
    }

 

要点:1.蓝牙配对和连接是两回事,不可混为一谈。

   2.蓝牙串口连接可通过端口 (1-30)和UUID两种方法进行操作。

   3.通过UUID进行蓝牙连接最好先进行配对操作。