Android网络开发之蓝牙
蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的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网络开发之蓝牙的更多相关文章
- android网络开发之测试机连接到服务器上面
1:本人使用Tomcat作为服务器软件,首先打开Tomcat.(可以在浏览器中输入http://www.127.0.0.1:8080/查看) 2:服务器后台使用Servelt开发,这里不再讲解. 3: ...
- 《精通android网络开发》--HTTP数据通信
No1: 例如:http://www.*****.com/china/index.htm 1)http:// 代表超文本传送协议,通知*****.com服务器显示web页,通常不用输入 2)www 代 ...
- Android网络开发
1. WebView用法 ①布局文件新建一个WebView,特别注意线性布局和控件的宽高都要匹配父控件 <LinearLayout xmlns:android="http://sche ...
- Android网络开发之实时获取最新数据
在实际开发中更多的是需要我们实时获取最新数据,比如道路流量.实时天气信息等,这时就需要通过一个线程来控制视图的更新. 示例:我们首先创建一个网页来显示系统当前的时间,然后在Android程序中每隔5秒 ...
- Android网络开发实例(基于抓包实现的网络模拟登录,登出和强制登出)
学习Android有几个月了,最近喜欢上了网络编程,于是想通过Android写一些一个小程序用于连接外网.在这里非常感谢雪夜圣诞的支持,非常感谢,给我打开新的一扇门. 1.声明,本程序只能用于西南大学 ...
- Android网络开发之基本介绍
Android平台浏览器采用WebKit引擎,名为ChormeLite,拥有强大扩展特性,每个开发者都可以编写自己的插件. 目前,Android平台有3种网络接口可以使用,分别是:java.net, ...
- 《精通android网络开发》--使用Socket实现数据通信
No1: 网络传输应用通常使用TCP.IP或UDP这三种协议实现数据传输.在传输数据的过程中,需要通过一个双向的通信连接实现数据的交互.在这个传输过程中,通常将这个双向链路的一端称为Socket,一个 ...
- Android 网络开发免费API接口
http://www.juhe.cn/ 聚合数据 目前很多接口都收费 https://www.showapi.com ...
- Android开发之蓝牙--扫描已经配对的蓝牙设备
一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) 1.3 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
随机推荐
- iPhone跳转的动画效果类型及实现方法 CATransition
实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransi ...
- C#中的集合(HashTable与Array类)【转】
一.Array类 1.Array类的属性 序号 属性 & 描述 1 IsFixedSize 获取一个值,该值指示数组是否带有固定大小. 2 IsReadOnly 获取一个值,该值指示数组是否只 ...
- 使用DebugView小工具调试已部署的.net程序 (转)
DebugView for Windows能够捕捉Debug输出的信息在本地的操作系统上.如何你需要调试程序有网络访问推荐使用Wireshark和监听HTTP的工具Fiddler. 下载下来是一个ZI ...
- 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问
讲得详细清楚明白的好文. Setting Up an NFS Server and Client on CentOS 7.2 https://www.howtoforge.com/tutorial/s ...
- iOS开发-KVC和KVO的理解
KVC和KVO看起来很专业,其实用起来还是比较简单的,KVC(Key-value coding)可以理解为键值对编码,如果对象的基本类型,那么键值对编码实际上和get,set方法没有区别,如果是属性是 ...
- Ios开发之协议protocol
Protocol是ios开发中的一个难点也是一个重点,要想使用好,或者理解好它,可能需要时间的累积.今天我们就通过一个例子来简单的看一下,怎么样使用protocol. 我们今天用的例子就是模拟电脑插入 ...
- 【Spark】SparkStreaming-CPU资源设置的蹊跷
SparkStreaming-CPU资源设置的蹊跷. Spark streaming network_wordcount.py does not print result - Stack Overfl ...
- 如何基于TensorFlow使用LSTM和CNN实现时序分类任务
https://www.jiqizhixin.com/articles/2017-09-12-5 By 蒋思源2017年9月12日 09:54 时序数据经常出现在很多领域中,如金融.信号处理.语音识别 ...
- Topic Model的分类和设计原则
Topic Model的分类和设计原则 http://blog.csdn.net/xianlingmao/article/details/7065318 topic model的介绍性文章已经很多,在 ...
- w3cscholl的在线代码编辑工具2
https://www.w3cschool.cn/tryrun/runcode?lang=c