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. AjaxPro.2使用小结

    这是我最近没事的时候研究的东东,使用AjaxPro.2.dll,从前台调用后台Ajax方法,希望对各位亲有帮助哦.. 1.首先将AjaxPro.2.dll从网上下载下来,打开VS项目,点击项目,右键- ...

  2. 常见的Activity Action Intent常量

    Intent的中文意思是“意图,目的”的意思,可以理解为不同组件之间通信的“媒介”或者“信使”. 目标组件一般要通过Intent来声明自己的条件,一般通过组件中的<intent-filter&g ...

  3. IntentFilterDemo

    intent-filter示例: <activity android:name=".CustomActivity" android:label="@string/t ...

  4. sql调用方法精简

    <%If IsArray(proList) Then%> <UL class="product-ul"> <%For x=0 to Ubound(pr ...

  5. iOS数据持久化-SQLite数据库使用详解

    使用SQLite数据库 创建数据库 创建数据库过程需要3个步骤: 1.使用sqlite3_open函数打开数据库: 2.使用sqlite3_exec函数执行Create Table语句,创建数据库表: ...

  6. UI1_UIScrollView

    // // AppDelegate.m // UI1_UIScrollView // // Created by zhangxueming on 15/7/10. // Copyright (c) 2 ...

  7. WCF之服务元数据

    服务元数据是用来获得服务的EndPoint的信息,也就是它的ABC. 服务有两种方案可以发布自己的元数据. 一种是基于HTTP-GET协议提供元数据: 一种是元数据交换方式,它往往使用一个专门的终结点 ...

  8. 20140213-想念是while里的死循环

    // stdafx.h : 标准系统包含文件的包含文件,// 或是经常使用但不常更改的// 特定于项目的包含文件// #pragma once#include "targetver.h&qu ...

  9. linux命令后台运行[转]

    有两种方式:    1. command & : 后台运行,你关掉终端会停止运行    2. nohup command & : 后台运行,你关掉终端也会继续运行 一. 简介     ...

  10. web应用中webapp. root重用问题解决方案

      同一个tomcat服务器里面部署两个JavaEE项目,都是用了log4j做日志.并且web.xml里面都监听了日志信息. 启动服务的时候报错. 于是在web.xml添加以下代码:   <di ...