Giter Site home page Giter Site logo

bleserial's Introduction

蓝牙工具类:BLESPPUtils.java 直达链接

调用示例:MainActivity.java 直达链接

配套讲解文章:快速上手 Android 蓝牙串口 SPP 开发 直达链接

使用方式:

  • 1 加入权限并确认处理好运行时权限,已经授予了定位权限(用于扫描蓝牙设备)

    <!--管理蓝牙需要-->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!--搜索蓝牙需要,因为蓝牙可以被用来定位,所以需要定位权限-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  • 2 实例化工具类,第一个参数是 context,可以为任意 context,不涉及 UI 操作;第二个参数是「蓝牙活动回调」

    // 初始化
    mBLESPPUtils = new BLESPPUtils(MainActivity.this, new BLESPPUtils.OnBluetoothAction() {
        /**
         * 当发现新设备
         *
         * @param device 设备
         */
        @Override
        public void onFoundDevice(BluetoothDevice device) {
            
        }
    
        /**
         * 当连接成功
         *
         * @param device
         */
        @Override
        public void onConnectSuccess(BluetoothDevice device) {
    
        }
    
        /**
         * 当连接失败
         *
         * @param msg 失败信息
         */
        @Override
        public void onConnectFailed(String msg) {
    
        }
    
        /**
         * 当接收到 byte 数组
         *
         * @param bytes 内容
         */
        @Override
        public void onReceiveBytes(byte[] bytes) {
    
        }
    
        /**
         * 当调用接口发送了 byte 数组
         *
         * @param bytes 内容
         */
        @Override
        public void onSendBytes(byte[] bytes) {
    
        }
    
        /**
         * 当结束搜索设备
         */
        @Override
        public void onFinishFoundDevice() {
    
        }
    });
  • 3 设置接收停止标志位字符串

    mBLESPPUtils.setStopString("\r\n");
  • 4 启用工具类

    mBLESPPUtils.onCreate();
  • 5 在适当的时候销毁工具类,譬如 Activity 的 onDestroy()

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBLESPPUtils.onDestroy();
    }
  • 6 检查蓝牙开启情况

    // 用户没有开启蓝牙的话打开蓝牙
    if (!mBLESPPUtils.isBluetoothEnable()) mBLESPPUtils.enableBluetooth();
  • 7 搜索蓝牙设备,搜索结果在上面的回调里了

    mBLESPPUtils.startDiscovery();
  • 8 使用搜索到的 BluetoothDevice 连接设备

    mBLESPPUtils.connect(bluetoothDevice);
  • 9 使用之前保存的 MAC 地址连接

    mBLESPPUtils.connect("mac地址");
  • 10 发送 byte 数组到串口

    mBLESPPUtils.send("hello\r\n".getBytes());
  • 11 接收到串口发送的 byte 数组在上面第二步的onReceiveBytes(byte[] bytes)回调中

  • 12 启用工具类日志输出

    mBLESPPUtils.enableBluetooth();

bleserial's People

Contributors

gtf35 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.