zoukankan      html  css  js  c++  java
  • 关于蓝牙开发

    转载一篇蓝牙开发的文章〜

    原博地址:http://www.cnblogs.com/menglin2010/archive/2011/11/02/2232923.html

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

     

      一:什么是蓝牙

        1:Bluetooth是目前使用最广泛的无线通讯协议,近距离无线通讯的标准。传说瑞典有个国王特别爱吃蓝莓导致自己的牙齿天天都是蓝色的,在他执政期间这位国王非常善于交际,能说会到,和邻国的搞得关系非常好,这个Bluetooth的发明者觉得蓝牙它的作用就是在近距离沟通周围的设备,跟这个国王很类似,于是起名叫蓝牙。

        2:主要针对短距离设备通讯(10米)

        3:无线耳机,无线鼠标,无线键盘

        

    蓝牙标志

      二:蓝牙工作流程图

        首先两个设备上都要有蓝牙设备或者专业一点叫蓝牙适配器,以手机和电脑为例我画了如下流程图。其次在手机上进行扫描,扫描周围蓝蓝牙设备,先找到手机附近的电脑,然后给它发出一个信号需要进行蓝牙的配对,再次返回一个信号说明手机和电脑已经配对成功了,最后配对成功后可以进行文件传输了。这是一个最基本的一个流程。

      三:与蓝牙相关的最重要的两个API

        1:BuletoothAdapter

        这个类的对象代表了本地的蓝牙适配器,相当于蓝牙工作流程图中的手机里的蓝牙适配器,也就是说比如这个应用程序是运行在手机上,那么手机上的蓝牙适配器就是本地蓝牙适配器。

        2:BuletoothDevice

        这个类的对象代表了远程的蓝牙设备,相当于蓝牙工作流程图中的计算机里的蓝牙适配器,也就是说比如这个应用程序是运行在手机上,那么BuletoothDevice代表了你要连接的远程的那个设备上面的蓝牙适配器。

      四:硬件准备

        今天这个示例必须运行在具有安卓2.0SDK以上的手机上面,不能运行在模拟器上面,因为现在的模拟器是不能模拟蓝牙的,所以必须有个安卓的手机,另外要有台具有蓝牙适配器的电脑。手机和电脑来进行配对,只能通过手动来进行,不可能通过代码是实现配对,因为安全性的问题不能通过应用程序自动的来进行配对,一旦配对成功就可以进行文件的传输了。如何配对在这里就不讲解了。

         五:如何蓝牙配对

               本来是要拿手机和电脑作为调试的,但是我的电脑上面没有蓝牙适配器,所以就用蓝牙笔代替了。

               1:插入手机

                如果发现没有驱动系统会提示安装驱动

               

               2 :下载豌豆荚

               豌豆荚会自动安装手机对应型号的USB驱动,USB调试默认是打开的(一定要开启手机的USB调试),等待安装完成。

             

              3 :打开在eclipse的DDMS视图里的Devices这一区域出现了你的手机设备的数字名称了。

              

             4:打开手机上的“设置”

            

          5:选择“无线和网络”

              给蓝牙打上勾,此时手机头部的蓝牙小图标已打开,表示开启了蓝牙

             

              

          6:扫描配对

             拿起蓝牙笔,打开它的开关,点击手机上面的“扫描查找设备”

            

        7:请求配对       

           输入密钥请求配对,然后等待配对成功

           

           

       六:实现效果

              扫描已配对的远程蓝牙设备

              代码步骤

        1:需要在AndroidMainfest.xml里声明蓝牙权限

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

        2:获得BluetoothAdapter对象

        3:判断当前设备中是否拥有蓝牙设备

        4:判断当前设备中的蓝牙设备是否已经打开

        5:得到所有已经配对的蓝牙设备对象

      七:代码

        1:布局文件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/btn2"
        />
      </LinearLayout>

      2:代码文件MainActivity.java

      package com.szy.bluetooth;

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

        public class MainActivity extends Activity {
          private Button mybutton = null;
          public void onCreate(Bundle savedInstanceState) 
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              //得到按钮
              mybutton = (Button)findViewById(R.id.btn2);
              //绑定监听器
              mybutton.setOnClickListener(new ButtonListener());
         }
      

      //监听器匿名类
         private class ButtonListener implements OnClickListener
         {
            public void onClick(View v)
          {
             //得到BluetoothAdapter对象
             BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
             //判断BluetoothAdapter对象是否为空,如果为空,则表明本机没有蓝牙设备
             if(adapter != null)
             {
                System.out.println("本机拥有蓝牙设备");
                //调用isEnabled()方法判断当前蓝牙设备是否可用
                if(!adapter.isEnabled())
                {     
                   //如果蓝牙设备不可用的话,创建一个intent对象,该对象用于启动一个Activity,提示用户启动蓝牙适配器
                   Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                   startActivity(intent);
                }
                //得到所有已经配对的蓝牙适配器对象
                Set<BluetoothDevice> devices = adapter.getBondedDevices();
                if(devices.size()>0)    
                {
                   //用迭代
                   for(Iterator iterator = devices.iterator();iterator.hasNext();)
                   {
                      //得到BluetoothDevice对象,也就是说得到配对的蓝牙适配器
                      BluetoothDevice device = (BluetoothDevice)iterator.next();
                      //得到远程蓝牙设备的地址
                      Log.d("mytag",device.getAddress());

                   }     
              }
           }
           else
           {
              System.out.println("没有蓝牙设备");
           }
         }           
          }
      }

        八:不连接安卓手机效果图

        因为找不到蓝牙设备所以会在DDMS视图下的系统信息里输出“没有蓝牙设备”。

     

        九:调试效果图

            我们得到了蓝牙笔的蓝牙适配器的地址,我们接下来用MAC地址建立通讯的通道进行文件的传输。

         

    图十

  • 相关阅读:
    java中&和&&是怎么运算的
    struts中ActionForward 使用mapping.findForward如何传递get参数
    EL表达式_详解
    JSTL标签_详解
    inner join, left join, right join, full join 的区别
    CentOS7部署FastDFS+nginx模块
    一个实例明白AutoResetEvent和 ManulResetEvent的用法
    C#防止在画面上闪烁的Button
    C#中给Label控件设置BackgroundImage属性
    浅析C#异步操作
  • 原文地址:https://www.cnblogs.com/howarddeng/p/5462045.html
Copyright © 2011-2022 走看看