转载一篇蓝牙开发的文章〜html
原博地址:http://www.cnblogs.com/menglin2010/archive/2011/11/02/2232923.htmljava
一:什么是蓝牙android
1:Bluetooth是目前使用最普遍的无线通信协议,近距离无线通信的标准。传说瑞典有个国王特别爱吃蓝莓致使本身的牙齿每天都是蓝色的,在他执政期间这位国王很是善于交际,能说会到,和邻国的搞得关系很是好,这个Bluetooth的发明者以为蓝牙它的做用就是在近距离沟通周围的设备,跟这个国王很相似,因而起名叫蓝牙。安全
2:主要针对短距离设备通信(10米)网络
3:无线耳机,无线鼠标,无线键盘app
二:蓝牙工做流程图eclipse
首先两个设备上都要有蓝牙设备或者专业一点叫蓝牙适配器,以手机和电脑为例我画了以下流程图。其次在手机上进行扫描,扫描周围蓝蓝牙设备,先找到手机附近的电脑,而后给它发出一个信号须要进行蓝牙的配对,再次返回一个信号说明手机和电脑已经配对成功了,最后配对成功后能够进行文件传输了。这是一个最基本的一个流程。布局
三:与蓝牙相关的最重要的两个APIpost
1:BuletoothAdapterurl
这个类的对象表明了本地的蓝牙适配器,至关于蓝牙工做流程图中的手机里的蓝牙适配器,也就是说好比这个应用程序是运行在手机上,那么手机上的蓝牙适配器就是本地蓝牙适配器。
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地址创建通信的通道进行文件的传输。
图十