蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的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. CF 329B(Biridian Forest-贪心-非二分)

    B. Biridian Forest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Qt解决:Qobject::connect queue arguments of type ‘xxxx’,Make sure ‘xxxx’ is registered using qRegister

    解决方法:在调用connect之前,通过 qRegisterMetaType() 注册你connect函数里对象的类型代码如下: typedef QString CustomString;//你自己定 ...

  3. fastText、TextCNN、TextRNN……这里有一套NLP文本分类深度学习方法库供你选择

    https://mp.weixin.qq.com/s/_xILvfEMx3URcB-5C8vfTw 这个库的目的是探索用深度学习进行NLP文本分类的方法. 它具有文本分类的各种基准模型,还支持多标签分 ...

  4. linux里tmpfs文件系统

    linux里tmpfs文件系统 是一个虚拟内存文件系统,它不同于传统的用块设备形式来实现的Ramdisk,也不同于针对物理内存的Ramfs.Tmpfs可以使用物理内存,也可以使用交换分区. umoun ...

  5. WDCP安装可选组件的快捷命令

    memcache的安装 wget -c http://down.wdlinux.cn/in/memcached_ins.sh chmod 755 memcached_ins.sh ./memcache ...

  6. chrome插件编写基本入门

    chrome插件编写基本入门  http://igeekbar.com/igeekbar/post/331.htm #精选JAVASCRIPTCHROME 作为一名程序猿,怎么能不会写chrome插件 ...

  7. Install SVN (Subversion) Server on Fedora 20/19, CentOS/Red Hat (RHEL) 6.5/5.10

    Install SVN (Subversion) Server on Fedora 20/19, CentOS/Red Hat (RHEL) 6.5/5.10 Updated by JR on Mar ...

  8. linux curl工具

    http://www.linuxidc.com/Linux/2008-01/10891.htm http://www.cnblogs.com/-clq/archive/2012/01/29/23308 ...

  9. Lintcode: Add Binary

    C++ class Solution { public: /** * @param a a number * @param b a number * @return the result */ str ...

  10. 使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法

    一.实体书写 将枚举类型的属性前面加上[JsonConverter(typeof(StringEnumConverter))]即可. 二.举例 [JsonConverter(typeof(String ...