蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的2.4GHz ISM(I-工业、S-科学、M-医学)频段,其数据速率为1Mbps,采用时分双工传输方案。

 

蓝牙协议可分4层:

1. 核心协议层

2. 电缆替代协议层

3. 电话控制协议层

4. 其他协议层

 

蓝牙协议之核心协议层包括

1. 基带,

2. 链路管理, LMP, 负责蓝牙组件间连接的建立,

3. 逻辑链路控制和适应协议, L2CAP,

4. 业务搜寻协议, SDP, 通过SDP可以查询设备信息、业务及业务特征、并在查询之后建立两个或多个蓝牙设备间的连接。SDP支持3种查询方式:按业务类别搜索,按业务熟悉搜索,业务浏览。

 

蓝牙是android 2.0的包,在建立android工程时,一定要将API Level等级设置为5,即android 2.0。

蓝牙api位于android.bluetooth中,提供的常用功能有

1. BluetoothAdapter

2. BluetoothClass

3. BluetoothClass.Device

4. BluetoothClass.Device.Major

5. BluetoothClass.Service

6. BluetoothDevice

7. BluetoothServerSocket

8. BluetoothSocket

这些api允许:

app连接和断开蓝牙耳机、蓝牙打印机、蓝牙扫描仪等设备;

编写和修改本地服务的SDP协议数据库和查询其他蓝牙设备上的SDP协议数据库;

在android上建立RFCOMM协议的连接并连接到其他指定设备。

 

示例:学习蓝牙api的使用

 

// 在AndroidManifest.xml文件中声明等级和授权

<uses-sdk android:minSdkVersion=”5”/>

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

<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN” />

 

// 取得蓝牙适配器

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

// 打开蓝牙

bluetooth.enable();

// 关闭蓝牙

bluetooth.disable();

// 请求打开蓝牙常量

private static final int REQUEST_ENABLE = 0X1;

// 请求允许被搜索常量

private static final int REQUEST_DISCOVERABLE = 0X2;

// 请求打开蓝牙

Intent it = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(it, REQUEST_ENABLE);

// 请求能够被搜索

Intent it = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

startActivityForResult(it, REQUEST_DISCOVERABLE);

 

BluetoothAdaper中的ACTION常量

1. ACTION_DISCOVERY_FINISHED

2. ACTION_DISCOVERY_STARTED

3. ACTION_LOCAL_NAME_CHANGED

4. ACTION_REQUEST_DISCOVERABLE

5. ACTION_REQUEST_ENABLE

6. ACTION_SCAN_MODE_CHANGED

7. ACTION_STATE_CHANGED

BluetoothAdapter中的常用方法

1. cancelDiscovery

2. checkBluetoothAddress

3. disable

4. enable

5. getAddress

6. getDefalutAdpater

7. getName

8. getRemoteDevice

9. getScanMode

10. getState

11. isDiscovering

12. isEnabled

13. setName

14. startDiscovery

 

// 学习如何搜索网络上的设备并显示

// 搜索到一个指定地址的蓝牙设备BluetoothDevice

private BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

private List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();

private volatile boolean discoveryFinished;

// 搜索到一个蓝牙设备

BroadcastReceiver findReceiver = new BroadcastReceiver(

  public void onReceive(Context context, Intent intent) {

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

    devices.add(device);

    showDevices();

  }

// 搜索蓝牙设备工作结束

BroadcastReceiver finishDiscoveryReceiver = new BroadcastReceiver(

  public void onReceive(Context context, Intent intent) {

    unregisterReceiver(findReceiver);

    unregisterReceiver(this);

    discoveryFinished = true;

  }

);

在onCreate()方法中

// 注册Receiver

registerReceiver(finishDiscoveryReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

registerReceiver(findReceiver, new IntentFilter(BluetoothAdapter.ACTION_FOUND));

// 显示对话框 正在搜索蓝牙设备

SampleUtils.indeterminate(mContext, mHandler, “扫描中…”, mDiscoveryWorker, new OnDismissListener(){

  public void onDismiss(DialogInterface dialog) {

    if(bluetooth.isDiscovering()){

      bluetooth.cancelDiscovery();

    }

    discoveryFinished = true;

  }

});

// mDiscoveryWorker

Runnable mDiscoveryWorker = new Runnable(){

  public void run(){

    bluetooth.startDiscovery();

    while(!discoveryFinished){

      try{

        Thread.sleep(100);

      }catch(InterruptedException ex){ }

    }

  }

};

在showDevices()方法中

List<String> data = new ArrayList<String>();

for(int i=0;i<devices.size;i++){

  BluetoothDevice device = devices.get(i);

  String desc = “name:(”+device.getName()+”), address:(”+device.getAddress()+”)";

  data.add(desc);

}

本例的Activity继承ListActivity

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

mHanlder.post(new Runnable(){

  public void run(){

    setListAdapter(adapter);

  }

});

重写ListActivity的onListItemClick方法

protected void onListItemClick(ListView listView, View, view, int position, long id){

  Intent it = new Intent();

  it.putExtra(BluetoothDevice.EXTRA_DEVICE, devices.get(position));

  setResult(RESULT_OK, it);

}

Android网络开发之蓝牙的更多相关文章

  1. android网络开发之测试机连接到服务器上面

    1:本人使用Tomcat作为服务器软件,首先打开Tomcat.(可以在浏览器中输入http://www.127.0.0.1:8080/查看) 2:服务器后台使用Servelt开发,这里不再讲解. 3: ...

  2. 《精通android网络开发》--HTTP数据通信

    No1: 例如:http://www.*****.com/china/index.htm 1)http:// 代表超文本传送协议,通知*****.com服务器显示web页,通常不用输入 2)www 代 ...

  3. Android网络开发

    1. WebView用法 ①布局文件新建一个WebView,特别注意线性布局和控件的宽高都要匹配父控件 <LinearLayout xmlns:android="http://sche ...

  4. Android网络开发之实时获取最新数据

    在实际开发中更多的是需要我们实时获取最新数据,比如道路流量.实时天气信息等,这时就需要通过一个线程来控制视图的更新. 示例:我们首先创建一个网页来显示系统当前的时间,然后在Android程序中每隔5秒 ...

  5. Android网络开发实例(基于抓包实现的网络模拟登录,登出和强制登出)

    学习Android有几个月了,最近喜欢上了网络编程,于是想通过Android写一些一个小程序用于连接外网.在这里非常感谢雪夜圣诞的支持,非常感谢,给我打开新的一扇门. 1.声明,本程序只能用于西南大学 ...

  6. Android网络开发之基本介绍

    Android平台浏览器采用WebKit引擎,名为ChormeLite,拥有强大扩展特性,每个开发者都可以编写自己的插件. 目前,Android平台有3种网络接口可以使用,分别是:java.net, ...

  7. 《精通android网络开发》--使用Socket实现数据通信

    No1: 网络传输应用通常使用TCP.IP或UDP这三种协议实现数据传输.在传输数据的过程中,需要通过一个双向的通信连接实现数据的交互.在这个传输过程中,通常将这个双向链路的一端称为Socket,一个 ...

  8. Android 网络开发免费API接口

    http://www.juhe.cn/                              聚合数据              目前很多接口都收费 https://www.showapi.com ...

  9. Android开发之蓝牙--扫描已经配对的蓝牙设备

    一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等. 二. ...

随机推荐

  1. 使用mmap可以方便地添加共享内存

    使用mmap添加的共享内存. 局限: 只能在有亲属关系的进程之间使用. #include <stdio.h> #include <stdlib.h> #include < ...

  2. [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...

  3. Linq的延迟加载问题

    什么是延迟加载:所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作.可以简单理解为,只有在使用的时候,才会发出sql语句进行查询,数据是分N次读取. 什么是立即加载:所谓立即加载既是所有的 ...

  4. JS的scrollIntoView简单使用

    scrollIntoView方法滚动当前元素,进入浏览器的可见区域 el.scrollIntoView(); // 等同于el.scrollIntoView(true) el.scrollIntoVi ...

  5. Android -- startActivityForResult和setResult

    startActivityForResult与startActivity的不同之处 startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivit ...

  6. 【ElasticSearch】ElasticSearch-SQL插件

    ElasticSearch-SQL插件 image2017-10-27_11-10-53.png (1067×738) elastic SQL_百度搜索 Druid SQL 解析器的解析过程 - be ...

  7. Linux下C结构体初始化[总结]

    1.前言 今天在公司看一同事写的代码,代码中用到了struct,初始化一个struct用的是乱序格式,如下代码所示: typedef struct _data_t { int a; int b; }d ...

  8. 下载google play下的app

    1.打开appk downland网站 https://apps.evozi.com/apk-downloader/?id=com.cmcm.live 2. 把google play的链接直接贴入 输 ...

  9. iOS DES ECB 模式加密

    //iOS DES ECB 模式加密 #import <CommonCrypto/CommonCryptor.h> ,,,,,,,}; +(NSString *) encryptUseDE ...

  10. 微信小程序 - 动态背景图片实现

    很简单-就两步 wxml(遍历style的background-image路径即可) wxss(.ab)