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();
            }
        }
    }

发布了46 篇原创文章 · 获赞 8 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览