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终端 ...
随机推荐
- 深入浅出ExtJS 第七章 弹出窗口
7.1 Ext.MessageBox 7.1 Ext.MessageBox //Ext.MessageBox为我们提供的alert/confirm/prompt等完全可以代替浏览器原生; 7.1.1 ...
- Unity3d 引擎原理详细介绍、Unity3D引擎架构设计
体系结构 为了更好地理解游戏的软件架构和对象模型,它获得更好的外观仅有一名Unity3D的游戏引擎和编辑器是非常有用的,它的主要原则. Unity3D 引擎 Unity3D的是一个屡获殊荣的工具,用于 ...
- SQL Server2000安装
本篇文章介绍了安装SQL Server 2000各版本的软硬件配置要求,企业版安装过程的详细步骤,以及需要注意的事项.其他版本可以参考安装 注意:Windows XP不能装企业版.win2000\wi ...
- 清除windows的EFS加密
所使用软件为aefsdr_setup_en,搜索名为advanced.efs.data.recovery 1. 创建需要加密的文件 2. 进行加密 ...
- C#代码配置IIS 操纵IIS
前言: IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全问题的基础上获得的发展,目前为止.6.0版本以后的都是 ...
- php面向对象的特性:OOP的封装
字段的作用域: 1.public 公共的(类外可以访问) 2.private 私有的(只能类内访问) 3.protected 受保护的(类内和子类可以访问,类外无法访问) /*通过公共的方法来访问私有 ...
- 安装WordPress详细教程指南
最近准备自己建一个个人博客,以便分享一些自己工作生活中的一些观点及经验,建博客当然选wordpress,毕竟wordpress是为博客而生的嘛.下边记录一下自己安装WordPress的详细过程指南,亦 ...
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...
- Linux下C程序插入执行shell脚本
1.system(执行shell命令) 相关函数 fork,execve,waitpid,popen表头文件 #include<stdlib.h>定义函数 int system(const ...
- C#不同页面之间通信的方法
以前做项目的时候经常头疼两个页面之间的交互(汗),这几天看的MVVM项目,忽然感觉好简单的!我自己写了个简单的demo 可以简单实现2个页面之间的交互,新人第一次发博客,不喜勿喷 代码很简单,注释我就 ...