Android 蓝牙操作详解
1.启用蓝牙并使设备处于可发现状态
2.连接启用蓝牙设备
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
/**
* 读写蓝牙设备
*
* @author hbbliyong
*
*/
public class BluetoothConnecion extends Thread {
private final BluetoothSocket mSocket;
private final InputStream mInStream;
private final OutputStream mOutStream;
byte[] buffer;
private final BluetoothAdapter mAdapter;
// 用于本应用程序唯一的UUID,
private static final UUID MY_UUID = UUID
.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
public BluetoothConnecion(BluetoothDevice device) {
BluetoothSocket tmp = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();
// 获得用于指定蓝牙连接的BluetoothSocket
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (Exception e) {
e.printStackTrace();
}
mSocket = tmp;
// 在新线程中建立套接字连接,避免FC
Thread connectionThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 始终取消发现,因为它会降低连接的速度
mAdapter.cancelDiscovery();
// 建立到BluetoothSocket的连接
try {
// 这是一个阻塞调用,只在成功连接或者异常时返回
mSocket.connect();
} catch (Exception e) {
e.printStackTrace();
// 设备连接失败,关闭套接字
try {
mSocket.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
});
connectionThread.start();
InputStream tmpIn = null;
OutputStream tmpOut = null;
// 获得BluetoothSoket输入输出流
try {
tmpIn = mSocket.getInputStream();
tmpOut = mSocket.getOutputStream();
buffer = new byte[1024];
} catch (Exception e) {
e.printStackTrace();
}
mInStream = tmpIn;
mOutStream = tmpOut;
}
public void run() {
// 连接时保持监听InputStream
while (true) {
try {
// 从套接字流读取数据
mInStream.read(buffer);
// 向UI Activity发送获取的数据
} catch (Exception e) {
// TODO: handle exception
// 这里的异常标志着连接的丢失
// 向UI Activity发送获取的数据
break;
}
}
}
public void write(byte[] buffer)
{
try {
mOutStream.write(buffer);
} catch (Exception e) {
e.printStackTrace();
}
}
public void cancel()
{
try {
mSocket.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
3.监听和接收蓝牙连接请求
Android 蓝牙操作详解的更多相关文章
- android 数据库操作详解
请看郭大神的八篇专栏,包含sql语句 android封装的databasehelper 和郭大神自己的LitePal 三种使用详解 http://blog.csdn.net/column/deta ...
- Android 蓝牙API详解
随着近两年可穿戴式产品逐渐进入人们的生活,蓝牙开发也成为了Android开发的一个重要模块,下面我们就来说一说蓝牙的这些API. 1.蓝牙开发有两个主要的API: BuletoothAdapter:本 ...
- [Android新手区] SQLite 操作详解--SQL语法
该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法 :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...
- Android的file文件操作详解
Android的file文件操作详解 android的文件操作要有权限: 判断SD卡是否插入 Environment.getExternalStorageState().equals( android ...
- Android系统目录结构详解
Android系统基于linux内核.JAVA应用,算是一个小巧精致的系统.虽是开源,但不像Linux一般庞大,娇小可亲,于是国内厂商纷纷开发出自己基于Android的操作系统.在此呼吁各大厂商眼光放 ...
- android:ToolBar详解
android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
随机推荐
- MySQL远程连接失败,MySQL远程连接出现Using password:YES错误的解决办法
相信很多实用MYSQL的朋友都遇到过这种问题,就是MySQL使用localhost能够连接成功,但是使用IP连接却出现Using password:YES或者其它的连接错误.今天就把解决方法给大家说一 ...
- hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- Centos 7 GCC 7.3编译器安装方法及C++17标准测试示例
1.下载gcc-7.3.0源码 http://mirror.linux-ia64.org/gnu/gcc/releases/gcc-7.3.0/ 2.下载编译依赖 [root@localhost ~] ...
- CentOS 7 - 最小化安装以及引发的问题!
一,操作系统和虚拟机 操作系统:CentOS 7 官方网站:https://www.centos.org 下载地址:https://www.centos.org/download/ 下载版本分三个:D ...
- Topological Sor-207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- poj1125
题目大意:哎,意思看了半天,看了别人的解释才明白,就是说从一个经纪人出发传递消息,直到所有人都收到消息 也就是说只要找到该经纪人到其它所有点的最短距离中的最大一个时间,就说明最后一个也收到消息了. 而 ...
- ajax在php中应用实例
1,ajax分为$.ajax(),$.get(),$.post(),$.getJSON() 几种形式,实例如下: <html> <meta http-equiv="Cont ...
- centos7硬盘分区
首先在虚拟机的设置中为系统添加硬盘 使用fdisk -l /dev/sdb 查看未分区的硬盘 fdisk -l /dev/sda 这是已经分区好得 接下来我们就要对sdb进行分区: 首先使用fd ...
- POJ 1101
#include <iostream> #include <string> #define MAXN 78 #define min _min #define inf 12345 ...
- Oracle 常见字符操作
一.拼接 1.使用 || 来实现 SELECT '你'||'好!' title FROM dual; 2.使用concat (不支持多个字符串的拼接,但是可以嵌套调用) SELECT concat(' ...