zhuguifei
2025-12-30 61eee1173c00a7ba9d9c748d28fe3acdb33b9441
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
package com.blakequ.bluetooth_manager_lib.util;
 
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
 
/**
 * the utils of bluetooth
 *
 * <li>
 * <li>1.check phone is supports BLE features{@link #isBluetoothLeSupported(Context)}
 * <li>2.the bluetooth is open or enable{@link #isBluetoothIsEnable()}
 * <li>3.if the bluetooth is closed you can using this method open system setting to open bluetooth{@link #askUserToEnableBluetoothIfNeeded(Activity)}??{@link #openBlueToothSetting(Activity)}
 */
public final class BluetoothUtils {
    private static String TAG = "BluetoothUtils";
    public final static int REQUEST_ENABLE_BT = 2001;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothManager mBluetoothManager = null;
    private static BluetoothUtils mBluetoothUtils;
    private Context mContext;
 
    public static synchronized BluetoothUtils getInstance(Context mContext){
        if (mBluetoothUtils == null){
            mBluetoothUtils = new BluetoothUtils(mContext);
        }
        return mBluetoothUtils;
    }
 
    private BluetoothUtils(Context mContext) {
        this.mContext = mContext;
    }
 
    /**
     * get adapter
     * @return
     */
    public BluetoothAdapter getBluetoothAdapter() {
        if (mBluetoothAdapter == null) {
            // Initializes Bluetooth adapter.
            if (mBluetoothManager == null){
                mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
            }
            mBluetoothAdapter = mBluetoothManager.getAdapter();
            if (mBluetoothAdapter == null) {
                Log.e(TAG, "Failed to construct a BluetoothAdapter");
            }
        }
        return mBluetoothAdapter;
    }
 
    /**
     * bluetooth is enable
     */
    public boolean isBluetoothIsEnable(){
        if (getBluetoothAdapter() == null || !isBluetoothLeSupported(mContext)) {
            return false;
        }
 
        return getBluetoothAdapter().isEnabled();
    }
 
    /**
     * open system setting to open bluetooth
     * <p>Notification of the result of this activity is posted using the
     * {@link android.app.Activity#onActivityResult} callback. The
     * <code>resultCode</code>
     * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
     * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
     * has rejected the request or an error has occurred.
     */
    public void askUserToEnableBluetoothIfNeeded(Activity activity) {
        if (isBluetoothLeSupported(activity) && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {
            final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
 
    /**
     * you phone is support bluetooth feature
     * @return
     */
    public static boolean isBluetoothLeSupported(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
    }
 
    /**
     * open system setting to open bluetooth
     * <p>Notification of the result of this activity is posted using the
     * {@link android.app.Activity#onActivityResult} callback. The
     * <code>resultCode</code>
     * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
     * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
     * has rejected the request or an error has occurred.
     * @param mActivity
     */
    public static void openBlueToothSetting(Activity mActivity){
        final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        mActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
 
    public static boolean isCharacteristicRead(int property){
        if ((property & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            return true;
        }
        return false;
    }
 
    public static boolean isCharacteristicWrite(int property){
        if ((property & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0
                || (property & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
            return true;
        }
        return false;
    }
 
    public static boolean isCharacteristicNotify(int property){
        if ((property & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0
                || (property & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
            return true;
        }
        return false;
    }
 
    public static boolean isCharacteristicBroadcast(int property){
        if ((property & BluetoothGattCharacteristic.PROPERTY_BROADCAST) > 0){
            return true;
        }
        return false;
    }
 
}