ANDROID经典蓝牙通讯

最近写了一个关于蓝牙的操作类,对蓝牙不熟悉的同学可以参考一下 功能包括 发现,搜索,连接,发送,接收等/ 直接上代码:

package com.***.****.BlueToothM;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.***.***.utils.LogUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * Created by bysd-2 on 2018/10/19.
 */

public class BlueToothHelper {
    public static final int REQUEST_BLUE_ENABLE = 110;
    private static Context mContext;
    private static BluetoothAdapter bluetoothAdapter;
    private static String UUID_STR = "00001101-0000-1000-8000-00805F9B34FB";
    public static CURR_STATUS curr_status;
    private static ConnectThread connectThread;
    private static ReadThread readThread;
    private static SendThread sendThread;
    private static Handler queueMsg;


    public enum CURR_STATUS {
        CONNECTED, DISCONNECTED
    }

// 初始化   这里需要注意是蓝牙如果没有开启则要开启后 在activity回调里再次获取发现搜索一下设备;
    public static BluetoothAdapter initBlueTooth(Context context, Activity currActivity) {
        mContext = context;
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
        if (bluetoothAdapter != null) {
            if (!bluetoothAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                currActivity.startActivityForResult(intent, REQUEST_BLUE_ENABLE);
            }
            return bluetoothAdapter;
        } else {
            return null;
        }
    }

// 初始化后可以调用此方法,还有在activity回调成功时调用;    此处还需要一个广播来接收相应的消息,这个广播我稍后发出来    ,广播其实也可以放到此工具类里,我只是分出来了,放到这个工具类里好处时各个状态比较好监听,放到外面则需要两个监听,有些多余;
    public static void startDiscover() {
        if (bluetoothAdapter != null) {
            if (bluetoothAdapter.isDiscovering()) {
                bluetoothAdapter.cancelDiscovery();
                bluetoothAdapter.startDiscovery();
            } else {
                bluetoothAdapter.startDiscovery();
            }
        }
    }

// 这个在连接之前,扫描设备超时的时候调用;节省资源;
    public static void cancelDiscover() {
        if (bluetoothAdapter != null) {
            bluetoothAdapter.cancelDiscovery();
        }
    }

// 退出的时候调用,释放及重置
    public static void close() {
        if (bluetoothSocket != null) {
            try {
                bluetoothSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bluetoothAdapter != null) {
            bluetoothAdapter = null;
        }
        if (connectThread !&#
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值