├── .gitignore
├── .idea
├── gradle.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── qt
│ │ └── bluetooth
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── qt
│ │ │ └── bluetooth
│ │ │ ├── BtItemBean.java
│ │ │ ├── MainActivity.java
│ │ │ ├── SimpleAdapter.java
│ │ │ └── bluetooth
│ │ │ ├── BluetoothHelper.java
│ │ │ └── interfaces
│ │ │ ├── IBTBoudListener.java
│ │ │ ├── IBTConnectListener.java
│ │ │ ├── IBTScanListener.java
│ │ │ ├── IBTStateListener.java
│ │ │ └── IBluetoothHelper.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ ├── ic_launcher_background.xml
│ │ └── time_settings_auto_sync.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── recycler_item_bluetooth.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_round.png
│ │ ├── icon_bluetooth.png
│ │ ├── icon_computer.png
│ │ ├── icon_headset.png
│ │ ├── icon_phone.png
│ │ └── icon_telephone.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_round.png
│ │ ├── setting_time_switchon_close_btn.png
│ │ └── setting_time_switchon_open_btn.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── qt
│ └── bluetooth
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 | C:\Users\win7\AppData\Roaming\Subversion
35 |
36 |
37 |
38 |
39 |
40 | 1.8
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 经典蓝牙的功能的使用,设备的管理,及蓝牙音箱、耳机的连接
2 | 具体的功能如下:
3 | ```
4 | void init(Context context);
5 |
6 | boolean removeBond(BluetoothDevice device);//取消配对
7 |
8 | boolean connect(BluetoothDevice device);//连接设备
9 |
10 | boolean open();//打开蓝牙
11 |
12 | boolean close();//关闭蓝牙
13 |
14 | boolean startDiscovery();//搜索蓝牙
15 | boolean stopDiscovery();//停止搜索蓝牙
16 | String getName();//获取本地蓝牙名称
17 | boolean setName(String name);//设置蓝牙的名称
18 | String getAddress();//获取本地蓝牙地址
19 | boolean isEnable();//蓝牙是否可用,即是否打开
20 | boolean isSupport();//是否支持蓝牙
21 | Set getBondedDevices();//获取以配对设备
22 | boolean createBond(BluetoothDevice device);//配对
23 | boolean disconnect(BluetoothDevice device);//断开设备
24 | void destroy();
25 | void getConnectedDevices();//获取已连接的设备
26 | boolean isConnected(BluetoothDevice device);//是否连接
27 |
28 |
29 | boolean setDiscoverableTimeout(int timeout);//设备可见时间
30 |
31 | void setBTStateListener(IBTStateListener btStateListener);//蓝牙状态监听(开关监听)
32 |
33 | void setBTScanListener(IBTScanListener btScanListener);//蓝牙搜索监听
34 |
35 | void setBTBoudListener(IBTBoudListener btBoudListener);//蓝牙绑定监听
36 |
37 | void setBTConnectListener(IBTConnectListener btConnectListener);//设置连接监听
38 | ```
39 | 使用时,直接使用BluetoothHelper即可,[详细介绍](https://blog.csdn.net/qtiao/article/details/97654675)
40 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.qt.bluetooth"
7 | minSdkVersion 18
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:26.1.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | implementation 'com.android.support:recyclerview-v7:26.1.0'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29 | androidTestImplementation('com.android.support:support-annotations:26.1.0') {
30 | force = true
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/qt/bluetooth/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.qt.bluetooth", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/BtItemBean.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 |
5 | /**
6 | *@date 2019/7/22
7 | *@desc 蓝牙bean
8 | *
9 | */
10 |
11 | public class BtItemBean{
12 |
13 | public static final int STATE_BOND_NONE=-1;//未配对
14 | public static final int STATE_UNCONNECT=0;//未连接
15 | public static final int STATE_BONDING=1;//配对中
16 | public static final int STATE_BONDED=2;//已配对
17 | public static final int STATE_CONNECTING=3;//连接中
18 | public static final int STATE_CONNECTED=4;//已连接
19 | public static final int STATE_DISCONNECTING=5;//断开中
20 | public static final int STATE_DISCONNECTED=6;//已断开(但还保存)
21 | private int state;
22 |
23 | private BluetoothDevice bluetoothDevice;
24 | public BtItemBean(){
25 |
26 | }
27 | public BtItemBean(BluetoothDevice bluetoothDevice){
28 | this.bluetoothDevice=bluetoothDevice;
29 | }
30 |
31 | public BtItemBean(int state, BluetoothDevice bluetoothDevice){
32 | this.state=state;
33 | this.bluetoothDevice=bluetoothDevice;
34 | }
35 |
36 | public int getState() {
37 | return state;
38 | }
39 |
40 | public void setState(int state) {
41 | this.state = state;
42 | }
43 |
44 | public BluetoothDevice getBluetoothDevice() {
45 | return bluetoothDevice;
46 | }
47 |
48 | public void setBluetoothDevice(BluetoothDevice bluetoothDevice) {
49 | this.bluetoothDevice = bluetoothDevice;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth;
2 |
3 | import android.app.AlertDialog;
4 | import android.bluetooth.BluetoothAdapter;
5 | import android.bluetooth.BluetoothDevice;
6 | import android.content.DialogInterface;
7 | import android.os.Bundle;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.support.v7.widget.LinearLayoutManager;
10 | import android.support.v7.widget.RecyclerView;
11 | import android.text.TextUtils;
12 | import android.view.View;
13 | import android.widget.CompoundButton;
14 | import android.widget.Switch;
15 | import android.widget.TextView;
16 | import android.widget.Toast;
17 |
18 | import com.qt.bluetooth.bluetooth.BluetoothHelper;
19 | import com.qt.bluetooth.bluetooth.interfaces.IBTBoudListener;
20 | import com.qt.bluetooth.bluetooth.interfaces.IBTConnectListener;
21 | import com.qt.bluetooth.bluetooth.interfaces.IBTScanListener;
22 | import com.qt.bluetooth.bluetooth.interfaces.IBTStateListener;
23 | import com.qt.bluetooth.bluetooth.interfaces.IBluetoothHelper;
24 |
25 | import java.util.List;
26 | import java.util.Set;
27 |
28 | public class MainActivity extends AppCompatActivity implements SimpleAdapter.ItemClickListener {
29 | private TextView mTvName,mTvNameTip,mTvPairedDeviceTip,mTvUseDeviceTip;
30 | private RecyclerView mRecyclerPaired,mRecyclerUse;
31 | private SimpleAdapter mPairedAdapter,mUseAdapter;
32 | private IBluetoothHelper mBluetoothHelper;
33 | private AlertDialog simpleDialog;
34 | private Switch mSwBluetooth;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.activity_main);
40 | mBluetoothHelper=new BluetoothHelper();
41 | mBluetoothHelper.setBTStateListener(mBTStateListener);//设置打开关闭状态监听
42 | mBluetoothHelper.setBTScanListener(mBTScanListener);//设置扫描监听
43 | mBluetoothHelper.setBTBoudListener(mBTBoudListener);//设置配对监听
44 | mBluetoothHelper.setBTConnectListener(mBTConnectListener);//设置连接监听
45 | mBluetoothHelper.init(this);
46 | mRecyclerPaired = (RecyclerView) findViewById(R.id.recycler_view_paired);
47 | mRecyclerUse = (RecyclerView) findViewById(R.id.recycler_view_use);
48 | mSwBluetooth = (Switch) findViewById(R.id.sw_bluetooth);
49 |
50 | mSwBluetooth = (Switch) findViewById(R.id.sw_bluetooth);
51 | mTvName = (TextView) findViewById(R.id.tv_name);
52 | mTvNameTip = (TextView) findViewById(R.id.tv_name_tip);
53 | mTvPairedDeviceTip = (TextView) findViewById(R.id.tv_paired_device_tip);
54 | mTvUseDeviceTip = (TextView) findViewById(R.id.tv_use_device_tip);
55 |
56 |
57 | mRecyclerPaired.setNestedScrollingEnabled(false);
58 | mPairedAdapter = new SimpleAdapter();
59 | mRecyclerPaired.setLayoutManager(new LinearLayoutManager(this));
60 | // mRecyclerPaired.addItemDecoration(new SpacesItemDecoration(10));
61 | mRecyclerPaired.setAdapter(mPairedAdapter);
62 | mPairedAdapter.setItemClickListener(this);
63 |
64 | mRecyclerUse.setNestedScrollingEnabled(false);
65 | mUseAdapter = new SimpleAdapter();
66 | mRecyclerUse.setLayoutManager(new LinearLayoutManager(this));
67 | mRecyclerUse.setAdapter(mUseAdapter);
68 | mUseAdapter.setItemClickListener(this);
69 | mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
70 |
71 | mSwBluetooth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
72 | @Override
73 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
74 | if(!buttonView.isPressed())
75 | return;
76 | if(isChecked){
77 | mBluetoothHelper.open();
78 | }else{
79 | mBluetoothHelper.close();
80 | }
81 | }
82 | });
83 |
84 | mTvName.setText(mBluetoothHelper.getName());
85 | getBondedDevices();
86 | mBluetoothHelper.getConnectedDevices();
87 | }
88 |
89 | @Override
90 | protected void onResume() {
91 | super.onResume();
92 | mBluetoothHelper.startDiscovery();
93 | }
94 |
95 | @Override
96 | protected void onPause() {
97 | super.onPause();
98 | mBluetoothHelper.stopDiscovery();
99 | }
100 |
101 | /**
102 | * 获取已配对设备
103 | */
104 | private void getBondedDevices(){//以配对设备
105 | Set bluetoothDeviceSet=mBluetoothHelper.getBondedDevices();
106 | if(bluetoothDeviceSet!=null&&bluetoothDeviceSet.size()>0){
107 | for(BluetoothDevice device:bluetoothDeviceSet){
108 | addDevPaire(BtItemBean.STATE_BONDED,device);
109 | }
110 | }
111 | }
112 |
113 | /**
114 | * 向已配对列表中添加设备
115 | * @param state
116 | * @param dev
117 | */
118 | private void addDevPaire(int state,BluetoothDevice dev){
119 | BtItemBean btUseItem=findItemByList(mPairedAdapter.getData(),dev);
120 | if(btUseItem!=null){
121 | btUseItem.setBluetoothDevice(dev);
122 | }else{
123 | BtItemBean bluetoothItem=createBluetoothItem(dev);
124 | bluetoothItem.setState(state);
125 | mPairedAdapter.add(0,bluetoothItem);
126 | }
127 | mPairedAdapter.notifyDataSetChanged();
128 | }
129 |
130 | /**
131 | * 从集合 datas 中找 dev 对应的项
132 | * @param datas
133 | * @param dev
134 | */
135 | private BtItemBean findItemByList(List datas,BluetoothDevice dev){
136 | if(datas==null||datas.size()<1){
137 | return null;
138 | }
139 | for(BtItemBean btItemBean:datas){
140 | if(!TextUtils.isEmpty(dev.getAddress())&&dev.getAddress().equals(btItemBean.getBluetoothDevice().getAddress())){
141 | return btItemBean;
142 | }
143 | }
144 | return null;
145 | }
146 |
147 | private BtItemBean createBluetoothItem(BluetoothDevice device){
148 | BtItemBean btItemBean=new BtItemBean();
149 | btItemBean.setBluetoothDevice(device);
150 | return btItemBean;
151 | }
152 |
153 | /**
154 | * 向可用列表中添加设备
155 | * @param dev
156 | */
157 | private void addDevUse(BluetoothDevice dev){
158 | BtItemBean btUseItem=findItemByList(mUseAdapter.getData(),dev);
159 | if(btUseItem!=null){
160 | btUseItem.setBluetoothDevice(dev);
161 | }else{
162 | BtItemBean bluetoothItem=createBluetoothItem(dev);
163 | if(dev.getBondState()==BluetoothDevice.BOND_BONDED){
164 | bluetoothItem.setState(BtItemBean.STATE_BONDED);
165 | }else if(dev.getBondState()==BluetoothDevice.BOND_BONDING){
166 | bluetoothItem.setState(BtItemBean.STATE_BONDING);
167 | }
168 | mUseAdapter.add(0,bluetoothItem);
169 | }
170 | mUseAdapter.notifyDataSetChanged();
171 | }
172 |
173 | /**
174 | * 可用设备列表发生改变
175 | * @param state
176 | * @param dev
177 | */
178 | private void paireDevStateChange(int state,BluetoothDevice dev){
179 | BtItemBean btUseItem=findItemByList(mUseAdapter.getData(),dev);
180 | BtItemBean btPaireItem=findItemByList(mPairedAdapter.getData(),dev);
181 | if(btUseItem!=null){
182 | btUseItem.setState(state);
183 | btUseItem.setBluetoothDevice(dev);
184 | mUseAdapter.remove(btUseItem);
185 | mUseAdapter.notifyDataSetChanged();
186 | if(btPaireItem!=null){
187 | mPairedAdapter.remove(btPaireItem);
188 | }
189 | mPairedAdapter.add(0,btUseItem);
190 | }else if(btPaireItem!=null){
191 | btPaireItem.setState(state);
192 | btPaireItem.setBluetoothDevice(dev);
193 | }else{
194 | BtItemBean bluetoothItem=createBluetoothItem(dev);
195 | bluetoothItem.setState(state);
196 | mPairedAdapter.add(0,bluetoothItem);
197 | }
198 | mPairedAdapter.notifyDataSetChanged();
199 | }
200 |
201 | /**
202 | * 可用设备列表发生改变
203 | * @param state
204 | * @param dev
205 | */
206 | private void useDevStateChange(int state,BluetoothDevice dev){
207 | BtItemBean btUseItem=findItemByList(mUseAdapter.getData(),dev);
208 | BtItemBean btPaireItem=findItemByList(mPairedAdapter.getData(),dev);
209 | if(btPaireItem!=null){
210 | btPaireItem.setState(state);
211 | btPaireItem.setBluetoothDevice(dev);
212 | mPairedAdapter.remove(btPaireItem);
213 | mPairedAdapter.notifyDataSetChanged();
214 | if(btUseItem!=null){
215 | mUseAdapter.remove(btUseItem);
216 | }
217 | mUseAdapter.add(0,btPaireItem);
218 | }else if(btUseItem!=null){
219 | btUseItem.setState(state);
220 | btUseItem.setBluetoothDevice(dev);
221 | }else{
222 | BtItemBean bluetoothItem=createBluetoothItem(dev);
223 | bluetoothItem.setState(state);
224 | mUseAdapter.add(0,bluetoothItem);
225 | }
226 | mUseAdapter.notifyDataSetChanged();
227 | }
228 |
229 | @Override
230 | protected void onDestroy() {
231 | super.onDestroy();
232 | mBluetoothHelper.setBTStateListener(null);//设置打开关闭状态监听
233 | mBluetoothHelper.setBTScanListener(null);//设置扫描监听
234 | mBluetoothHelper.setBTBoudListener(null);//设置配对监听
235 | mBluetoothHelper.setBTConnectListener(null);//设置连接监听
236 | mBluetoothHelper.destroy();
237 | }
238 |
239 | //蓝牙状态监听
240 | private IBTStateListener mBTStateListener=new IBTStateListener() {
241 |
242 | /**
243 | * 蓝牙开关状态
244 | * int STATE_OFF = 10; //蓝牙关闭
245 | * int STATE_ON = 12; //蓝牙打开
246 | * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
247 | * int STATE_TURNING_ON = 11; //蓝牙正在打开
248 | */
249 | @Override
250 | public void onStateChange(int state) {
251 | switch (state){
252 | case BluetoothAdapter.STATE_OFF:
253 | Toast.makeText(MainActivity.this,"蓝牙已关闭",Toast.LENGTH_SHORT).show();
254 | mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
255 | mTvNameTip.setVisibility(View.GONE);
256 | mTvName.setVisibility(View.GONE);
257 | mTvPairedDeviceTip.setVisibility(View.GONE);
258 | mTvUseDeviceTip.setVisibility(View.GONE);
259 | mRecyclerPaired.setVisibility(View.GONE);
260 | mRecyclerUse.setVisibility(View.GONE);
261 | mPairedAdapter.clear();
262 | mPairedAdapter.notifyDataSetChanged();
263 | mUseAdapter.clear();
264 | mUseAdapter.notifyDataSetChanged();
265 | break;
266 | case BluetoothAdapter.STATE_ON:
267 | Toast.makeText(MainActivity.this,"蓝牙已打开",Toast.LENGTH_SHORT).show();
268 | mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
269 | mTvNameTip.setVisibility(View.VISIBLE);
270 | mTvName.setVisibility(View.VISIBLE);
271 | mTvPairedDeviceTip.setVisibility(View.VISIBLE);
272 | mTvUseDeviceTip.setVisibility(View.VISIBLE);
273 | mRecyclerPaired.setVisibility(View.VISIBLE);
274 | mRecyclerUse.setVisibility(View.VISIBLE);
275 | getBondedDevices();
276 | mBluetoothHelper.setDiscoverableTimeout(300);//设置可见时间
277 | // Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
278 | // //讲一个键值对对方到Intent对象当中,用于指定可见状态的持续时间
279 | // discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
280 | // startActivity(discoverableIntent);
281 | mBluetoothHelper.startDiscovery();
282 | break;
283 | case BluetoothAdapter.STATE_TURNING_OFF:
284 | case BluetoothAdapter.STATE_TURNING_ON:
285 | break;
286 | }
287 | }
288 | };
289 |
290 | //蓝牙搜索监听
291 | private IBTScanListener mBTScanListener=new IBTScanListener() {
292 | @Override
293 | public void onScanStart() {//搜索开始
294 | Toast.makeText(MainActivity.this,"搜索开始",Toast.LENGTH_SHORT).show();
295 | }
296 |
297 | @Override
298 | public void onScanStop(List deviceList) {//搜索结束
299 |
300 | }
301 |
302 | /**
303 | *
304 | * @param device
305 | */
306 | @Override
307 | public void onFindDevice(BluetoothDevice device) {//发现新设备
308 | if(device.getBondState()==BluetoothDevice.BOND_BONDED) {//已配对
309 | addDevPaire(BtItemBean.STATE_BONDED,device);
310 | }else{
311 | addDevUse(device);
312 | }
313 | }
314 | };
315 |
316 | //蓝牙配对监听
317 | private IBTBoudListener mBTBoudListener=new IBTBoudListener() {
318 |
319 | /**
320 | * 设备配对状态改变
321 | * int BOND_NONE = 10; //配对没有成功
322 | * int BOND_BONDING = 11; //配对中
323 | * int BOND_BONDED = 12; //配对成功
324 | */
325 | @Override
326 | public void onBondStateChange(BluetoothDevice dev) {
327 | if(dev.getBondState()==BluetoothDevice.BOND_BONDED) {//已配对
328 | paireDevStateChange(BtItemBean.STATE_BONDED,dev);
329 | mBluetoothHelper.connect(dev);
330 | }else if(dev.getBondState()==BluetoothDevice.BOND_BONDING){//配对中
331 | useDevStateChange(BtItemBean.STATE_BONDING,dev);
332 | }else{//未配对
333 | BtItemBean btUseItem=findItemByList(mUseAdapter.getData(),dev);
334 | if(btUseItem!=null&&btUseItem.getState()==BtItemBean.STATE_BONDING){
335 | Toast.makeText(MainActivity.this,"请确认配对设备已打开且在通信范围内",Toast.LENGTH_SHORT).show();
336 | }
337 | useDevStateChange(BtItemBean.STATE_BOND_NONE,dev);
338 | }
339 | }
340 | };
341 |
342 | //蓝牙配对监听
343 | private IBTConnectListener mBTConnectListener=new IBTConnectListener() {
344 | @Override
345 | public void onConnecting(BluetoothDevice bluetoothDevice) {//连接中
346 | paireDevStateChange(BtItemBean.STATE_CONNECTING,bluetoothDevice);
347 | }
348 |
349 | @Override
350 | public void onConnected(BluetoothDevice bluetoothDevice) {//连接成功
351 | paireDevStateChange(BtItemBean.STATE_CONNECTED,bluetoothDevice);
352 | }
353 |
354 | @Override
355 | public void onDisConnecting(BluetoothDevice bluetoothDevice) {//断开中
356 | paireDevStateChange(BtItemBean.STATE_DISCONNECTING,bluetoothDevice);
357 | }
358 |
359 | @Override
360 | public void onDisConnect(BluetoothDevice bluetoothDevice) {//断开
361 | paireDevStateChange(BtItemBean.STATE_DISCONNECTED,bluetoothDevice);
362 | }
363 |
364 | @Override
365 | public void onConnectedDevice(List devices) {//已连接设备
366 | if(devices==null||devices.size()<1){
367 | return;
368 | }
369 | for(BluetoothDevice dev:devices){
370 | BtItemBean btUseItem=findItemByList(mPairedAdapter.getData(),dev);
371 | if(btUseItem!=null){
372 | btUseItem.setBluetoothDevice(dev);
373 | if(mBluetoothHelper.isConnected(dev)){
374 | btUseItem.setState(BtItemBean.STATE_CONNECTED);
375 | }else if( btUseItem.getState()!=BtItemBean.STATE_CONNECTED){
376 | btUseItem.setState(BtItemBean.STATE_DISCONNECTED);
377 | }
378 | }else{
379 | BtItemBean bluetoothItem=createBluetoothItem(dev);
380 | if(mBluetoothHelper.isConnected(dev)){
381 | bluetoothItem.setState(BtItemBean.STATE_CONNECTED);
382 | }else{
383 | btUseItem.setState(BtItemBean.STATE_DISCONNECTED);
384 | }
385 | mPairedAdapter.add(0,bluetoothItem);
386 | }
387 | }
388 | mPairedAdapter.notifyDataSetChanged();
389 | }
390 | };
391 |
392 | @Override
393 | public void onItemClickListener(BtItemBean btItemBean) {
394 | final BluetoothDevice bluetoothDevice=btItemBean.getBluetoothDevice();
395 | switch (btItemBean.getState()){
396 | case BtItemBean.STATE_UNCONNECT://未连接
397 | case BtItemBean.STATE_BOND_NONE://未配对
398 | mBluetoothHelper.createBond(bluetoothDevice);
399 | break;
400 | case BtItemBean.STATE_BONDING://配对中
401 | break;
402 | case BtItemBean.STATE_BONDED://已配对
403 | simpleDialog=new AlertDialog.Builder(this)
404 | .setTitle("已配对")
405 | .setMessage(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName())
406 | .setNegativeButton("取消",null)
407 | .setNeutralButton("配对", new DialogInterface.OnClickListener() {
408 | @Override
409 | public void onClick(DialogInterface dialog, int which) {
410 | dialog.dismiss();
411 | simpleDialog = null;
412 | mBluetoothHelper.removeBond(bluetoothDevice);
413 | }
414 | })
415 | .show();
416 | break;
417 | case BtItemBean.STATE_CONNECTING://连接中
418 | break;
419 | case BtItemBean.STATE_CONNECTED://已连接
420 | simpleDialog = new AlertDialog.Builder(this)
421 | .setTitle("已连接")
422 | .setMessage(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName())
423 | .setNegativeButton("取消",null)
424 | .setNeutralButton("配对", new DialogInterface.OnClickListener() {
425 | @Override
426 | public void onClick(DialogInterface dialog, int which) {
427 | dialog.dismiss();
428 | simpleDialog = null;
429 | mBluetoothHelper.disconnect(bluetoothDevice);
430 | }
431 | })
432 | .show();
433 | break;
434 | case BtItemBean.STATE_DISCONNECTING://断开中
435 | break;
436 | case BtItemBean.STATE_DISCONNECTED://已断开(但还保存)
437 | simpleDialog = new AlertDialog.Builder(this)
438 | .setTitle("已保存")
439 | .setMessage(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName())
440 | .setNegativeButton("取消",null)
441 | .setNeutralButton("删除", new DialogInterface.OnClickListener() {
442 | @Override
443 | public void onClick(DialogInterface dialog, int which) {
444 | dialog.dismiss();
445 | simpleDialog = null;
446 | mBluetoothHelper.removeBond(bluetoothDevice);
447 | }
448 | })
449 | .setPositiveButton("连接", new DialogInterface.OnClickListener() {
450 | @Override
451 | public void onClick(DialogInterface dialog, int which) {
452 | dialog.dismiss();
453 | simpleDialog = null;
454 | mBluetoothHelper.connect(bluetoothDevice);
455 | }
456 | })
457 | .show();
458 | break;
459 | }
460 | }
461 | }
462 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/SimpleAdapter.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth;
2 |
3 | import android.bluetooth.BluetoothClass;
4 | import android.bluetooth.BluetoothDevice;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.text.TextUtils;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ImageView;
11 | import android.widget.TextView;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | /**
17 | * Created by win7 on 2019/7/29.
18 | */
19 |
20 | public class SimpleAdapter extends RecyclerView.Adapter{
21 | private List datas;
22 | private ItemClickListener mItemClickListener;
23 | public SimpleAdapter(){
24 | datas=new ArrayList<>();
25 | }
26 |
27 | public void addData(BtItemBean btItemBean){
28 | datas.add(btItemBean);
29 | notifyDataSetChanged();
30 | }
31 |
32 | public void add(int index,BtItemBean btItemBean){
33 | datas.add(index,btItemBean);
34 | notifyDataSetChanged();
35 | }
36 |
37 | public void addDataALL(List btItemBeans){
38 | datas.addAll(btItemBeans);
39 | notifyDataSetChanged();
40 | }
41 |
42 | public void clear(){
43 | datas.clear();
44 | }
45 |
46 | public List getData(){
47 | return datas;
48 | }
49 |
50 | public void remove(Object o){
51 | datas.remove(o);
52 | }
53 |
54 |
55 | @Override
56 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
57 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_bluetooth,parent,false);
58 | return new MyViewHolder(view);
59 | }
60 |
61 | @Override
62 | public void onBindViewHolder(MyViewHolder holder, int position) {
63 | final BtItemBean btItemBean=datas.get(position);
64 | BluetoothDevice bluetoothDevice=btItemBean.getBluetoothDevice();
65 | holder.txt_wifi_name.setText(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName());
66 | //连接状态
67 | switch (btItemBean.getState()){
68 | case BtItemBean.STATE_UNCONNECT://未连接
69 | case BtItemBean.STATE_BOND_NONE://未配对
70 | holder.txt_link_tips.setText("");
71 | break;
72 | case BtItemBean.STATE_BONDING://配对中
73 | holder.txt_link_tips.setText("配对中");
74 | break;
75 | case BtItemBean.STATE_BONDED://已配对
76 | holder.txt_link_tips.setText("已配对");
77 | break;
78 | case BtItemBean.STATE_CONNECTING://连接中
79 | holder.txt_link_tips.setText("连接中");
80 | break;
81 | case BtItemBean.STATE_CONNECTED://已连接
82 | holder.txt_link_tips.setText("已连接");
83 | break;
84 | case BtItemBean.STATE_DISCONNECTING://断开中
85 | holder.txt_link_tips.setText("断开中");
86 | break;
87 | case BtItemBean.STATE_DISCONNECTED://已断开
88 | holder.txt_link_tips.setText("已保存");
89 | break;
90 | }
91 |
92 | int styleMajor = bluetoothDevice.getBluetoothClass().getMajorDeviceClass();//获取蓝牙主要分类
93 | switch (styleMajor) {
94 | case BluetoothClass.Device.Major.AUDIO_VIDEO://音频设备
95 | holder.img_signal.setImageResource(R.mipmap.icon_headset);
96 | break;
97 | case BluetoothClass.Device.Major.COMPUTER://电脑
98 | holder.img_signal.setImageResource(R.mipmap.icon_computer);
99 | break;
100 | case BluetoothClass.Device.Major.HEALTH://健康状况
101 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
102 | break;
103 | case BluetoothClass.Device.Major.IMAGING://镜像,映像
104 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
105 | break;
106 | case BluetoothClass.Device.Major.MISC://麦克风
107 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
108 | break;
109 | case BluetoothClass.Device.Major.NETWORKING://网络
110 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
111 | break;
112 | case BluetoothClass.Device.Major.PERIPHERAL://外部设备
113 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
114 | break;
115 | case BluetoothClass.Device.Major.PHONE://电话
116 | holder.img_signal.setImageResource(R.mipmap.icon_phone);
117 | break;
118 | case BluetoothClass.Device.Major.TOY://玩具
119 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
120 | break;
121 | case BluetoothClass.Device.Major.UNCATEGORIZED://未知的
122 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
123 | break;
124 | case BluetoothClass.Device.Major.WEARABLE://穿戴设备
125 | holder.img_signal.setImageResource(R.mipmap.icon_bluetooth);
126 | break;
127 | }
128 |
129 | holder.layout.setOnClickListener(new View.OnClickListener() {
130 | @Override
131 | public void onClick(View v) {
132 | if(mItemClickListener!=null){
133 | mItemClickListener.onItemClickListener(btItemBean);
134 | }
135 | }
136 | });
137 | }
138 |
139 | @Override
140 | public int getItemCount() {
141 | return datas==null?0:datas.size();
142 | }
143 |
144 | class MyViewHolder extends RecyclerView.ViewHolder{
145 | private ImageView img_signal;
146 | private TextView txt_wifi_name;
147 | private TextView txt_link_tips;
148 | private View layout;
149 |
150 | public MyViewHolder(View itemView) {
151 | super(itemView);
152 | img_signal = (ImageView) itemView.findViewById(R.id.img_signal);
153 | txt_wifi_name = (TextView) itemView.findViewById(R.id.txt_wifi_name);
154 | txt_link_tips = (TextView) itemView.findViewById(R.id.txt_link_tips);
155 | layout = itemView.findViewById(R.id.layout);
156 | }
157 | }
158 |
159 | public void setItemClickListener(ItemClickListener listener){
160 | mItemClickListener=listener;
161 | }
162 |
163 | public interface ItemClickListener{
164 | void onItemClickListener(BtItemBean btItemBean);
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/bluetooth/BluetoothHelper.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth.bluetooth;
2 |
3 | import android.bluetooth.BluetoothA2dp;
4 | import android.bluetooth.BluetoothAdapter;
5 | import android.bluetooth.BluetoothClass;
6 | import android.bluetooth.BluetoothDevice;
7 | import android.bluetooth.BluetoothHeadset;
8 | import android.bluetooth.BluetoothManager;
9 | import android.bluetooth.BluetoothProfile;
10 | import android.content.BroadcastReceiver;
11 | import android.content.Context;
12 | import android.content.Intent;
13 | import android.content.IntentFilter;
14 | import android.text.TextUtils;
15 | import android.util.Log;
16 |
17 | import com.qt.bluetooth.bluetooth.interfaces.IBTBoudListener;
18 | import com.qt.bluetooth.bluetooth.interfaces.IBTConnectListener;
19 | import com.qt.bluetooth.bluetooth.interfaces.IBTScanListener;
20 | import com.qt.bluetooth.bluetooth.interfaces.IBTStateListener;
21 | import com.qt.bluetooth.bluetooth.interfaces.IBluetoothHelper;
22 |
23 | import java.lang.reflect.Method;
24 | import java.util.ArrayList;
25 | import java.util.List;
26 | import java.util.Set;
27 |
28 | /**
29 | *@date 2019/7/23
30 | *@desc 蓝牙辅助类
31 | *
32 | */
33 |
34 | public class BluetoothHelper implements IBluetoothHelper {
35 | private final String TAG="BluetoothHelper";
36 | private Context mContext;
37 | private BluetoothManager mBluetoothManager;
38 | private BluetoothAdapter mBluetoothadapter;
39 | private BluetoothA2dp mBluetoothA2dp;
40 | private BluetoothHeadset mBluetoothHeadset;
41 | // private BluetoothHealth mBluetoothHealth;
42 | private IntentFilter mFilter;
43 |
44 | private IBTStateListener mBTStateListener;//蓝牙状态监听
45 | private IBTScanListener mBTScanListener;//蓝牙搜索监听
46 | private IBTBoudListener mBTBoudListener;//蓝牙绑定监听
47 | private IBTConnectListener mBTConnectListener;//连接监听
48 | private boolean isBackConDev;//是否返回已连接的设备
49 | private boolean isA2dpComplete,isHeadsetComplete;
50 |
51 | @Override
52 | public void init(Context context) {
53 | mContext=context.getApplicationContext();
54 | mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
55 | mBluetoothadapter = mBluetoothManager.getAdapter();
56 | isA2dpComplete=false;
57 | isHeadsetComplete=false;
58 | mBluetoothadapter.getProfileProxy(mContext,mProfileServiceListener, BluetoothProfile.A2DP);
59 | mBluetoothadapter.getProfileProxy(mContext,mProfileServiceListener, BluetoothProfile.HEADSET);
60 | // mBluetoothadapter.getProfileProxy(mContext,mProfileServiceListener, BluetoothProfile.HEALTH);
61 | if(mFilter==null){
62 | mContext.registerReceiver(mBluetoothReceiver,makeFilter());
63 | }
64 | }
65 |
66 |
67 | private IntentFilter makeFilter() {
68 | if(mFilter==null){
69 | mFilter = new IntentFilter();
70 | mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
71 | mFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //蓝牙开关状态
72 | mFilter.addAction(BluetoothDevice.ACTION_FOUND);//蓝牙发现新设备(未配对)
73 | mFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //蓝牙开始搜索
74 | mFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //蓝牙搜索结束
75 | mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //设备配对状态改变
76 | mFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//设备建立连接
77 | mFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); //设备断开连接
78 | mFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); //BluetoothAdapter连接状态
79 | mFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); //BluetoothHeadset连接状态
80 | mFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); //BluetoothA2dp连接状态
81 | }
82 | return mFilter;
83 | }
84 |
85 |
86 | /**
87 | * 连接A2dp 与 HeadSet
88 | * @param device
89 | * @param device
90 | */
91 | private boolean connectA2dpAndHeadSet(Class btClass,BluetoothProfile bluetoothProfile,BluetoothDevice device){
92 | setPriority(device, 100); //设置priority
93 | try {
94 | //通过反射获取BluetoothA2dp中connect方法(hide的),进行连接。
95 | Method connectMethod =btClass.getMethod("connect",
96 | BluetoothDevice.class);
97 | connectMethod.setAccessible(true);
98 | connectMethod.invoke(bluetoothProfile, device);
99 | return true;
100 | } catch (Exception e) {
101 | e.printStackTrace();
102 | }
103 | return false;
104 | }
105 |
106 | /**
107 | * 断开A2dp 与 HeadSet
108 | * @param device
109 | */
110 | private boolean disConnectA2dpAndHeadSet(Class btClass,BluetoothProfile bluetoothProfile,BluetoothDevice device){
111 | setPriority(device, 0);
112 | try {
113 | //通过反射获取BluetoothA2dp中connect方法(hide的),断开连接。
114 | Method connectMethod =btClass.getMethod("disconnect",
115 | BluetoothDevice.class);
116 | connectMethod.setAccessible(true);
117 | connectMethod.invoke(bluetoothProfile, device);
118 | return true;
119 | } catch (Exception e) {
120 | e.printStackTrace();
121 | }
122 | return false;
123 | }
124 |
125 | /**
126 | * 设置优先级
127 | * 优先级是必要的,否则可能导致连接或断开连接失败等问题
128 | * @param device
129 | * @param priority
130 | */
131 | private void setPriority(BluetoothDevice device, int priority) {
132 | if (mBluetoothA2dp == null) return;
133 | try {//通过反射获取BluetoothA2dp中setPriority方法(hide的),设置优先级
134 | Method connectMethod =BluetoothA2dp.class.getMethod("setPriority",
135 | BluetoothDevice.class,int.class);
136 | connectMethod.setAccessible(true);
137 | connectMethod.invoke(mBluetoothA2dp, device, priority);
138 | } catch (Exception e) {
139 | e.printStackTrace();
140 | }
141 | }
142 |
143 |
144 |
145 |
146 | @Override
147 | public boolean open() {//打开蓝牙 ture-打开成功
148 | if(mBluetoothadapter==null){
149 | return false;
150 | }
151 | return mBluetoothadapter.enable();
152 | }
153 |
154 | @Override
155 | public boolean close() {//关闭蓝牙
156 | if(mBluetoothadapter==null){
157 | return true;
158 | }
159 | return mBluetoothadapter.disable();
160 | }
161 |
162 | @Override
163 | public boolean startDiscovery() {//搜索蓝牙
164 | if(mBluetoothadapter==null){
165 | return false;
166 | }
167 | if (mBluetoothadapter.isDiscovering()) {
168 | mBluetoothadapter.cancelDiscovery();
169 | }
170 |
171 | return mBluetoothadapter.startDiscovery();
172 | }
173 |
174 | @Override
175 | public boolean stopDiscovery() {//停止搜索蓝牙
176 | if(mBluetoothadapter==null||!mBluetoothadapter.isDiscovering()){
177 | return true;
178 | }
179 | return mBluetoothadapter.cancelDiscovery();
180 | }
181 |
182 | @Override
183 | public String getName() {//获取本地蓝牙名称
184 | if(mBluetoothadapter==null){
185 | return null;
186 | }
187 | return mBluetoothadapter.getName();
188 | }
189 |
190 | @Override
191 | public boolean setName(String name) {//设置蓝牙的名称
192 | if (mBluetoothadapter == null) {
193 | return false;
194 | }
195 | return mBluetoothadapter.setName(name);
196 | }
197 |
198 | @Override
199 | public String getAddress() {//获取本地蓝牙地址
200 | if(mBluetoothadapter==null){
201 | return null;
202 | }
203 | return mBluetoothadapter.getAddress();
204 | }
205 |
206 | @Override
207 | public boolean isEnable() {//蓝牙是否可用,即是否打开
208 | if(mBluetoothadapter==null){
209 | return false;
210 | }
211 | return mBluetoothadapter.isEnabled();
212 | }
213 |
214 | @Override
215 | public boolean isSupport() {//是否支持蓝牙
216 | return mBluetoothadapter==null?false:true;
217 | }
218 |
219 | @Override
220 | public Set getBondedDevices() {//获取以配对设备
221 | if(mBluetoothadapter==null){
222 | return null;
223 | }
224 | return mBluetoothadapter.getBondedDevices();
225 | }
226 |
227 | @Override
228 | public boolean createBond(BluetoothDevice device) {//配对
229 | if(device==null){
230 | return false;
231 | }
232 | return device.createBond();
233 | }
234 |
235 | @Override
236 | public boolean removeBond(BluetoothDevice device) {//取消配对
237 | Class btDeviceCls = BluetoothDevice.class;
238 | Method removeBond = null;
239 | try {
240 | removeBond = btDeviceCls.getMethod("removeBond");
241 | removeBond.setAccessible(true);
242 | return (boolean) removeBond.invoke(device);
243 | } catch (Exception e) {
244 | e.printStackTrace();
245 | return false;
246 | }
247 | }
248 |
249 | @Override
250 | public boolean connect(BluetoothDevice device) {
251 | int styleMajor = device.getBluetoothClass().getMajorDeviceClass();
252 | boolean isConnect=false;
253 | switch (styleMajor) {
254 | case BluetoothClass.Device.Major.AUDIO_VIDEO://音频设备
255 | if(connectA2dpAndHeadSet(BluetoothHeadset.class,mBluetoothHeadset,device)){
256 | isConnect=true;
257 | }
258 | if(connectA2dpAndHeadSet(BluetoothA2dp.class,mBluetoothA2dp,device)){
259 | isConnect=true;
260 | }
261 | return isConnect;
262 | // case BluetoothClass.Device.Major.COMPUTER://电脑
263 | // break;
264 | // case BluetoothClass.Device.Major.HEALTH://健康状况
265 | // break;
266 | // case BluetoothClass.Device.Major.IMAGING://镜像,映像
267 | // break;
268 | case BluetoothClass.Device.Major.MISC://麦克风
269 | if(connectA2dpAndHeadSet(BluetoothHeadset.class,mBluetoothHeadset,device)){
270 | isConnect=true;
271 | }
272 | if(connectA2dpAndHeadSet(BluetoothA2dp.class,mBluetoothA2dp,device)){
273 | isConnect=true;
274 | }
275 | return isConnect;
276 | // case BluetoothClass.Device.Major.NETWORKING://网络
277 | // break;
278 | // case BluetoothClass.Device.Major.PERIPHERAL://外部设备
279 | // break;
280 | case BluetoothClass.Device.Major.PHONE://电话
281 | if(connectA2dpAndHeadSet(BluetoothHeadset.class,mBluetoothHeadset,device)){
282 | isConnect=true;
283 | }
284 | if(connectA2dpAndHeadSet(BluetoothA2dp.class,mBluetoothA2dp,device)){
285 | isConnect=true;
286 | }
287 | return isConnect;
288 | // case BluetoothClass.Device.Major.TOY://玩具
289 | // break;
290 | // case BluetoothClass.Device.Major.UNCATEGORIZED://未知的
291 | // break;
292 | // case BluetoothClass.Device.Major.WEARABLE://穿戴设备
293 | // break;
294 | }
295 | if(connectA2dpAndHeadSet(BluetoothHeadset.class,mBluetoothHeadset,device)){
296 | isConnect=true;
297 | }
298 | if(connectA2dpAndHeadSet(BluetoothA2dp.class,mBluetoothA2dp,device)){
299 | isConnect=true;
300 | }
301 | return isConnect;
302 | }
303 |
304 | @Override
305 | public boolean disconnect(BluetoothDevice device) {
306 | boolean isDisconnect=false;
307 | if(mBluetoothA2dp!=null){
308 | List devices=mBluetoothA2dp.getConnectedDevices();
309 | if(devices!=null&&devices.contains(device)){
310 | Log.d(TAG,"disconnect A2dp");
311 | isDisconnect=disConnectA2dpAndHeadSet(BluetoothA2dp.class,mBluetoothA2dp,device);
312 | }
313 | }
314 | if(mBluetoothHeadset!=null){
315 | List devices=mBluetoothHeadset.getConnectedDevices();
316 | if(devices!=null&&devices.contains(device)){
317 | Log.d(TAG,"disconnect Headset");
318 | isDisconnect=disConnectA2dpAndHeadSet(BluetoothHeadset.class,mBluetoothHeadset,device);
319 | }
320 | }
321 | return isDisconnect;
322 | // int styleMajor = device.getBluetoothClass().getMajorDeviceClass();
323 | // switch (styleMajor) {
324 | // case BluetoothClass.Device.Major.AUDIO_VIDEO://音频设备
325 | // return disConnectA2dpAndHeadSet(BluetoothA2dp.class,device);
326 | // case BluetoothClass.Device.Major.COMPUTER://电脑
327 | // break;
328 | // case BluetoothClass.Device.Major.HEALTH://健康状况
329 | // return disConnectA2dpAndHeadSet(BluetoothHealth.class,device);
330 | // case BluetoothClass.Device.Major.IMAGING://镜像,映像
331 | // break;
332 | // case BluetoothClass.Device.Major.MISC://麦克风
333 | // break;
334 | // case BluetoothClass.Device.Major.NETWORKING://网络
335 | // break;
336 | // case BluetoothClass.Device.Major.PERIPHERAL://外部设备
337 | // break;
338 | // case BluetoothClass.Device.Major.PHONE://电话
339 | // return disConnectA2dpAndHeadSet(BluetoothHeadset.class,device);
340 | // case BluetoothClass.Device.Major.TOY://玩具
341 | // break;
342 | // case BluetoothClass.Device.Major.UNCATEGORIZED://未知的
343 | // break;
344 | // case BluetoothClass.Device.Major.WEARABLE://穿戴设备
345 | // break;
346 | // }
347 | // return disConnectA2dpAndHeadSet(BluetoothA2dp.class,device);
348 | }
349 |
350 | @Override
351 | public void destroy() {
352 | if(mFilter!=null){
353 | mFilter=null;
354 | mContext.unregisterReceiver(mBluetoothReceiver);
355 | }
356 | isA2dpComplete=false;
357 | isHeadsetComplete=false;
358 | mBluetoothadapter.closeProfileProxy(BluetoothProfile.A2DP,mBluetoothA2dp);
359 | mBluetoothadapter.closeProfileProxy(BluetoothProfile.HEADSET,mBluetoothHeadset);
360 |
361 | }
362 |
363 | @Override
364 | public void getConnectedDevices() {
365 | if(isBackConDev){
366 | return;
367 | }
368 | isBackConDev=true;
369 | if(isA2dpComplete&&isHeadsetComplete){
370 | List devices=new ArrayList<>();
371 | if(mBluetoothA2dp!=null){
372 | // removeA2dpMacEqual();
373 | List deviceList=mBluetoothA2dp.getConnectedDevices();
374 | if(deviceList!=null&&deviceList.size()>0){
375 |
376 | devices.addAll(deviceList);
377 | }
378 | }
379 | if(mBluetoothHeadset!=null){
380 | // removeHeadsetMacEqual();
381 | List deviceList=mBluetoothHeadset.getConnectedDevices();
382 | if(deviceList!=null&&deviceList.size()>0){
383 | devices.addAll(deviceList);
384 | }
385 | }
386 | mBTConnectListener.onConnectedDevice(devices);
387 | isBackConDev=false;
388 | }
389 |
390 | }
391 |
392 | // /**
393 | // * 移除A2dp mac相等设备
394 | // */
395 | // private void removeA2dpMacEqual(){
396 | // if(mBluetoothA2dp==null){
397 | // return;
398 | // }
399 | // List deviceList=mBluetoothA2dp.getConnectedDevices();
400 | // if(deviceList==null||deviceList.size()<1){
401 | // return;
402 | // }
403 | // for(int i=0;i deviceList=mBluetoothHeadset.getConnectedDevices();
436 | // if(deviceList==null||deviceList.size()<1){
437 | // return;
438 | // }
439 | // for(int i=0;i devices);//已连接的设备
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/bluetooth/interfaces/IBTScanListener.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth.bluetooth.interfaces;
2 |
3 |
4 | import android.bluetooth.BluetoothDevice;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | *@date 2019/7/23
10 | *@desc 蓝牙搜索监听
11 | *
12 | */
13 |
14 | public interface IBTScanListener {
15 |
16 |
17 | /**
18 | * 搜索开始
19 | */
20 | void onScanStart();
21 |
22 | /**
23 | * 搜索结束
24 | *
25 | * @param deviceList
26 | */
27 | void onScanStop(List deviceList);
28 |
29 | /**
30 | * 发现新设备
31 | * @param device
32 | */
33 | void onFindDevice(BluetoothDevice device);
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/bluetooth/interfaces/IBTStateListener.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth.bluetooth.interfaces;
2 |
3 | /**
4 | *@date 2019/7/23
5 | *@desc 蓝牙状态监听(开关、配对、扫描、连接)
6 | *
7 | */
8 |
9 | public interface IBTStateListener {
10 |
11 | /**
12 | * 蓝牙开关状态
13 | * int STATE_OFF = 10; //蓝牙关闭
14 | * int STATE_ON = 12; //蓝牙打开
15 | * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
16 | * int STATE_TURNING_ON = 11; //蓝牙正在打开
17 | */
18 | void onStateChange(int state);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/qt/bluetooth/bluetooth/interfaces/IBluetoothHelper.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth.bluetooth.interfaces;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 | import android.content.Context;
5 |
6 | import java.util.Set;
7 |
8 | /**
9 | *@date 2019/7/23
10 | *@desc 蓝牙辅助统一接口
11 | *
12 | */
13 |
14 | public interface IBluetoothHelper {
15 |
16 | void init(Context context);
17 | boolean open();//打开蓝牙
18 | boolean close();//关闭蓝牙
19 | boolean startDiscovery();//搜索蓝牙
20 | boolean stopDiscovery();//停止搜索蓝牙
21 | String getName();//获取本地蓝牙名称
22 | boolean setName(String name);//设置蓝牙的名称
23 | String getAddress();//获取本地蓝牙地址
24 | boolean isEnable();//蓝牙是否可用,即是否打开
25 | boolean isSupport();//是否支持蓝牙
26 | Set getBondedDevices();//获取以配对设备
27 | boolean createBond(BluetoothDevice device);//配对
28 | boolean removeBond(BluetoothDevice device);//取消配对
29 |
30 | boolean connect(BluetoothDevice device);//连接设备
31 | boolean disconnect(BluetoothDevice device);//断开设备
32 | void destroy();
33 | void getConnectedDevices();//获取已连接的设备
34 | boolean isConnected(BluetoothDevice device);//是否连接
35 | boolean setDiscoverableTimeout(int timeout);//设备可见时间
36 |
37 |
38 | void setBTStateListener(IBTStateListener btStateListener);//蓝牙状态监听(开关监听)
39 |
40 | void setBTScanListener(IBTScanListener btScanListener);//蓝牙搜索监听
41 |
42 | void setBTBoudListener(IBTBoudListener btBoudListener);//蓝牙绑定监听
43 |
44 | void setBTConnectListener(IBTConnectListener btConnectListener);//设置连接监听
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/time_settings_auto_sync.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
13 |
21 |
34 |
35 |
43 |
44 |
52 |
53 |
61 |
67 |
68 |
78 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycler_item_bluetooth.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
18 |
19 |
32 |
33 |
43 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_bluetooth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/icon_bluetooth.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_computer.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/icon_computer.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_headset.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/icon_headset.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_phone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/icon_phone.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_telephone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xhdpi/icon_telephone.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/setting_time_switchon_close_btn.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxxhdpi/setting_time_switchon_close_btn.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/setting_time_switchon_open_btn.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/app/src/main/res/mipmap-xxxhdpi/setting_time_switchon_open_btn.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | BluetoothTest
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/qt/bluetooth/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.qt.bluetooth;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.1'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/qtqt/ClassicBluetooth/11557a2415bd526233218f1591039dfa7059260e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jul 29 11:11:16 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------