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的更多相关文章

  1. android Bluetooth(官方翻译)

    Bluetooth Using the Bluetooth APIs, an Android application can perform the following: 使用蓝牙APIs,一个And ...

  2. Android Bluetooth开发

    原文地址:http://developer.android.com/guide/topics/wireless/bluetooth.html 翻译:jykenan 更新:2012.06.19 Andr ...

  3. 【转】Android bluetooth介绍(二): android blueZ蓝牙代码架构及其uart 到rfcomm流程

    原文网址:http://blog.sina.com.cn/s/blog_602c72c50102uzoj.html 关键词:蓝牙blueZ  UART  HCI_UART H4  HCI  L2CAP ...

  4. Android bluetooth介绍(四): a2dp connect流程分析

    关键词:蓝牙blueZ  A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...

  5. android Bluetooth程序设计

    Bluetooth一个简短的引论 蓝牙,是一种短距离通信配套设备(一般10m中)无线技术. 包含移动电话.PDA.无线耳机.笔记本电脑.相关外设等众多设备之间进行无线信息交换.利用"蓝牙&q ...

  6. Android bluetooth介绍(两): android 蓝牙源架构和uart 至rfcomm过程

    关键词:蓝牙blueZ  UART  HCI_UART H4  HCI  L2CAP RFCOMM  版本号:基于android4.2先前版本 bluez内核:linux/linux3.08系统:an ...

  7. 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ  A2DP.SINK.sink_connect.s ...

  8. Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表

    Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...

  9. Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

    Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...

随机推荐

  1. [转]美国的软件公司是什么样?---- 以Fog Creek为例

    作者: 阮一峰 日期: 2009年2月15日 Joel写过一篇文章,关于软件公司如何雇到优秀的程序员. 他说,软件公司的头等大事,就是找到第一流的人才.不同于其他行业,软件是一种纯智力产品,你有什么样 ...

  2. MYSQL多实例配置与使用教程

    原文http://www.111cn.net/database/mysql/58651.htm 在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 ...

  3. 命令行启动tomcat,怎么配置

    进和你tomcat的安装目录进入里面bin目录下列可以直接在cmd中运行(要进入tomcat的bin目录),也可直接双击startup.bat 启动tomcatshutdown.bat 关闭tomca ...

  4. spring3+struts2+hibernate3整合出现的问题,No mapping found for dependency [type=java.lang.String, name='struts.objectFactory.spring.enableAopSupport']

    七月 11, 2016 3:49:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...

  5. (转)RabbitMQ 安装和监控

    在Windows上安装Rabbit MQ 指南,最好的是这篇<Rabbit MQ Windows Installation guide>,其中还包括了使用.NET RabbitMQ.Cli ...

  6. [译]开始学习webpack

    写在前面: 文章翻译自petehunt大神的petehunt/webpack-howto,作为学习webpack的开始.fork了一份,翻译后的在这里https://github.com/zjzhom ...

  7. 8款效果惊艳的HTML5 3D动画

    1.HTML5 WebGL水面水波荡漾特效 之前已经向各位分享过一款很逼真的HTML5水波荡漾特效,效果还算不错.今天再向大家分享一款更加给力的HTML5水波动画,画面上是一个大水池,水池底部是一颗大 ...

  8. java大数--总结

    BigInteger(高精度整数) 1.所在包: java.math.BigInteger 2.大数运算,以下返回类型均为BigInteger BigInteger a; BigInteger b; ...

  9. DataGrid3

    a标签,DataGrid的数据绑定 1.function aa(id, url) {            //alert(id);            window.open(url + '&am ...

  10. [windows phone开发]新生助手的开发过程与体会二

    上一讲咱们谈了新生助手主页的基本的设计,今天我们谈一谈关于展现实景地图时等动画的设计,即Storyboard的应用. 在Windows phone中,Storyboard类表示通过时间线控制动画,并为 ...