Android 蓝牙通信

1,关于蓝牙

随着这几年的发展现在越来越多的场景会用到蓝牙技术,最近非常火的共享单车都提供了打开蓝牙快速解锁的功能。它最为近场通信的一种,我们很有必要对它进行下研究和探索。

2,使用蓝牙

 
上图是手机设置中蓝牙界面。
1,当打开蓝牙时会在通知栏中有蓝牙图表展示
2,加载按钮,点击后取搜寻周围蓝牙设备。
3,打开关闭蓝牙的togglebutton
4,开发检测,关闭后周围蓝牙设备将不会搜索到本设备。
5,蓝牙的名称,一般和手机的名字一样可以自定义设置任意值。
6,接收到的文件,顾名思义我们可以通过蓝牙通信进行交换的文件例如发送一个mp3文件,在这里可以看到这个文件。
7,已配对设备:这里展示的是和本地蓝牙进行过配对的蓝牙设备,在这里通过列表的格式展示出配对设备列表的名字。
8,可用设备:已发现的蓝牙设备的列表,打开蓝牙后,点击2按钮会去搜索周围蓝牙设备,并且将搜索到的设备列表添加到这里。

注:可用设备和已配对设备的关系是,我们搜索到的设备可以进行配对,可能需要输入配对码,配对成功后将该设备加入到已配对的列表中,已配对的设备中,我们可以取消配对,将其从已配对列表中移除。

3,相关类

在Android 中关于蓝牙的类都在android.bluetooth包中,下面对几个常用的类进行概述。

BluetoothAdapter:表示本地的蓝牙适配器,bluetoothAdapter控制着执行蓝牙的基本任务,例如:搜索周围蓝牙设备,获取配对列表,通过一个已知的MAC地址(每个蓝牙设备都有一个唯一的Mac地址)构建BluetoothDevice对象, 并创建一个BluetoothServerSocket从其他设备监听连接请求,并开始一个扫描蓝牙设备。

BluetoothDevice:蓝牙设备类,代表一个远程蓝牙设备。BluetoothDevice允许您创建一个连接与各自的设备或查询相关信息,如蓝牙名字、MAC地址、class和绑定的状态(配对与否)等。

BluetoothServerSocket  :这个类的用法非常像java网络编程中ServerSocket,用于监听Bluetooth Socket类。

BluetoothSocket:用法类似于Socket。一个连接上或连接中的Bluetooth Socket类。

4,连接通信过程

蓝牙的版本不一样通信的方式也有所不同,我们将分析两种不同的连接方式:classic类型蓝牙,Ble蓝牙。
classic类型蓝牙通信过程如下

1,•设置权限:

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

2,打开蓝牙:
  • 创建蓝牙适配器对象
BluetoothAdapter BluetoothAdapter.getDefaultAdapter(),如果返回为空说明此Android设备不支持蓝牙(或者蓝牙模块已经损坏)。
如果你运行在 JELLY_BEAN_MR1以及以下的版本时,直接使用BluetoothAdapter.getDefaultAdapter()创建BluetoothAdapter。
如果你运行在 JELLY_BEAN_MR2以及以上的版本时使用如下方法构建:
BluetoothManager manager = getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter adapter = manager.getAdapter();
使用BluetoothManager类来帮助构建得到BluetoothAdapter。
  • 打开蓝牙
可以直接强制打开蓝牙:
adapter.enable(),需要设置好权限 BLUETOOTH_ADMIN 
或者使用系统弹窗请求打开蓝牙:
	 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
	 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CONNECT_DEVICE_SECURE:
                // When DeviceListActivity returns with a device to connect
                if (resultCode == Activity.RESULT_OK) {
                    connectDevice(data, true);
                }
                break;
            case REQUEST_CONNECT_DEVICE_INSECURE:
                // When DeviceListActivity returns with a device to connect
                if (resultCode == Activity.RESULT_OK) {
                    connectDevice(data, false);
                }
                break;
            case REQUEST_ENABLE_BT:
                // When the request to enable Bluetooth returns
                if (resultCode == Activity.RESULT_OK) {
                    // Bluetooth is now enabled, so set up a chat session
                    setupChat();
                } else {
                    // User did not enable Bluetooth or an error occurred
                    Log.d(TAG, "BT not enabled");
                    Toast.makeText(getActivity(), R.string.bt_not_enabled_leaving,
                            Toast.LENGTH_SHORT).show();
                    getActivity().finish();
                }
        }
    }


注:如果是原生Android系统以上两种方式就是我所说的效果,但是Android系统一般手机厂商定制的比较严重,我们即使使用强制打开蓝牙的 这种方式也有可能需要弹窗,点击允许即可。


3,搜索蓝牙设备:

  • 搜索配对连接
这里其实是分两种情况,一是去连接已经配对过的设备,二是搜索设备,配对,连接。
adapter.startDiscovery(),开始搜索周围的蓝牙设备。搜索到设备将会以广播的形式将设备发送,我们创建广播接收者来接收搜索到的设备即可进行下一步操作。广播创建代码如下:
public class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //扫描到了任一蓝牙设备
            if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
            {
                Log.v(TAG, "### BT BluetoothDevice.ACTION_FOUND ##");
                BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		if(adapter.isDiscovering())//扫描比较耗费性能,如果搜索到需要的设备,立即关闭扫描。
		   adapter.cancelDiscovery();
                if(btDevice != null){
                    Log.v(TAG , "Name : " + btDevice.getName() + " Address: " + btDevice.getAddress());
                }
            }
        }
    }
//注册
MyReceiver receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver,filter);
//最后在Activity结束时记得销毁广播接收者
unregisterReceiver(receiver);




接下来我们在ACTION_FOUND中的到了BluetoothDevice,使用它进行连接,如果该设备之前没有配对,会弹出配对对话框,点击配对或者输入配对码配对后执行之 后的连接过程代码:

连接的两端分为服务端和客户端。
服务端使用BluetoothServerSocket进行监听,客户端使用BluetoothSocket进行连接。

服务端:
/**
     * This thread runs while listening for incoming connections. It behaves
     * like a server-side client. It runs until a connection is accepted
     * (or until cancelled).
     */
    private class AcceptThread extends Thread {
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;
        private String mSocketType;
        public AcceptThread(boolean secure) {//这里的参数就是为了创建不同的监听服务,注意两个不同的UUID值
            BluetoothServerSocket tmp = null;
            mSocketType = secure ? "Secure" : "Insecure";
            // Create a new listening server socket
            try {
                if (secure) {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                            MY_UUID_SECURE);
                } else {
                    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                            NAME_INSECURE, MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
            }
            mmServerSocket = tmp;
            mState = STATE_LISTEN;
        }
        public void run() {
            Log.d(TAG, "Socket Type: " + mSocketType +
                    "BEGIN mAcceptThread" + this);
            setName("AcceptThread" + mSocketType);
            BluetoothSocket socket = null;
            // Listen to the server socket if we're not connected
            while (mState != STATE_CONNECTED) {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    socket = mmServerSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
                    break;
                }
                // If a connection was accepted
                if (socket != null) {
                    synchronized (BluetoothChatService.this) {
                        switch (mState) {
                            case STATE_LISTEN:
                            case STATE_CONNECTING:
                                // Situation normal. Start the connected thread.
                                connected(socket, socket.getRemoteDevice(),
                                        mSocketType);
                                break;
                            case STATE_NONE:
                            case STATE_CONNECTED:
                                // Either not ready or already connected. Terminate new socket.
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    Log.e(TAG, "Could not close unwanted socket", e);
                                }
                                break;
                        }
                    }
                }
            }
            Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
        }
        public void cancel() {
            Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
            }
        }
    }
客户端:
/**
     * This thread runs while attempting to make an outgoing connection
     * with a device. It runs straight through; the connection either
     * succeeds or fails.
     */
    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;
        private String mSocketType;
        public ConnectThread(BluetoothDevice device, boolean secure) {//如果需要创建安全的BluetoothScoket第二个参数传true
            mmDevice = device;
            BluetoothSocket tmp = null;
            mSocketType = secure ? "Secure" : "Insecure";
            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
                if (secure) {
                    tmp = device.createRfcommSocketToServiceRecord(
                            MY_UUID_SECURE);
                } else {
                    tmp = device.createInsecureRfcommSocketToServiceRecord(
                            MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
            }
            mmSocket = tmp;
            mState = STATE_CONNECTING;
        }
        public void run() {
            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
            setName("ConnectThread" + mSocketType);
            // Always cancel discovery because it will slow down a connection
            mAdapter.cancelDiscovery();
            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() " + mSocketType +
                            " socket during connection failure", e2);
                }
                connectionFailed();
                return;
            }
            // Reset the ConnectThread because we're done
            synchronized (BluetoothChatService.this) {
                mConnectThread = null;
            }
            // Start the connected thread
            connected(mmSocket, mmDevice, mSocketType);
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
            }
        }
    }

操作类(客户端和服务端连接上之后进行通讯的类)
/**
     * This thread runs during a connection with a remote device.
     * It handles all incoming and outgoing transmissions.
     */
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        public ConnectedThread(BluetoothSocket socket, String socketType) {
            Log.d(TAG, "create ConnectedThread: " + socketType);
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
            mState = STATE_CONNECTED;
        }
        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;
            // Keep listening to the InputStream while connected
            while (mState == STATE_CONNECTED) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }
        /**
         * Write to the connected OutStream.
         *
         * @param buffer The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }


以上涉及到三个类分别是AcceptThread,ConnectThread,ConnectedThread类,代码有点多但是都比较简单易懂,都是一些类似于Socket通信时的操作。AcceptThread中的accept(),ConnectThread中的connect(),ConnectedThread中的read()这些方法都会阻塞主线程的运行所有这几个都是线程类。

两个方法:

服务端BluetoothServiceSocket创建:

if (secure) {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                            MY_UUID_SECURE);
                } else {
                    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                            NAME_INSECURE, MY_UUID_INSECURE);
                }
第一个参数:相当于你个你的监听器起的名字
第二个参数:类似于TCP连接时的端口号

两种方式创建监听服务,客户端要和服务端建立连接,类似于TCP连接时的端口对应一样,BluetoothSocket要和BluetoothServerSocket必须两者的UUID值相同。MY_UUID_SECURE和MY_UUID_INSECURE两个UUID是为了区分是否建立安全的Rfcomm通道。

客户端BluetoothSocket创建:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);

两个方法都可以创建出BluetoothSocket对象,在cretaeRfcommSocketToServiceRecord方法创建的tmp对象连接之前如果设备没有配对会有提示配对的对话框出现,这也是安全性的一个体现吧。网上许多文章提到的如何取消配对话框,其实使用createInsecureRfcommSocketToServiceRecord这个方法创建就会免去配对的过程了。但是免去了配对过程又掌握你的UUID的话就可以直接搜索到你的设备后直接建立连接发送数据了,有一定的安全问题。

UUID:蓝牙设备连接需要的UUID的格式是:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID值和TCP的端口号一样,也存在着一些标准的UUID默认值:例如,将蓝牙模拟成串口的服务就使用了一个标准的UUID:

00001101-0000-1000-8000-00805F9B34FB

除此之外,还有很多标准的UUID,如下面就是两个标准的UUID:

信息同步服务:00001104-0000-1000-8000-00805F9B34FB
文件传输服务:00001106-0000-1000-8000-00805F9B34FB


连接建立后获取输入输出流对象:

tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();

使用IO操作进行服务端和客户端的通信过程。

下面附上官方demo的github地址:

https://github.com/googlesamples/android-BluetoothChat

如果觉得还可以请添加我们的公众号:






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值