当前位置:   article > 正文

Android蓝牙工具类:连接、配对、传输_手机蓝牙工具

手机蓝牙工具

ChangeTool 是一个工具类,详见: https://blog.csdn.net/qq_30297763/article/details/93873467

/**
 * Created by XingAijian
 * Date: 2019/6/12_17:23
 * <p>
 * 蓝牙工具类
 */
public class BTUtils extends Activity {

    private final static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";   //SPP服务UUID号

    public static BluetoothDevice _device = null;     //蓝牙设备
    public static BluetoothSocket _socket = null;      //蓝牙通信socket
    public static BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();    //获取本地蓝牙适配器,即蓝牙设备
    public static List<BluetoothDevice> bluetoothDeviceList = new ArrayList<>();
    public static InputStream is;    //输入流,用来接收蓝牙数据

    public static int fingerprintFunction = 0;
    public static btSendCallBack btSendCallBack = null;
    public static OutputStream os = null;
    public static  boolean bThread = false;
    public static String linkBTMac = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

 /**
     * 打开蓝牙
     */
    public static void startBth() {
        //如果打开本地蓝牙设备不成功,提示信息,结束程序
        if (_bluetooth == null) {
            Toast.makeText(MyApplication.myApplication, "无法打开手机蓝牙,请确认手机是否有蓝牙功能!", Toast.LENGTH_LONG).show();
            return;
        }

        // 设置设备可以被搜索
        new Thread() {
            public void run() {
                /*隐式打开蓝牙*/
                if (_bluetooth.isEnabled() == false) {
                    _bluetooth.enable();
                }
            }
        }.start();
    }

    /**
     * 连接
     * address :mac 地址
     */
    public static void linkBT(String address, btDataCallBack btDataCallBack) {

        Log.e("linkBT", "address" + address);
        _device = _bluetooth.getRemoteDevice(address);

        try {
            _socket = _device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
        } catch (IOException e) {
            Toast.makeText(MyApplication.myApplication, "连接失败!", Toast.LENGTH_SHORT).show();
        }
        //连接socket
        try {
            _socket.connect();
            Toast.makeText(MyApplication.myApplication, "连接" + _device.getName() + "成功!", Toast.LENGTH_SHORT).show();
            linkBTMac = address;
            btDataCallBack.doCallBack(true);
        } catch (IOException e) {
            Toast.makeText(MyApplication.myApplication, "连接失败!", Toast.LENGTH_SHORT).show();
            return;
        }
    }
    /**
     * 通过蓝牙传输信息
     */
    public static boolean sendBT( byte[] content ) {
        Log.d("getRead", "sendBT: "+ content);
        try {
            if (os == null){
                os = _socket.getOutputStream();   //蓝牙连接输出流
            }
            os.write(content);
            Log.d("getRead", "sendBT2: "+ content);
            return true;
        } catch (IOException e) {
            return false;
        }

    }

    /**
     * 读取蓝牙信息
     */
    public static void getRead( final btSendCallBack btSendCallBack1) {
        btSendCallBack = btSendCallBack1;
        try {
            is = _socket.getInputStream();   //得到蓝牙数据输入流
            Log.d("getRead", "得到蓝牙数据输入流 ");
        } catch (IOException e) {
            Toast.makeText(MyApplication.myApplication, "接收数据失败!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (bThread == false ) {
            bThread = true;
            readBuffer.start();
        }
    }
    static Thread readBuffer = new Thread(){
        @Override
        public void run() {
            super.run();
            byte[] buffer = new byte[128];
            Log.d("getRead12", "bThread: "+ bThread);
            while (bThread) {
                try {
                    is.read(buffer); //写入buffer,现在buffer就是我们取到的值了
                    Log.d("getRead12", "is: "+ ChangeTool.ByteArrToHex(buffer)); 
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    //连接回调
    public interface btDataCallBack {
        void doCallBack(boolean btData);
    }

    //发数据接受回调
    public interface btSendCallBack {
        void doCallBack(int btData,int fpId);
    }

    /**
     * 关闭蓝牙相关
     */
    public static void closeBt(){
        Log.d("BTUtils", "closeBt: ");
        bThread = false;
        if (_socket != null) {
            //关闭连接socket
            try {
                Thread.sleep(2000);

                if (is !=null){
                    is.close();
                }
                _socket.close();
                _socket = null;
            } catch (IOException e) {
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/192235
推荐阅读
相关标签
  

闽ICP备14008679号