android蓝牙操作一
来源:互联网 发布:手机数据恢复 编辑:程序博客网 时间:2024/05/21 15:54
一:什么是蓝牙 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地址建立通讯的通道进行文件的传输。
- <?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>
- 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("没有蓝牙设备");
- }
- }
- }
- }
- android蓝牙操作一
- Android 蓝牙编程学习一基本操作
- Android 蓝牙编程学习一基本操作
- Android Wifi和蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- android开发之 蓝牙使用(一)简单操作
- Android深入浅出系列之Bluetooth-蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android 蓝牙操作(待续)
- Android 蓝牙操作
- Android之蓝牙操作
- Android蓝牙操作
- Android蓝牙操作
- Android 3.0 http网络请求
- android蓝牙通讯
- 浮夸
- 情绪管理六堂课:认识负向情绪
- 音视频编码技术与格式大全
- android蓝牙操作一
- System.arraycopy的用法
- 视频尺寸相关知识
- freeswitch录音功能
- 较快的排序
- 蓝牙操作二
- Silverlight中Grid.RowDefinitions和Grid.Row
- [黑马程序员]post和get的区别
- OPENGL ES 常用API记录