Android蓝牙通信

来源:互联网 发布:软件项目范围管理 编辑:程序博客网 时间:2024/06/03 13:35

Android蓝牙通信

1、简介

    蓝牙(Bluetooth)是目前比较流行的一种短距离无线通讯技术,蓝牙技术是为了实现以无线电波替换移动设备所使 用的电缆而产生的。它试图以相同成本和安全性完成一般电缆的功能,从而使移动用户摆脱电缆束缚,蓝牙技术具备成本低、功耗低、体积小、近距离通信、安全性等技术特性。“蓝牙”技术的目的是使特定的移动电话、便携式电脑以及各种便携式通信设备的主机之间在近距离内实现无缝的资源共享。

    本次实习通过编写基于Android的移动端应用程序,了解不同设备蓝牙之间的发现、连接、绑定以及通信,旨在通过课上所学的通信技术,掌握好蓝牙通信技术的基本原理。

2、设计方案

    所写程序的类图如图所示:DeviceListActivityBluetoothChat同时继承于ActivityDeviceListActivity是在搜索蓝牙时显示所搜到的蓝牙以及包括以配对蓝牙信息的界面,BluetoothChat是主类,它包括蓝牙的开启、搜索配对、发送消息等一系列的活动。右侧、ConnectThreadConnectedThreadAcceptThread分别表示进行连接的线程类、已经连接的线程类、接受连接的线程类,他们的各种情况分别在BluetoothChatService类里面,BluetoothChatService又与主类相关联。


3、步骤描述

        1、首先通过调用静态方法getDefaultAdapter()获取蓝牙适配器BluetoothAdapter,以后你就可以使用该对象了。

   2、其次,调用isEnabled()来查询当前蓝牙设备的状态,如果返回为false,则表示蓝牙设备没有开启,接下来你需要封装ACTION_REQUEST_ENABLE请求到intent里面,调用startActivityF

OrResult()方法使能蓝牙设备,

   3、使用BluetoothAdapter类里的方法,你可以查找远端设备(大概十米以内)或者查询在你手机上已经匹配(或者说绑定)的其他手机了。当然需要确定对方蓝牙设备已经开启或者已经开启了“被发现使能”功能(对方设备是可以被发现的是你能够发起连接的前提条件)。如果该设备是可以被发现的,会反馈回来一些对方的设备信息,比如名字、MAC地址等,利用这些信息,你的设备就可以选择去向对方初始化一个连接。 

    如果你是第一次与该设备连接,那么一个配对的请求就会自动的显示给用户。当设备配对好之后,他的一些基本信息(主要是名字和MAC)被保存下来并可以使用蓝牙的API来读取。使用已知的MAC地址就可以对远端的蓝牙设备发起连接请求。 

匹配好的设备和连接上的设备的不同点:匹配好只是说明对方设备发现了你的存在,并拥有一个共同的识别码,并且可以连接。连接上:表示当前设备共享一个RFCOMM信道并且两者之间可以交换数据。也就是是说蓝牙设备在建立RFCOMM信道之前,必须是已经配对好了的。

   4、在建立连接之前你必须先查询配对好了的蓝牙设备集(你周围的蓝牙设备可能不止一个),以便你选取哪一个设备进行通信,例如你可以你可以查询所有配对的蓝牙设备,并使用一个数组适配器将其打印显示出来(建立一个蓝牙连接只需要MAC地址就已经足够了)。

   5、扫描设备,只需要简单的调用系统方法,这个扫描的过程大概持续是12秒,应用程序为了ACTION_FOUND动作需要注册一个BroadcastReceiver来接受设备扫描到的信息。对于每一个设备,系统都会广播ACTION_FOUND动作。

   6、连接设备,在应用程序中,想建立两个蓝牙设备之间的连接,必须实现客户端和服务器端的代码(因为任何一个设备都必须可以作为服务端或者客户端)。一个开启服务来监听,一个发起连接请求(使用服务器端设备的MAC地址)。当他们都拥有一个蓝牙套接字在同一RFECOMM信道上的时候,可以认为他们之间已经连接上了。服务端和客户端通过不同的方式或其他们的蓝牙套接字。当一个连接监听到的时候,服务端获取到蓝牙套接字。当客户可打开一个FRCOMM信道给服务器端的时候,客户端获取到蓝牙套接字。

7、数据传输:当设备连接上以后,每个设备都拥有各自的BluetoothSocket。现在你就可以实现设备之间数据的共享了。通过调用getInputStream()getOutputStream()方法来获取输入输出流。然后通过调用read(byte[])write(byte[]).方法来读取或者写数据。

4、程序核心代码

进行连接:

<span style="font-family:FangSong_GB2312;font-size:18px;"> public synchronized void connect(BluetoothDevice device) {        if (D) Log.d(TAG, "connect to: " + device);        if (mState == STATE_CONNECTING) {            if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}        }        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}        mConnectThread = new ConnectThread(device);        mConnectThread.start();        setState(STATE_CONNECTING);}  public void run() {            Log.i(TAG, "BEGIN mConnectThread");            setName("ConnectThread");            mAdapter.cancelDiscovery();            try {                mmSocket.connect();            } catch (IOException e) {                connectionFailed();                try {                    mmSocket.close();                } catch (IOException e2) {                    Log.e(TAG, "unable to close() socket during connection failure", e2);                }                BluetoothChatService.this.start();                return;            }            synchronized (BluetoothChatService.this) {                mConnectThread = null;            }            connected(mmSocket, mmDevice);        }</span>

接受连接:

<span style="font-family:FangSong_GB2312;font-size:18px;">public void run() {            if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);            setName("AcceptThread");            BluetoothSocket socket = null;                        while (mState != STATE_CONNECTED) {                try {                    socket = mmServerSocket.accept();                } catch (IOException e) {                    Log.e(TAG, "accept() failed", e);                    break;                }                if (socket != null) {                    synchronized (BluetoothChatService.this) {                        switch (mState) {                        case STATE_LISTEN:                        case STATE_CONNECTING:                            connected(socket, socket.getRemoteDevice());                            break;                        case STATE_NONE:                        case STATE_CONNECTED:                                                        try {                                socket.close();                            } catch (IOException e) {                                Log.e(TAG, "Could not close unwanted socket", e);                            }                            break;                        }                    }                }            }            if (D) Log.i(TAG, "END mAcceptThread");        }</span>

发送消息:

<span style="font-family:FangSong_GB2312;font-size:18px;">private void sendMessage(String message) {if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();return;}if (message.length() > 0) {byte[] send = message.getBytes();mChatService.write(send);mOutStringBuffer.setLength(0);mOutEditText.setText(mOutStringBuffer);}}</span>

处理消息:

<span style="font-family:FangSong_GB2312;font-size:18px;">private final Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MESSAGE_STATE_CHANGE:if (D)Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);switch (msg.arg1) {case BluetoothChatService.STATE_CONNECTED:mTitle.setText(R.string.title_connected_to);mTitle.append(mConnectedDeviceName);mConversationArrayAdapter.clear();break;case BluetoothChatService.STATE_CONNECTING:mTitle.setText(R.string.title_connecting);break;case BluetoothChatService.STATE_LISTEN: case BluetoothChatService.STATE_NONE:mTitle.setText(R.string.title_not_connected);break;}break;case MESSAGE_WRITE:byte[] writeBuf = (byte[]) msg.obj;String writeMessage = new String(writeBuf);mConversationArrayAdapter.add("Me:  " + writeMessage);break;case MESSAGE_READ:byte[] readBuf = (byte[]) msg.obj;String readMessage = new String(readBuf, 0, msg.arg1);mConversationArrayAdapter.add(mConnectedDeviceName + ":  "+ readMessage);break;case MESSAGE_DEVICE_NAME:mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);Toast.makeText(getApplicationContext(),"Connected to " + mConnectedDeviceName,Toast.LENGTH_SHORT).show();break;case MESSAGE_TOAST:Toast.makeText(getApplicationContext(),msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();break;}}};</span>


1 0
原创粉丝点击