Android Bluetooth Stream Non-blocking Communication Tutorial
This is a tutorial for Android to do non-blocking bluetooth socket communication. I am using 32feet Bluetooth library, but it should be the same if you were using other network socket communication that reply on inputstream mechanism.
In fact it is not asynchronous, however with a bit of threading magic, this system can work the way as asynchronous.
To move data from a background thread to the UI thread, use a Handler that’s running on the UI thread.
To get handler form a fragment, you could use this.getView().getHandler().
BluetoothStreamListener bsl = new BluetoothStreamListener(handler, socket, message);
Thread messageListener = new Thread(bsl);
messageListener.start();
messaging system
private class MessageEventListener implements Runnable {
private someUI ui;
private String message;
public MessageEventListener(someUI ui, String message) {
this.ui= ui;
this.message = message;
}
public void run() {
ui.passMsg(message);
}
}
stream listener
private class BluetoothStreamListener implements Runnable {
private BluetoothSocket socket;
private Handler handler;
private someUI ui = null;
public BluetoothStreamListener(BluetoothSocket socket, Handler handler, someUI ui) {
this.socket = socket;
this.handler = handler;
this.ui = ui;
}
public void run() {
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
try {
InputStream instream = socket.getInputStream();
while (true) {
if (instream.available() > 0) {
instream.read(buffer);
handler.post(new MessageEventListener(ui, buffer.toString()));
socket.getInputStream();
}
}
} catch (IOException e) {
Log.d("BluetoothStreamListener", e.getMessage());
}
}
}
Android Bluetooth Stream Non-blocking Communication Tutorial的更多相关文章
- android Bluetooth(官方翻译)
Bluetooth Using the Bluetooth APIs, an Android application can perform the following: 使用蓝牙APIs,一个And ...
- Android Bluetooth开发
原文地址:http://developer.android.com/guide/topics/wireless/bluetooth.html 翻译:jykenan 更新:2012.06.19 Andr ...
- 【转】Android bluetooth介绍(二): android blueZ蓝牙代码架构及其uart 到rfcomm流程
原文网址:http://blog.sina.com.cn/s/blog_602c72c50102uzoj.html 关键词:蓝牙blueZ UART HCI_UART H4 HCI L2CAP ...
- Android bluetooth介绍(四): a2dp connect流程分析
关键词:蓝牙blueZ A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...
- android Bluetooth程序设计
Bluetooth一个简短的引论 蓝牙,是一种短距离通信配套设备(一般10m中)无线技术. 包含移动电话.PDA.无线耳机.笔记本电脑.相关外设等众多设备之间进行无线信息交换.利用"蓝牙&q ...
- Android bluetooth介绍(两): android 蓝牙源架构和uart 至rfcomm过程
关键词:蓝牙blueZ UART HCI_UART H4 HCI L2CAP RFCOMM 版本号:基于android4.2先前版本 bluez内核:linux/linux3.08系统:an ...
- 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ A2DP.SINK.sink_connect.s ...
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...
随机推荐
- IE6的position:fixed
手头一个项目中,要实现把一个浮层控制在浏览器窗口右下角,用”position:fixed”来控制最合适不过了. 但万恶的IE6不支持这个属性,之前采用过的方法有:将滚动条转移到body上,使用绝对定位 ...
- iOS 高阶
1.UIStoryBoard 2. segue跳转传值 3. UIColor配色 //1. 十进制配色 [UIColor colorWithRed:163.0/255.0 green:148.0/25 ...
- C#.NET实现Word或Excel文件转为HTML文件
Word文件转html,返回相对路径 private string GetPathByDocToHTML(string strFile) { if (string.IsNullOrEmpty(strF ...
- Chrome系列 Failed to load resource: net::ERR_CACHE_MISS
在IE/FF下没有该错误提示,但在Chrome下命令行出现如下错误信息: Failed to load resource: net::ERR_CACHE_MISS 该问题是Chrome浏览器开发工具的 ...
- javascript笔记——jquery.each中使用continue和break的方式
jQuery.each中continue的方式是 return true break 的方式是return false
- JVM调优总结 -Xms -Xmx -Xmn -Xss(转载)
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- 正则匹配 去掉 多余的js和html标签
$reg17 = '/><strong>公司介绍<\/strong><\/td>([\S\s*]+?)<\/div>/'; $this->d ...
- win10任务视图
之所以用到win10任务视图源自于一个需求,就是我想在上班操作电脑的同时想将某个游戏在后台挂机,这样工作归工作,挂机归挂机,互不干扰.win10任务视图就能轻松的解决这个问题. 任务视图 新建任务视图 ...
- 前端自动化构建工具——gulp
gulp是基于流的前端自动化构建工具. 一.环境配置 gulp是基于nodejs的,所以没有 nodejs 环境的要先去安装好 然后给系统配上gulp环境 npm install -g gulp 再到 ...
- 关于查看Android系统源码【Written By KillerLegend】
可能你会想下载Android系统源码,但是我不知道你会看多少系统的源码,如果你对源码只是偶尔看一次的话,推荐你在线看Android的系统源码,下面提供几种查看android系统源码的方法. 1:打开这 ...