Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能


前面几篇一直在讲一些基础,其实说实话,蓝牙主要为多的还是一些概念性的东西,当你把概念都熟悉了之后,你会很简单的就可以实现一些逻辑,主要是Socket和I/O流的操作,今天就来一起做一个聊天的小程序,我们都知道,我们实现蓝牙连接,蓝牙是有主从关系的,所以有客户端和服务端之分,我们新建一个工程——BLE_QQ(hh,毕竟是即时通讯嘛,和QQ挨个边)

参考Google的API:http://developer.android.com/guide/topics/connectivity/bluetooth.html

在开始之前,别忘了添加权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

一.OpenBluetooth

Google的API上说的是十分的清楚,我们作为初学者要把他当做说明书一样来看待

这样的话,我们就来实现打开蓝牙并且打开可见性,可见性默认是120s,MAX为3600s,这里在强调一遍,打开蓝牙有两种方式,一种是弹框提示,一种是强制打开,这在之前也是提过好多次了的

/**
     * 打开蓝牙并且搜索
     */
    public void openBluetooth(View v) {
        // 开启搜索
        Intent discoverableIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        // 设置可见性300s
        discoverableIntent.putExtra(
                BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);

        // 强制打开
        // mBluetoothAdapter.enable();
    }

CloseBluetooth

关闭蓝牙也就直接调用BluetoothAdapter的disable()方法;
    /**
     * 关闭蓝牙
     */
    public void closeBluetooth(View v) {
        mBluetoothAdapter.disable();
    }

客户端

我们在MainActivity中写一个button直接跳转到ClientActivity中去
    /**
     * 打开客户端
     */
    public void Client(View v) {
        startActivity(new Intent(this, ClientActivity.class));
    }

ClientActivity

package com.example.ble_qq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.SocketOptions;
import java.util.UUID;

import com.example.ble_qq.ServiceActivity.ReceiverInfoThread;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 客户端
 *
 * @author LGL
 *
 */
public class ClientActivity extends Activity {

    // 连接成功
    private static final int CONN_SUCCESS = 0x1;
    // 连接失败
    private static final int CONN_FAIL = 0x2;
    private static final int RECEIVER_INFO = 0x3;
    // 设置文本框为空
    private static final int SET_EDITTEXT_NULL = 0x4;
    // 接收到的消息
    private TextView tv_content;
    // 输入框
    private EditText et_info;

    // 发送按钮
    private Button btn_send;

    // 本地蓝牙适配器
    private BluetoothAdapter mBluetoothAdapter = null;

    // 远程设备
    private BluetoothDevice device = null;

    // 蓝牙设备Socket客户端
    private BluetoothSocket socket = null;

    private boolean isReceiver = true;

    // 设备名称
    private static final String NAME = "LGL";

    // 输入输出流
    private PrintStream out;
    private BufferedReader in;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setTitle("客户端");
        setContentView(R.layout.activity_client);

        initView();
        // 初始化Socket客户端连接
        init();

    }

    private void initView() {
        // 初始化
        tv_content = (TextView) findViewById(R.id.tv_content);
        et_info = (EditText) findViewById(R.id.et_info);
        btn_send = (Button) findViewById(R.id.btn_send);
    }

    private void init() {
        tv_content.setText("客户端已经启动,正在与服务端连接...\n");
        // 开始连接
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    // 得到本地蓝牙适配器
                    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    // 通过本地适配器得到地址,这个地址可以公共扫描来获取,就是getAddress()返回的地址
                    device = mBluetoothAdapter
                            .getRemoteDevice("98:6C:F5:CE:0E:81");
                    // 根据UUID返回一个socket,要与服务器的UUID一致
                    socket = device.createRfcommSocketToServiceRecord(UUID
                            .fromString("00000000-2527-eef3-ffff-ffffe3160865"));
                    if (socket != null) {
                        // 连接
                        socket.connect();
                        // 处理流
                        out = new PrintStream(socket.getOutputStream());

                        in = new BufferedReader(new InputStreamReader(socket
                                .getInputStream()));
                    }
                    // 连接成功发送handler
                    handler.sendEmptyMessage(CONN_SUCCESS);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Message mes = handler.obtainMessage(CONN_FAIL,
                            e.getLocalizedMessage());
                    handler.sendMessage(mes);
                }

            }
        }).start();
    }

    /**
     * Handler接收消息
     *
     */
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case CONN_SUCCESS:
                setInfo("连接成功! \n");
                btn_send.setEnabled(true);
                Log.i("设备的名称", device.getName());
                Log.i("设备的UUID", device.getUuids() + "");
                Log.i("设备的地址", device.getAddress());
                // 开始接收信息
                new Thread(new ReceiverInfoThread()).start();
                break;
            case CONN_FAIL:
                setInfo("连接失败! \n");
                setInfo(msg.obj.toString() + "\n");
                break;
            case RECEIVER_INFO:
                setInfo(msg.obj.toString() + "\n");
                break;
            case SET_EDITTEXT_NULL:
                et_info.setText("");
                break;

            }
        }
    };

    /**
     * 接收消息的线程
     */
    class ReceiverInfoThread implements Runnable {

        @Override
        public void run() {
            String info = null;
            while (isReceiver) {
                try {
                    info = in.readLine();
                    Message msg = handler.obtainMessage(RECEIVER_INFO);
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 发送消息
     */
    public void SendText(View v) {
        final String text = et_info.getText().toString();
        // 不能为空
        if (!TextUtils.isEmpty(text)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
        }
        new Thread(new Runnable() {

            @Override
            public void run() {
                // 输出
                out.println(text);
                out.flush();
                // 把文本框设置为空
                handler.sendEmptyMessage(SET_EDITTEXT_NULL);
            }
        }).start();
    }

    /**
     * 拼接文本信息
     */
    private void setInfo(String info) {
        StringBuffer sb = new StringBuffer();
        sb.append(tv_content.getText());
        sb.append(info);
        tv_content.setText(sb);
    }
}

服务端

我们在MainActivity中写一个button直接跳转到ServiceActivity中去
    /**
     * 打开服务端
     */
    public void Service(View v) {
        startActivity(new Intent(this, ServiceActivity.class));
    }

ServiceActivity

package com.example.ble_qq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 服务端
 *
 * @author LGL
 *
 */
public class ServiceActivity extends Activity {

    // 连接成功
    private static final int CONN_SUCCESS = 0x1;
    // 连接失败
    private static final int CONN_FAIL = 0x2;
    private static final int RECEIVER_INFO = 0x3;
    // 设置文本框为空
    private static final int SET_EDITTEXT_NULL = 0x4;
    // 接收到的消息
    private TextView tv_content;
    // 输入框
    private EditText et_info;

    // 发送按钮
    private Button btn_send;

    // 本地蓝牙适配器
    private BluetoothAdapter mBluetoothAdapter = null;

    // 蓝牙设备Socket服务端
    private BluetoothServerSocket serviceSocket = null;

    // 蓝牙设备Socket客户端
    private BluetoothSocket socket = null;

    // 设备名称‘
    private static final String NAME = "LGL";

    private boolean isReceiver = true;

    // 输入输出流
    private PrintStream out;
    private BufferedReader in;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setTitle("服务端");
        setContentView(R.layout.activity_service);

        initView();
        // 创建蓝牙服务端的Socket
        initService();
    }

    private void initView() {
        // 初始化
        tv_content = (TextView) findViewById(R.id.tv_content);
        et_info = (EditText) findViewById(R.id.et_info);
        btn_send = (Button) findViewById(R.id.btn_send);

    }

    private void initService() {
        tv_content.setText("服务器已经启动,正在等待设备连接...\n");
        // 开启线程操作
        new Thread(new Runnable() {

            @Override
            public void run() {
                // 得到本地适配器
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                // 创建蓝牙Socket服务端
                try {
                    // 服务端地址
                    serviceSocket = mBluetoothAdapter
                            .listenUsingInsecureRfcommWithServiceRecord(
                                    NAME,
                                    UUID.fromString("00000000-2527-eef3-ffff-ffffe3160865"));
                    // 阻塞线程等待连接
                    socket = serviceSocket.accept();
                    if (socket != null) {
                        // I/O流
                        out = new PrintStream(socket.getOutputStream());

                        in = new BufferedReader(new InputStreamReader(socket
                                .getInputStream()));
                    }
                    // 连接成功发送handler
                    handler.sendEmptyMessage(CONN_SUCCESS);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Message mes = handler.obtainMessage(CONN_FAIL,
                            e.getLocalizedMessage());
                    handler.sendMessage(mes);
                }

            }
        }).start();
    }

    /**
     * Handler接收消息
     *
     */
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case CONN_SUCCESS:
                setInfo("连接成功! \n");
                btn_send.setEnabled(true);
                new Thread(new ReceiverInfoThread()).start();
                break;
            case CONN_FAIL:
                setInfo("连接失败! \n");
                setInfo(msg.obj.toString() + "\n");
                break;
            case RECEIVER_INFO:
                setInfo(msg.obj.toString() + "\n");
                break;
            case SET_EDITTEXT_NULL:
                et_info.setText("");
                break;

            }
        }
    };

    /**
     * 接收消息的线程
     */
    class ReceiverInfoThread implements Runnable {

        @Override
        public void run() {
            String info = null;
            while (isReceiver) {
                try {
                    info = in.readLine();
                    Message msg = handler.obtainMessage(RECEIVER_INFO);
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 发送消息
     */
    public void SendText(View v) {
        final String text = et_info.getText().toString();
        // 不能为空
        if (!TextUtils.isEmpty(text)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
        }
        new Thread(new Runnable() {

            @Override
            public void run() {
                // 输出
                out.println(text);
                out.flush();
                // 把文本框设置为空
                handler.sendEmptyMessage(SET_EDITTEXT_NULL);
            }
        }).start();
    }

    /**
     * 拼接文本信息
     */
    private void setInfo(String info) {
        StringBuffer sb = new StringBuffer();
        sb.append(tv_content.getText());
        sb.append(info);
        tv_content.setText(sb);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/et_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="SendText"
        android:text="发送" />

</LinearLayout>

实际上运行结果是这样的,我们必须准备两部手机,然后先用蓝牙配对,在进行连接

这样我们两个设备就配对成功了

Demo下载地址:http://download.csdn.net/detail/qq_26787115/9420355

Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能的更多相关文章

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

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

  2. Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探

    Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...

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

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

  4. Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信

    Android BLE与终端通信(三)--客户端与服务端通信过程以及实现数据通信 前面的终究只是小知识点,上不了台面,也只能算是起到一个科普的作用,而同步到实际的开发上去,今天就来延续前两篇实现蓝牙主 ...

  5. Android BLE与终端通信(三)——client与服务端通信过程以及实现数据通信

    Android BLE与终端通信(三)--client与服务端通信过程以及实现数据通信 前面的终究仅仅是小知识点.上不了台面,也仅仅能算是起到一个科普的作用.而同步到实际的开发上去,今天就来延续前两篇 ...

  6. Android WebSocket实现即时通讯功能

    最近做这个功能,分享一下.即时通讯(Instant Messaging)最重要的毫无疑问就是即时,不能有明显的延迟,要实现IM的功能其实并不难,目前有很多第三方,比如极光的JMessage,都比较容易 ...

  7. Linux网络编程--多线程实现echo服务器与客户端“一对多”功能,是网络编程的“Hello World!”

    在linux平台下,用多线程实现echo服务器与客户端“一对多”(即是一台服务器可以响应多个客户端的请求).本人写了个demo,和大家一起分享,有不足的地方,请多多指教,我是壮壮熊. 编译时,在后面加 ...

  8. XMPP环境搭建 (mac环境下 安装自己独立的mysql与服务器(openfire),实现即时聊天功能)

    1简单概览 [一]XMPP简介 http://xmpp.org 即时通讯技术 IM - Instant Messaging ⽀支持⽤用户在线实时交谈.交谈双⽅方都需要⼀一个聊天窗⼝口,其中⼀一个⽤用户 ...

  9. Android BLE设备蓝牙通信框架BluetoothKit

    BluetoothKit是一款功能强大的Android蓝牙通信框架,支持低功耗蓝牙设备的连接通信.蓝牙广播扫描及Beacon解析. 关于该项目的详细文档请关注:https://github.com/d ...

随机推荐

  1. 【伯乐在线】Java线程面试题 Top 50

    本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特 ...

  2. Android插件化的思考——仿QQ一键换肤,思考比实现更重要!

    Android插件化的思考--仿QQ一键换肤,思考比实现更重要! 今天群友希望写一个关于插件的Blog,思来想去,插件也不是很懂,只是用大致的思路看看能不能模拟一个,思路还是比较重要的,如果你有兴趣的 ...

  3. x264源代码简单分析:x264_slice_write()

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. GDAL 2.0版本RPC校正速度测试

    GDAL2.0版本的更新日志中提到了对RPC校正的优化,今天测试了一下,发现提升的速度还是蛮快的,测试的数据是一个IRS-P5的数据. 单线程测试 首先使用一个线程进行测试,使用下面的批处理进行运行, ...

  5. 第一行代码阅读笔记---AndroidMainfest.xml分析

    按照这本书的指引,我随作者一样创建了一个安卓应用,开始了安卓开发的启程. 找到AndroidMainfest.xml这个文件,打开后看到了我创建的Activity在这个文件里被成功注册,文件内容如下: ...

  6. UNIX网络编程——epoll的 et,lt关注点

           epoll模型有两种工作模式,ET和LT两种模式下都有一些细节值得注意,以下是一些思考:   一.ET模式下 Q1:调用accept时,到底TCP完成队列里有多少个已经建立好的连接? 这 ...

  7. Java基础---Java---IO流-----对象的序列化、管道流、RandomAccessFile、数据类型的流对象DataStream、ByteArrayStream

    ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化. ObjectOutputStream 和 ObjectInputStream ...

  8. javascript 下拉列表 自动取值 无需value

    <select id="applyType" name="$!{status.expression}" class="inp" onc ...

  9. Java-IO之BufferedWriter(字符缓冲输出流)

    BufferedWriter是字符缓冲输出流,继承于Writer,作用是为其他字符输出流添加一些缓冲功能. BufferedWriter主要的函数列表: BufferedWriter(Writer o ...

  10. hive支持in用法是从0.3.2版本后

    写hive 用in 如分时段,分类型,分平台统计点击量 select substr(createtime,12,2) hour,logtype,os_id,count(*)  from  wizad_ ...