Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
BluToysApplication
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chengchong
BluToysApplication
Commits
4e74f69f
Commit
4e74f69f
authored
May 09, 2022
by
韩飞虎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
能运行但是不知道啥版本
parent
297b65d7
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
1000 additions
and
111 deletions
+1000
-111
misc.xml
.idea/misc.xml
+18
-0
build.gradle
FastBleLib/build.gradle
+3
-3
AndroidManifest.xml
..._friendly_merged_manifests/debug/aapt/AndroidManifest.xml
+3
-1
compile-file-map.properties
...emental/packageDebugResources/compile-file-map.properties
+1
-5
manifest-merger-blame-debug-report.txt
...e_blame_file/debug/manifest-merger-blame-debug-report.txt
+13
-9
AndroidManifest.xml
...d/intermediates/merged_manifest/debug/AndroidManifest.xml
+3
-1
classes.jar
...termediates/runtime_library_classes_jar/debug/classes.jar
+0
-0
manifest-merger-debug-report.txt
...leLib/build/outputs/logs/manifest-merger-debug-report.txt
+11
-7
source-classes-mapping.txt
.../tmp/compileDebugJavaWithJavac/source-classes-mapping.txt
+20
-20
AndroidManifest.xml
FastBleLib/src/main/AndroidManifest.xml
+2
-0
build.gradle
app/build.gradle
+38
-38
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+9
-4
MainActivity.java
app/src/main/java/com/stoplight/blu/toys/MainActivity.java
+7
-0
BaseActivity.java
...in/java/com/stoplight/blu/toys/activity/BaseActivity.java
+2
-2
SelectDeviceActivity.java
...com/stoplight/blu/toys/activity/SelectDeviceActivity.java
+20
-1
BleDeviceAdapter.java
...java/com/stoplight/blu/toys/adapter/BleDeviceAdapter.java
+1
-1
BlePlay.java
app/src/main/java/com/stoplight/blu/toys/ble/BlePlay.java
+23
-0
MyService.java
app/src/main/java/com/stoplight/blu/toys/ble/MyService.java
+136
-0
IBlePeripheralCallback.java
.../stoplight/blu/toys/ble/inter/IBlePeripheralCallback.java
+55
-0
BluetoothOpenAndClose.java
...toplight/blu/toys/ble/receiver/BluetoothOpenAndClose.java
+49
-9
BlePeripheralHelper.java
...ava/com/stoplight/blu/toys/utils/BlePeripheralHelper.java
+566
-0
build.gradle
build.gradle
+3
-1
build.gradle
library/build.gradle
+17
-9
No files found.
.idea/misc.xml
View file @
4e74f69f
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<project
version=
"4"
>
<component
name=
"DesignSurface"
>
<option
name=
"filePathToZoomLevelMap"
>
<map>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/drawable/black_thumb.xml"
value=
"0.20989583333333334"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/drawable/gray_thumb.xml"
value=
"0.20989583333333334"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_add_device.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_main.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_music.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_play.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_searching.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_seleclt_device.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_select_timer.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/activity_setting_music.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/music_type.xml"
value=
"0.19375"
/>
<entry
key=
"..\:/程序项目/蓝牙app/BluToysApplication/app/src/main/res/layout/pickerview_custom_time.xml"
value=
"0.19375"
/>
</map>
</option>
</component>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_11"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_11"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
<output
url=
"file://$PROJECT_DIR$/build/classes"
/>
</component>
</component>
...
...
FastBleLib/build.gradle
View file @
4e74f69f
apply
plugin:
'com.android.library'
apply
plugin:
'com.android.library'
android
{
android
{
compileSdkVersion
3
0
compileSdkVersion
3
2
buildToolsVersion
"30.0.2"
buildToolsVersion
'30.0.2'
defaultConfig
{
defaultConfig
{
minSdkVersion
14
minSdkVersion
14
targetSdkVersion
3
0
targetSdkVersion
3
2
}
}
buildTypes
{
buildTypes
{
release
{
release
{
...
...
FastBleLib/build/intermediates/aapt_friendly_merged_manifests/debug/aapt/AndroidManifest.xml
View file @
4e74f69f
...
@@ -4,10 +4,12 @@
...
@@ -4,10 +4,12 @@
<uses-sdk
<uses-sdk
android:minSdkVersion=
"14"
android:minSdkVersion=
"14"
android:targetSdkVersion=
"3
0
"
/>
android:targetSdkVersion=
"3
2
"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_CONNECT"
/>
BLUETOOTH_SCAN
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
...
...
FastBleLib/build/intermediates/incremental/packageDebugResources/compile-file-map.properties
View file @
4e74f69f
<<<<<<<
HEAD
#Sat Dec 18 09:10:09 CST 2021
#Sat Dec 18 08:49:53 CST 2021
=======
#Sat May 22 18:28:17 CST 2021
>>>>>>>
ee610bcfe666c5a8aee5df89f5948dec147a6d1f
FastBleLib/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt
View file @
4e74f69f
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
5 <uses-sdk
5 <uses-sdk
6 android:minSdkVersion="14"
6 android:minSdkVersion="14"
6-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
6-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
7 android:targetSdkVersion="3
0
" />
7 android:targetSdkVersion="3
2
" />
7-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
7-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
8
8
9 <uses-permission android:name="android.permission.BLUETOOTH" />
9 <uses-permission android:name="android.permission.BLUETOOTH" />
...
@@ -14,11 +14,15 @@
...
@@ -14,11 +14,15 @@
10 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
10 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
10-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:5-74
10-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:5-74
10-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:22-71
10-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:22-71
11 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
11 <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
11-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:5-81
11-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:5-76
11-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:22-78
11-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:22-73
12 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
12 BLUETOOTH_SCAN
12-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:7:5-79
13 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
12-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:7:22-76
13-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:8:5-81
13
13-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:8:22-78
14</manifest>
14 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
14-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:9:5-79
14-->D:\程序项目\蓝牙app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:9:22-76
15
16</manifest>
FastBleLib/build/intermediates/merged_manifest/debug/AndroidManifest.xml
View file @
4e74f69f
...
@@ -4,10 +4,12 @@
...
@@ -4,10 +4,12 @@
<uses-sdk
<uses-sdk
android:minSdkVersion=
"14"
android:minSdkVersion=
"14"
android:targetSdkVersion=
"3
0
"
/>
android:targetSdkVersion=
"3
2
"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_CONNECT"
/>
BLUETOOTH_SCAN
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
...
...
FastBleLib/build/intermediates/runtime_library_classes_jar/debug/classes.jar
View file @
4e74f69f
No preview for this file type
FastBleLib/build/outputs/logs/manifest-merger-debug-report.txt
View file @
4e74f69f
-- Merging decision tree log ---
-- Merging decision tree log ---
manifest
manifest
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
9
:12
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
11
:12
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
9
:12
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
11
:12
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
9
:12
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:1:1-
11
:12
package
package
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:2:5-30
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:2:5-30
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
...
@@ -17,14 +17,18 @@ uses-permission#android.permission.BLUETOOTH_ADMIN
...
@@ -17,14 +17,18 @@ uses-permission#android.permission.BLUETOOTH_ADMIN
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:5-74
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:5-74
android:name
android:name
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:22-71
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:5:22-71
uses-permission#android.permission.BLUETOOTH_CONNECT
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:5-76
android:name
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:6:22-73
uses-permission#android.permission.ACCESS_COARSE_LOCATION
uses-permission#android.permission.ACCESS_COARSE_LOCATION
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
6
:5-81
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
8
:5-81
android:name
android:name
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
6
:22-78
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
8
:22-78
uses-permission#android.permission.ACCESS_FINE_LOCATION
uses-permission#android.permission.ACCESS_FINE_LOCATION
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
7
:5-79
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
9
:5-79
android:name
android:name
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
7
:22-76
ADDED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml:
9
:22-76
uses-sdk
uses-sdk
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml reason: use-sdk injection requested
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml reason: use-sdk injection requested
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
INJECTED from D:\Ŀ\app\BluToysApplication\FastBleLib\src\main\AndroidManifest.xml
...
...
FastBleLib/build/tmp/compileDebugJavaWithJavac/source-classes-mapping.txt
View file @
4e74f69f
...
@@ -6,8 +6,8 @@ com/clj/fastble/data/BleWriteState.java
...
@@ -6,8 +6,8 @@ com/clj/fastble/data/BleWriteState.java
com.clj.fastble.data.BleWriteState
com.clj.fastble.data.BleWriteState
com/clj/fastble/data/BleConnectStateParameter.java
com/clj/fastble/data/BleConnectStateParameter.java
com.clj.fastble.data.BleConnectStateParameter
com.clj.fastble.data.BleConnectStateParameter
com/clj/fastble/callback/Ble
Write
Callback.java
com/clj/fastble/callback/Ble
ScanAndConnect
Callback.java
com.clj.fastble.callback.Ble
Write
Callback
com.clj.fastble.callback.Ble
ScanAndConnect
Callback
com/clj/fastble/exception/GattException.java
com/clj/fastble/exception/GattException.java
com.clj.fastble.exception.GattException
com.clj.fastble.exception.GattException
com/clj/fastble/callback/BleRssiCallback.java
com/clj/fastble/callback/BleRssiCallback.java
...
@@ -19,8 +19,8 @@ com/clj/fastble/data/BleScanState.java
...
@@ -19,8 +19,8 @@ com/clj/fastble/data/BleScanState.java
com.clj.fastble.data.BleScanState
com.clj.fastble.data.BleScanState
com/clj/fastble/callback/BleBaseCallback.java
com/clj/fastble/callback/BleBaseCallback.java
com.clj.fastble.callback.BleBaseCallback
com.clj.fastble.callback.BleBaseCallback
com/clj/fastble/
exception/OtherException
.java
com/clj/fastble/
callback/BleGattCallback
.java
com.clj.fastble.
exception.OtherException
com.clj.fastble.
callback.BleGattCallback
com/clj/fastble/utils/BleLog.java
com/clj/fastble/utils/BleLog.java
com.clj.fastble.utils.BleLog
com.clj.fastble.utils.BleLog
com/clj/fastble/callback/BleNotifyCallback.java
com/clj/fastble/callback/BleNotifyCallback.java
...
@@ -37,10 +37,14 @@ com/clj/fastble/exception/ConnectException.java
...
@@ -37,10 +37,14 @@ com/clj/fastble/exception/ConnectException.java
com.clj.fastble.exception.ConnectException
com.clj.fastble.exception.ConnectException
com/clj/fastble/exception/BleException.java
com/clj/fastble/exception/BleException.java
com.clj.fastble.exception.BleException
com.clj.fastble.exception.BleException
com/clj/fastble/exception/TimeoutException.java
com/clj/fastble/scan/BleScanner.java
com.clj.fastble.exception.TimeoutException
com.clj.fastble.scan.BleScanner
com/clj/fastble/data/BleMsg.java
com.clj.fastble.scan.BleScanner$1
com.clj.fastble.data.BleMsg
com.clj.fastble.scan.BleScanner$1$1
com.clj.fastble.scan.BleScanner$BleScannerHolder
com/clj/fastble/BleManager.java
com.clj.fastble.BleManager
com.clj.fastble.BleManager$BleManagerHolder
com/clj/fastble/bluetooth/SplitWriter.java
com/clj/fastble/bluetooth/SplitWriter.java
com.clj.fastble.bluetooth.SplitWriter
com.clj.fastble.bluetooth.SplitWriter
com.clj.fastble.bluetooth.SplitWriter$1
com.clj.fastble.bluetooth.SplitWriter$1
...
@@ -50,21 +54,16 @@ com/clj/fastble/bluetooth/BleBluetooth.java
...
@@ -50,21 +54,16 @@ com/clj/fastble/bluetooth/BleBluetooth.java
com.clj.fastble.bluetooth.BleBluetooth$1
com.clj.fastble.bluetooth.BleBluetooth$1
com.clj.fastble.bluetooth.BleBluetooth$LastState
com.clj.fastble.bluetooth.BleBluetooth$LastState
com.clj.fastble.bluetooth.BleBluetooth$MainHandler
com.clj.fastble.bluetooth.BleBluetooth$MainHandler
com/clj/fastble/callback/BleScanAndConnectCallback.java
com/clj/fastble/data/BleMsg.java
com.clj.fastble.callback.BleScanAndConnectCallback
com.clj.fastble.data.BleMsg
com/clj/fastble/callback/BleWriteCallback.java
com.clj.fastble.callback.BleWriteCallback
com/clj/fastble/bluetooth/MultipleBluetoothController.java
com/clj/fastble/bluetooth/MultipleBluetoothController.java
com.clj.fastble.bluetooth.MultipleBluetoothController
com.clj.fastble.bluetooth.MultipleBluetoothController
com.clj.fastble.bluetooth.MultipleBluetoothController$1
com.clj.fastble.bluetooth.MultipleBluetoothController$1
com/clj/fastble/scan/BleScanner.java
com.clj.fastble.scan.BleScanner
com.clj.fastble.scan.BleScanner$1
com.clj.fastble.scan.BleScanner$1$1
com.clj.fastble.scan.BleScanner$BleScannerHolder
com/clj/fastble/bluetooth/BleConnector.java
com/clj/fastble/bluetooth/BleConnector.java
com.clj.fastble.bluetooth.BleConnector
com.clj.fastble.bluetooth.BleConnector
com.clj.fastble.bluetooth.BleConnector$1
com.clj.fastble.bluetooth.BleConnector$1
com/clj/fastble/callback/BleGattCallback.java
com.clj.fastble.callback.BleGattCallback
com/clj/fastble/scan/BleScanPresenter.java
com/clj/fastble/scan/BleScanPresenter.java
com.clj.fastble.scan.BleScanPresenter
com.clj.fastble.scan.BleScanPresenter
com.clj.fastble.scan.BleScanPresenter$1
com.clj.fastble.scan.BleScanPresenter$1
...
@@ -74,6 +73,10 @@ com/clj/fastble/scan/BleScanPresenter.java
...
@@ -74,6 +73,10 @@ com/clj/fastble/scan/BleScanPresenter.java
com.clj.fastble.scan.BleScanPresenter$5
com.clj.fastble.scan.BleScanPresenter$5
com.clj.fastble.scan.BleScanPresenter$6
com.clj.fastble.scan.BleScanPresenter$6
com.clj.fastble.scan.BleScanPresenter$ScanHandler
com.clj.fastble.scan.BleScanPresenter$ScanHandler
com/clj/fastble/exception/TimeoutException.java
com.clj.fastble.exception.TimeoutException
com/clj/fastble/exception/OtherException.java
com.clj.fastble.exception.OtherException
com/clj/fastble/callback/BleScanPresenterImp.java
com/clj/fastble/callback/BleScanPresenterImp.java
com.clj.fastble.callback.BleScanPresenterImp
com.clj.fastble.callback.BleScanPresenterImp
com/clj/fastble/data/BleDevice.java
com/clj/fastble/data/BleDevice.java
...
@@ -81,6 +84,3 @@ com/clj/fastble/data/BleDevice.java
...
@@ -81,6 +84,3 @@ com/clj/fastble/data/BleDevice.java
com.clj.fastble.data.BleDevice$1
com.clj.fastble.data.BleDevice$1
com/clj/fastble/callback/BleScanCallback.java
com/clj/fastble/callback/BleScanCallback.java
com.clj.fastble.callback.BleScanCallback
com.clj.fastble.callback.BleScanCallback
com/clj/fastble/BleManager.java
com.clj.fastble.BleManager
com.clj.fastble.BleManager$BleManagerHolder
FastBleLib/src/main/AndroidManifest.xml
View file @
4e74f69f
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission
android:name=
"android.permission.BLUETOOTH_CONNECT"
/>
BLUETOOTH_SCAN
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
...
...
app/build.gradle
View file @
4e74f69f
...
@@ -2,17 +2,20 @@ apply plugin: 'com.android.application'
...
@@ -2,17 +2,20 @@ apply plugin: 'com.android.application'
android
{
android
{
compileSdkVersion
30
compileSdkVersion
30
buildToolsVersion
"30.0.2"
buildToolsVersion
'30.0.2'
defaultConfig
{
defaultConfig
{
applicationId
"com.stoplight.blu.toys"
applicationId
"com.stoplight.blu.toys"
minSdkVersion
19
minSdkVersion
19
targetSdkVersion
3
0
targetSdkVersion
3
2
versionCode
2
versionCode
2
versionName
"1.2"
versionName
"1.2"
multiDexEnabled
true
multiDexEnabled
true
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
applicationIdSuffix
'dev'
versionNameSuffix
'dev'
signingConfig
signingConfigs
.
debug
}
}
lintOptions
{
lintOptions
{
...
@@ -21,39 +24,34 @@ android {
...
@@ -21,39 +24,34 @@ android {
// but continue the build even whenerrorsarefound:
// but continue the build even whenerrorsarefound:
abortOnError
false
abortOnError
false
}
}
buildTypes
{
debug
{
applicationIdSuffix
'com.stoplight.blu.toys'
versionNameSuffix
'1.0'
debuggable
true
jniDebuggable
false
renderscriptDebuggable
false
renderscriptOptimLevel
3
signingConfig
signingConfigs
.
debug
minifyEnabled
true
multiDexEnabled
true
}
}
buildTypes
{
buildTypes
{
release
{
release
{
minifyEnabled
false
minifyEnabled
false
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
applicationIdSuffix
'com.stoplight.blu.toys'
}
}
}
}
buildTypes
{
buildTypes
{
debug
{
buildConfigField
"int"
,
"myInt"
,
"0"
buildConfigField
"String"
,
"myStr"
,
"\"hello\""
buildConfigField
"boolean"
,
"LOG_DEBUG"
,
"true"
}
release
{
buildConfigField
"int"
,
"myInt"
,
"1"
buildConfigField
"String"
,
"myStr"
,
"\"world\""
buildConfigField
"boolean"
,
"LOG_DEBUG"
,
"false"
minifyEnabled
false
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
}
}
}
compileOptions
{
compileOptions
{
coreLibraryDesugaringEnabled
true
coreLibraryDesugaringEnabled
true
sourceCompatibility
JavaVersion
.
VERSION_1_8
sourceCompatibility
targetCompatibility
targetCompatibility
JavaVersion
.
VERSION_1_8
targetCompatibility
targetCompatibility
}
}
}
}
...
@@ -66,23 +64,23 @@ dependencies {
...
@@ -66,23 +64,23 @@ dependencies {
testImplementation
'junit:junit:4.12'
testImplementation
'junit:junit:4.1
3.
2'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.3.0'
coreLibraryDesugaring
'com.android.tools:desugar_jdk_libs:1.
0.9
'
coreLibraryDesugaring
'com.android.tools:desugar_jdk_libs:1.
1.5
'
implementation
'com.squareup.okhttp3:okhttp:
4.8.1
'
implementation
'com.squareup.okhttp3:okhttp:
5.0.0-alpha.3
'
implementation
'com.squareup.okhttp3:logging-interceptor:
4.8.1
'
implementation
'com.squareup.okhttp3:logging-interceptor:
5.0.0-alpha.3
'
implementation
'com.google.code.gson:gson:2.8.
6
'
implementation
'com.google.code.gson:gson:2.8.
9
'
// //数据传递
// //数据传递
implementation
'org.greenrobot:eventbus:3.
2.0
'
implementation
'org.greenrobot:eventbus:3.
3.1
'
implementation
'com.hjq:xxpermissions:8.6'
implementation
'com.hjq:xxpermissions:8.6'
implementation
"androidx.multidex:multidex:2.0.0"
implementation
'androidx.multidex:multidex:2.0.1'
implementation
'androidx.recyclerview:recyclerview:1.
1
.0'
implementation
'androidx.recyclerview:recyclerview:1.
2
.0'
...
@@ -99,12 +97,12 @@ dependencies {
...
@@ -99,12 +97,12 @@ dependencies {
//底部导航
//底部导航
implementation
'com.roughike:bottom-bar:2.3.1'
implementation
'com.roughike:bottom-bar:2.3.1'
// //findViewById
// //findViewById
implementation
'com.jakewharton:butterknife:10.2.
1
'
implementation
'com.jakewharton:butterknife:10.2.
3
'
annotationProcessor
'com.jakewharton:butterknife-compiler:10.2.
1
'
annotationProcessor
'com.jakewharton:butterknife-compiler:10.2.
3
'
//跑马灯
//跑马灯
implementation
'com.superluo:textbannerview:1.0.5'
//最新版本
implementation
'com.superluo:textbannerview:1.0.5'
//最新版本
//design
//design
implementation
"com.google.android.material:material:1.2.1"
implementation
'com.google.android.material:material:1.3.0'
//万能适配器
//万能适配器
// implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
// implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
//二维码
//二维码
...
@@ -120,10 +118,11 @@ dependencies {
...
@@ -120,10 +118,11 @@ dependencies {
// implementation 'com.wx.wheelview:wheelview:1.3.3'
// implementation 'com.wx.wheelview:wheelview:1.3.3'
//工具类
//工具类
implementation
'com.blankj:utilcodex:1.
28
.0'
implementation
'com.blankj:utilcodex:1.
31
.0'
implementation
'com.alibaba:fastjson:1.2.7
3
'
implementation
'com.alibaba:fastjson:1.2.7
8
'
implementation
'androidx.multidex:multidex:2.0.
0
'
implementation
'androidx.multidex:multidex:2.0.
1
'
implementation
'com.orhanobut:logger:
1.15
'
implementation
'com.orhanobut:logger:
2.2.0
'
implementation
'top.zibin:Luban:1.1.8'
implementation
'top.zibin:Luban:1.1.8'
implementation
'com.yhd.hdbluetooth:hdbluetooth:1.0.0'
}
}
\ No newline at end of file
app/src/main/AndroidManifest.xml
View file @
4e74f69f
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
xmlns:tools=
"http://schemas.android.com/tools"
xmlns:tools=
"http://schemas.android.com/tools"
package=
"com.stoplight.blu.toys"
>
package=
"com.stoplight.blu.toys"
>
<uses-permission
android:name=
"android.permission.BLUETOOTH_ADVERTISE"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
...
@@ -23,8 +24,7 @@
...
@@ -23,8 +24,7 @@
<uses-permission
android:name=
"android.permission.VIBRATE"
/>
<uses-permission
android:name=
"android.permission.VIBRATE"
/>
<uses-permission
android:name=
"android.permission.FLASHLIGHT"
/>
<uses-permission
android:name=
"android.permission.FLASHLIGHT"
/>
<uses-feature
android:name=
"android.hardware.camera"
/>
<uses-feature
android:name=
"android.hardware.camera.autofocus"
/>
<uses-permission
android:name=
"android.permission.WAKE_LOCK"
/>
<uses-permission
android:name=
"android.permission.WAKE_LOCK"
/>
...
@@ -52,15 +52,20 @@
...
@@ -52,15 +52,20 @@
<activity
android:name=
"com.stoplight.blu.toys.activity.SelectDeviceActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.activity.SelectDeviceActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.activity.SearchingActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.activity.SearchingActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.activity.AddDeviceActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.activity.AddDeviceActivity"
/>
<activity
android:name=
"com.stoplight.blu.toys.MainActivity"
>
<activity
android:name=
"com.stoplight.blu.toys.MainActivity"
android:exported=
"true"
>
<intent-filter>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
<action
android:name=
"android.intent.action.MAIN"
/>
<category
android:name=
"android.intent.category.LAUNCHER"
/>
<category
android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</intent-filter>
</activity>
</activity>
<service
android:name=
"com.stoplight.blu.toys.ble.MyService"
android:enabled=
"true"
android:exported=
"true"
></service>
<receiver
android:name=
"com.stoplight.blu.toys.ble.receiver.BluetoothOpenAndClose"
>
<receiver
android:name=
"com.stoplight.blu.toys.ble.receiver.BluetoothOpenAndClose"
android:exported=
"false"
>
<intent-filter>
<intent-filter>
<action
android:name=
"android.bluetooth.adapter.action.STATE_CHANGED"
/>
<action
android:name=
"android.bluetooth.adapter.action.STATE_CHANGED"
/>
</intent-filter>
</intent-filter>
...
...
app/src/main/java/com/stoplight/blu/toys/MainActivity.java
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
;
package
com
.
stoplight
.
blu
.
toys
;
import
android.content.Intent
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.os.Handler
;
...
@@ -7,6 +8,7 @@ import com.stoplight.blu.toys.R;
...
@@ -7,6 +8,7 @@ import com.stoplight.blu.toys.R;
import
com.stoplight.blu.toys.activity.AddDeviceActivity
;
import
com.stoplight.blu.toys.activity.AddDeviceActivity
;
import
com.stoplight.blu.toys.activity.BaseActivity
;
import
com.stoplight.blu.toys.activity.BaseActivity
;
import
com.stoplight.blu.toys.app.AppManager
;
import
com.stoplight.blu.toys.app.AppManager
;
import
com.stoplight.blu.toys.ble.MyService
;
public
class
MainActivity
extends
BaseActivity
{
public
class
MainActivity
extends
BaseActivity
{
...
@@ -19,6 +21,11 @@ public class MainActivity extends BaseActivity {
...
@@ -19,6 +21,11 @@ public class MainActivity extends BaseActivity {
@Override
@Override
public
void
init
(
Bundle
savedInstanceState
)
{
public
void
init
(
Bundle
savedInstanceState
)
{
new
Handler
().
postDelayed
(()
->
{
new
Handler
().
postDelayed
(()
->
{
Intent
intent
=
new
Intent
(
this
,
MyService
.
class
);
this
.
startService
(
intent
);
goActivity
(
AddDeviceActivity
.
class
);
goActivity
(
AddDeviceActivity
.
class
);
AppManager
.
getInstance
().
finishActivity
();
AppManager
.
getInstance
().
finishActivity
();
},
1000
);
//延时1s执行
},
1000
);
//延时1s执行
...
...
app/src/main/java/com/stoplight/blu/toys/activity/BaseActivity.java
View file @
4e74f69f
...
@@ -30,7 +30,7 @@ public abstract class BaseActivity extends AppCompatActivity {
...
@@ -30,7 +30,7 @@ public abstract class BaseActivity extends AppCompatActivity {
protected
static
final
int
PERMISSION_REQUEST
=
1001
;
protected
static
final
int
PERMISSION_REQUEST
=
1001
;
private
List
<
String
>
permissionsList
=
new
ArrayList
<>();
private
List
<
String
>
permissionsList
=
new
ArrayList
<>();
private
String
[]
permissions
=
new
String
[]{
Manifest
.
permission
.
CAMERA
,
private
String
[]
permissions
=
new
String
[]{
Manifest
.
permission
.
MOUNT_UNMOUNT_FILESYSTEMS
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
,
Manifest
.
permission
.
MOUNT_UNMOUNT_FILESYSTEMS
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
,
Manifest
.
permission
.
READ_EXTERNAL_STORAGE
};
Manifest
.
permission
.
READ_EXTERNAL_STORAGE
};
...
@@ -47,7 +47,7 @@ public abstract class BaseActivity extends AppCompatActivity {
...
@@ -47,7 +47,7 @@ public abstract class BaseActivity extends AppCompatActivity {
private
void
checkPermission
()
{
private
void
checkPermission
()
{
XXPermissions
.
with
(
this
)
// 申请安装包权限
XXPermissions
.
with
(
this
)
// 申请安装包权限
.
permission
(
Permission
.
CAMERA
,
Permission
.
ACCESS_FINE_LOCATION
,
Permission
.
ACCESS_FINE_LOCATION
)
// 申请多个权限
.
permission
(
Permission
.
ACCESS_FINE_LOCATION
,
Permission
.
ACCESS_FINE_LOCATION
)
// 申请多个权限
.
request
(
new
OnPermission
()
{
.
request
(
new
OnPermission
()
{
@Override
@Override
public
void
hasPermission
(
List
<
String
>
granted
,
boolean
all
)
{
public
void
hasPermission
(
List
<
String
>
granted
,
boolean
all
)
{
...
...
app/src/main/java/com/stoplight/blu/toys/activity/SelectDeviceActivity.java
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
activity
;
package
com
.
stoplight
.
blu
.
toys
.
activity
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.le.AdvertiseSettings
;
import
android.content.Intent
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.text.TextUtils
;
import
android.text.TextUtils
;
import
android.view.View
;
import
android.view.View
;
...
@@ -12,9 +16,13 @@ import com.stoplight.blu.toys.R;
...
@@ -12,9 +16,13 @@ import com.stoplight.blu.toys.R;
import
com.stoplight.blu.toys.adapter.BleDeviceAdapter
;
import
com.stoplight.blu.toys.adapter.BleDeviceAdapter
;
import
com.stoplight.blu.toys.app.AppManager
;
import
com.stoplight.blu.toys.app.AppManager
;
import
com.stoplight.blu.toys.ble.BlePlay
;
import
com.stoplight.blu.toys.ble.BlePlay
;
import
com.stoplight.blu.toys.ble.MyService
;
import
com.stoplight.blu.toys.ble.inter.IBlePeripheralCallback
;
import
com.stoplight.blu.toys.utils.BlePeripheralHelper
;
import
com.stoplight.blu.toys.view.TypefaceTextView
;
import
com.stoplight.blu.toys.view.TypefaceTextView
;
import
org.greenrobot.eventbus.EventBus
;
import
org.greenrobot.eventbus.EventBus
;
import
org.greenrobot.eventbus.Logger
;
import
org.greenrobot.eventbus.Subscribe
;
import
org.greenrobot.eventbus.Subscribe
;
import
org.greenrobot.eventbus.ThreadMode
;
import
org.greenrobot.eventbus.ThreadMode
;
...
@@ -53,8 +61,19 @@ public class SelectDeviceActivity extends BaseActivity {
...
@@ -53,8 +61,19 @@ public class SelectDeviceActivity extends BaseActivity {
@Override
@Override
public
void
initData
()
{
public
void
initData
()
{
bleDeviceAdapter
.
setOnItemClickListener
((
adapter
,
view
,
position
)
->
{
bleDeviceAdapter
.
setOnItemClickListener
((
adapter
,
view
,
position
)
->
{
BleDevice
bleDevice
=
(
BleDevice
)
adapter
.
getItem
(
position
);
BleDevice
bleDevice
=
(
BleDevice
)
adapter
.
getItem
(
position
);
BlePlay
.
getInstance
().
connectedBleDevice
(
bleDevice
);
BlePlay
.
getInstance
().
connectedBleDevice
(
bleDevice
);
System
.
out
.
println
(
bleDevice
.
getShowName
());
//Intent intent = new Intent(SelectDeviceActivity.this, MyService.class);
//startService(intent);
});
});
showBleDevices
();
showBleDevices
();
}
}
...
...
app/src/main/java/com/stoplight/blu/toys/adapter/BleDeviceAdapter.java
View file @
4e74f69f
...
@@ -22,7 +22,7 @@ public class BleDeviceAdapter extends BaseQuickAdapter<BleDevice, BaseViewHolder
...
@@ -22,7 +22,7 @@ public class BleDeviceAdapter extends BaseQuickAdapter<BleDevice, BaseViewHolder
@Override
@Override
protected
void
convert
(
BaseViewHolder
helper
,
BleDevice
item
)
{
protected
void
convert
(
BaseViewHolder
helper
,
BleDevice
item
)
{
String
deviceName
=
item
.
getName
();
String
deviceName
=
item
.
getName
();
if
((!
TextUtils
.
isEmpty
(
deviceName
)
&&
deviceName
.
contains
(
"Traffic"
))||!
TextUtils
.
isEmpty
(
deviceName
)
&&
deviceName
.
contains
(
"
blector
"
))
{
if
((!
TextUtils
.
isEmpty
(
deviceName
)
&&
deviceName
.
contains
(
"Traffic"
))||!
TextUtils
.
isEmpty
(
deviceName
)
&&
deviceName
.
contains
(
"
Traffic
"
))
{
helper
.
setText
(
R
.
id
.
dv_name
,
item
.
getShowName
()
+
""
);
helper
.
setText
(
R
.
id
.
dv_name
,
item
.
getShowName
()
+
""
);
}
}
}
}
...
...
app/src/main/java/com/stoplight/blu/toys/ble/BlePlay.java
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
ble
;
package
com
.
stoplight
.
blu
.
toys
.
ble
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGatt
;
import
android.bluetooth.BluetoothGatt
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.BluetoothGattService
;
import
android.bluetooth.BluetoothGattService
;
import
android.util.Log
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.clj.fastble.BleManager
;
import
com.clj.fastble.BleManager
;
...
@@ -189,6 +191,27 @@ public class BlePlay {
...
@@ -189,6 +191,27 @@ public class BlePlay {
}
}
/* public boolean pair(BluetoothDevice device) {
String TAG="pair";
// Stops the discovery and then creates the pairing.
if (bluetooth.isDiscovering()) {
Log.d(TAG, "Bluetooth cancelling discovery.");
bluetooth.cancelDiscovery();
}
Log.d(TAG, "Bluetooth bonding with device: " + device.getName());
boolean outcome = device.createBond();
Log.d(TAG, "Bounding outcome : " + outcome);
// If the outcome is true, we are bounding with this device.
if (outcome == true) {
this.boundingDevice = device;
}
return outcome;
}*/
/**
/**
* 暂停
* 暂停
*/
*/
...
...
app/src/main/java/com/stoplight/blu/toys/ble/MyService.java
0 → 100644
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
ble
;
import
android.app.IntentService
;
import
android.app.Notification
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.le.AdvertiseSettings
;
import
android.content.BroadcastReceiver
;
import
android.content.ComponentName
;
import
android.content.Intent
;
import
android.content.IntentFilter
;
import
android.os.IBinder
;
import
androidx.annotation.Nullable
;
import
com.stoplight.blu.toys.ble.inter.IBlePeripheralCallback
;
import
com.stoplight.blu.toys.ble.receiver.BluetoothOpenAndClose
;
import
com.stoplight.blu.toys.utils.BlePeripheralHelper
;
public
class
MyService
extends
Service
{
IntentFilter
connectedFilter
=
new
IntentFilter
(
BluetoothAdapter
.
ACTION_STATE_CHANGED
);
public
MyService
()
{
System
.
out
.
println
(
"--------------------MyService"
);
}
public
BlePeripheralHelper
mBlePeripheralHelper
;
private
BluetoothOpenAndClose
bluetoothOpenAndClose
;
@Override
public
void
onCreate
()
{
// TODO Auto-generated method stub
mBlePeripheralHelper
=
BlePeripheralHelper
.
getInstance
(
this
);
connectedFilter
=
new
IntentFilter
(
BluetoothAdapter
.
ACTION_STATE_CHANGED
);
bluetoothOpenAndClose
=
new
BluetoothOpenAndClose
();
registerReceiver
(
bluetoothOpenAndClose
,
connectedFilter
);
System
.
out
.
println
(
"--------------------onCreate"
);
startBle
();
super
.
onCreate
();
}
@Override
public
ComponentName
startService
(
Intent
service
)
{
startBle
();
System
.
out
.
println
(
"--------------------startService"
);
return
super
.
startService
(
service
);
}
@Override
public
int
onStartCommand
(
@Nullable
Intent
intent
,
int
flags
,
int
startId
)
{
System
.
out
.
println
(
"--------------------onStartCommand"
);
return
super
.
onStartCommand
(
intent
,
flags
,
startId
);
}
@Override
public
void
onDestroy
()
{
// unregisterReceiver(bluetoothOpenAndClose);
// registerReceiver(bluetoothOpenAndClose,connectedFilter);
mBlePeripheralHelper
=
BlePeripheralHelper
.
getInstance
(
this
);
System
.
out
.
println
(
"--------------------onDestroy"
);
Intent
localIntent
=
new
Intent
(
this
,
MyService
.
class
);
this
.
startService
(
localIntent
);
}
@Nullable
@Override
public
IBinder
onBind
(
Intent
intent
)
{
System
.
out
.
println
(
"--------------------intent"
);
return
null
;
}
public
void
startBle
(){
if
(
mBlePeripheralHelper
.
isBleEnabled
()){
// 设置回调
System
.
out
.
println
(
"--------------------startBle"
);
mBlePeripheralHelper
.
setBlePeripheralCallback
(
new
IBlePeripheralCallback
()
{
//连接状态回调
@Override
public
void
onConnectionStateChange
(
BluetoothDevice
bluetoothDevice
,
int
i
,
int
i1
)
{
System
.
out
.
println
(
bluetoothDevice
);
}
//开启广播成功回调
@Override
public
void
onStartAbSuccess
(
AdvertiseSettings
advertiseSettings
)
{
System
.
out
.
println
(
advertiseSettings
);
}
//开启广失败的功回调
@Override
public
void
onStartAbFailure
(
int
i
)
{
System
.
out
.
println
(
i
);
}
//收到BLE数据回调
@Override
public
void
onReceiveNewBytes
(
BluetoothDevice
bluetoothDevice
,
int
i
,
BluetoothGattCharacteristic
bluetoothGattCharacteristic
,
byte
[]
bytes
)
{
System
.
out
.
println
(
bluetoothDevice
);
}
//发送BLE数据回调
@Override
public
void
onWriteBytesAndStatus
(
boolean
b
,
byte
[]
bytes
)
{
System
.
out
.
println
(
bytes
);
}
});
// 初始化广播
mBlePeripheralHelper
.
initGATTServer
();
}
}
}
app/src/main/java/com/stoplight/blu/toys/ble/inter/IBlePeripheralCallback.java
0 → 100644
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
ble
.
inter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.le.AdvertiseSettings
;
/**
* Created by Roy.lee
* On 2021/6/27
* Email: 631934797@qq.com
* Description:
*/
public
interface
IBlePeripheralCallback
{
/**
* Connection status callback
*
* @param device
* @param status
* @param newState
*/
void
onConnectionStateChange
(
BluetoothDevice
device
,
int
status
,
int
newState
);
/**
* Advertise success callback
*
* @param settingsInEffect
*/
void
onStartAbSuccess
(
AdvertiseSettings
settingsInEffect
);
/**
* Advertise failure callback
*
* @param errorCode
*/
void
onStartAbFailure
(
int
errorCode
);
/**
* Receive new data callback
*
* @param device
* @param requestId
* @param characteristic
*/
void
onReceiveNewBytes
(
BluetoothDevice
device
,
int
requestId
,
BluetoothGattCharacteristic
characteristic
,
byte
[]
reqeustBytes
);
/**
* Send data status callback
* @param status
* @param bytes
*/
void
onWriteBytesAndStatus
(
boolean
status
,
byte
[]
bytes
);
}
app/src/main/java/com/stoplight/blu/toys/ble/receiver/BluetoothOpenAndClose.java
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
ble
.
receiver
;
package
com
.
stoplight
.
blu
.
toys
.
ble
.
receiver
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.le.AdvertiseSettings
;
import
android.content.BroadcastReceiver
;
import
android.content.BroadcastReceiver
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
...
@@ -9,6 +12,9 @@ import com.blankj.utilcode.util.LogUtils;
...
@@ -9,6 +12,9 @@ import com.blankj.utilcode.util.LogUtils;
import
com.clj.fastble.BleManager
;
import
com.clj.fastble.BleManager
;
import
com.clj.fastble.callback.BleScanCallback
;
import
com.clj.fastble.callback.BleScanCallback
;
import
com.clj.fastble.data.BleDevice
;
import
com.clj.fastble.data.BleDevice
;
import
com.stoplight.blu.toys.ble.MyService
;
import
com.stoplight.blu.toys.ble.inter.IBlePeripheralCallback
;
import
com.stoplight.blu.toys.utils.BlePeripheralHelper
;
import
java.util.List
;
import
java.util.List
;
...
@@ -36,19 +42,48 @@ public class BluetoothOpenAndClose extends BroadcastReceiver {
...
@@ -36,19 +42,48 @@ public class BluetoothOpenAndClose extends BroadcastReceiver {
break
;
break
;
case
BluetoothAdapter
.
STATE_ON
:
case
BluetoothAdapter
.
STATE_ON
:
LogUtils
.
d
(
"手机蓝牙开启"
);
LogUtils
.
d
(
"手机蓝牙开启"
);
//
//myService.startBle();
// BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
BlePeripheralHelper
mBlePeripheralHelper
=
BlePeripheralHelper
.
getInstance
(
context
);
// .setServiceUuids(serviceUuids)
// //.setDeviceName(true, names)
// //.setDeviceMac(mac)
// .setAutoConnect(false)
// .setScanTimeOut(10000)
// .build();
//BleManager.getInstance().initScanRule(scanRuleConfig);
if
(
mBlePeripheralHelper
.
isBleEnabled
()){
// 设置回调
System
.
out
.
println
(
"--------------------startBle"
);
mBlePeripheralHelper
.
setBlePeripheralCallback
(
new
IBlePeripheralCallback
()
{
//连接状态回调
@Override
public
void
onConnectionStateChange
(
BluetoothDevice
bluetoothDevice
,
int
i
,
int
i1
)
{
System
.
out
.
println
(
bluetoothDevice
);
}
//开启广播成功回调
@Override
public
void
onStartAbSuccess
(
AdvertiseSettings
advertiseSettings
)
{
System
.
out
.
println
(
advertiseSettings
);
}
//开启广失败的功回调
@Override
public
void
onStartAbFailure
(
int
i
)
{
System
.
out
.
println
(
i
);
}
//收到BLE数据回调
@Override
public
void
onReceiveNewBytes
(
BluetoothDevice
bluetoothDevice
,
int
i
,
BluetoothGattCharacteristic
bluetoothGattCharacteristic
,
byte
[]
bytes
)
{
System
.
out
.
println
(
bluetoothDevice
);
}
//发送BLE数据回调
@Override
public
void
onWriteBytesAndStatus
(
boolean
b
,
byte
[]
bytes
)
{
System
.
out
.
println
(
bytes
);
}
});
// 初始化广播
mBlePeripheralHelper
.
initGATTServer
();
}
break
;
break
;
...
@@ -58,4 +93,9 @@ public class BluetoothOpenAndClose extends BroadcastReceiver {
...
@@ -58,4 +93,9 @@ public class BluetoothOpenAndClose extends BroadcastReceiver {
}
}
}
}
}
}
public
BluetoothOpenAndClose
(){
}
}
}
app/src/main/java/com/stoplight/blu/toys/utils/BlePeripheralHelper.java
0 → 100644
View file @
4e74f69f
package
com
.
stoplight
.
blu
.
toys
.
utils
;
import
android.annotation.SuppressLint
;
import
android.bluetooth.BluetoothA2dp
;
import
android.bluetooth.BluetoothAdapter
;
import
android.bluetooth.BluetoothDevice
;
import
android.bluetooth.BluetoothGatt
;
import
android.bluetooth.BluetoothGattCharacteristic
;
import
android.bluetooth.BluetoothGattDescriptor
;
import
android.bluetooth.BluetoothGattServer
;
import
android.bluetooth.BluetoothGattServerCallback
;
import
android.bluetooth.BluetoothGattService
;
import
android.bluetooth.BluetoothManager
;
import
android.bluetooth.le.AdvertiseCallback
;
import
android.bluetooth.le.AdvertiseData
;
import
android.bluetooth.le.AdvertiseSettings
;
import
android.bluetooth.le.BluetoothLeAdvertiser
;
import
android.content.BroadcastReceiver
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.IntentFilter
;
import
android.os.ParcelUuid
;
import
android.os.SystemClock
;
import
android.util.Log
;
import
com.stoplight.blu.toys.ble.inter.IBlePeripheralCallback
;
import
java.util.Arrays
;
import
java.util.UUID
;
/**
* Created by Roy.lee
* On 2021/6/27
* Email: 631934797@qq.com
* Description:
*/
public
class
BlePeripheralHelper
{
private
static
final
String
TAG
=
"@@@ ===> "
+
BlePeripheralHelper
.
class
.
getSimpleName
();
// 这里的参数可自行定义
private
static
String
BLE_NAME
=
"SmartBox"
;
private
static
final
UUID
UUID_SERVER
=
UUID
.
fromString
(
"0000fff0-0000-1000-8000-00805f9b34fb"
);
private
static
final
UUID
UUID_CHARREAD
=
UUID
.
fromString
(
"0000fff1-0000-1000-8000-00805f9b34fb"
);
private
static
final
UUID
UUID_CHARWRITE
=
UUID
.
fromString
(
"0000fff2-0000-1000-8000-00805f9b34fb"
);
private
static
final
UUID
UUID_DESCRIPTOR
=
UUID
.
fromString
(
"00002902-0000-1000-8000-00805f9b34fb"
);
private
BluetoothGattCharacteristic
mCharacteristicRead
;
//BluetoothHelper
private
static
BlePeripheralHelper
mBlePeripheralHelper
;
private
BluetoothManager
mBluetoothManager
;
private
BluetoothAdapter
mBluetoothAdapter
;
private
static
Context
mContext
;
private
BluetoothLeAdvertiser
mBluetoothLeAdvertiser
;
private
BluetoothGattServer
mBluetoothGattServer
;
private
IBlePeripheralCallback
mBlePeripheralCallback
;
private
BlePeripheralHelper
()
{
initPeripheral
(
mContext
);
}
/**
* 获取BleController实例对象
* @return
*/
public
synchronized
static
BlePeripheralHelper
getInstance
(
Context
context
)
{
mContext
=
context
;
if
(
null
==
mBlePeripheralHelper
)
{
mBlePeripheralHelper
=
new
BlePeripheralHelper
();
}
return
mBlePeripheralHelper
;
}
/**
* 初始化BLE相关参数
*
* @param context
*/
private
void
initPeripheral
(
Context
context
)
{
// 初始化mBluetoothManager
mBluetoothManager
=
getBleManager
(
mContext
);
// 初始化mBluetoothAdapter
mBluetoothAdapter
=
getBluetoothAdapter
();
if
(
isBleEnabled
()){
mBluetoothLeAdvertiser
=
getBluetoothLeAdvertiser
();
}
else
{
if
(
setBleEnabled
(
true
)){
SystemClock
.
sleep
(
1000
);
mBluetoothLeAdvertiser
=
getBluetoothLeAdvertiser
();
}
}
if
(
mBluetoothLeAdvertiser
==
null
){
Log
.
e
(
TAG
,
"== The device not support peripheral =="
);
}
// 注册BLE的状态变化广播
context
.
registerReceiver
(
mBluetoothReceiver
,
makeGattUpdateIntentFilter
());
}
/**
* 初始化BLE广播
*/
public
void
initGATTServer
()
{
initGATTServer
(
null
);
}
/**
* 初始化BLE广播并设置BEL_NAME
* @param bleName
*/
@SuppressLint
(
"InlinedApi"
)
public
void
initGATTServer
(
String
bleName
)
{
if
(
bleName
!=
null
&&
!
bleName
.
isEmpty
())
BLE_NAME
=
bleName
;
AdvertiseSettings
settings
=
new
AdvertiseSettings
.
Builder
()
.
setConnectable
(
true
)
.
setTimeout
(
0
)
//超时时间
.
setAdvertiseMode
(
AdvertiseSettings
.
ADVERTISE_MODE_BALANCED
)
//广播模式
.
setTxPowerLevel
(
AdvertiseSettings
.
ADVERTISE_TX_POWER_HIGH
)
//发射功率
.
build
();
AdvertiseData
advertiseData
=
new
AdvertiseData
.
Builder
()
.
setIncludeDeviceName
(
true
)
//是否在广播中携带设备的名称
.
setIncludeTxPowerLevel
(
true
)
//是否在广播中携带信号强度
.
build
();
AdvertiseData
scanResponseData
=
new
AdvertiseData
.
Builder
()
.
addServiceUuid
(
new
ParcelUuid
(
UUID_SERVER
))
.
setIncludeTxPowerLevel
(
true
)
.
build
();
//设置BLE设备的名称
mBluetoothAdapter
.
setName
(
BLE_NAME
);
/**
* 开启广播的结果callback
*/
AdvertiseCallback
mAdCallback
=
new
AdvertiseCallback
()
{
@Override
public
void
onStartSuccess
(
AdvertiseSettings
settingsInEffect
)
{
Log
.
d
(
TAG
,
"BLE advertisement added successfully"
);
// TODO 初始化服务
initServices
();
if
(
mBlePeripheralCallback
!=
null
)
mBlePeripheralCallback
.
onStartAbSuccess
(
settingsInEffect
);
}
@Override
public
void
onStartFailure
(
int
errorCode
)
{
Log
.
e
(
TAG
,
"Failed to add BLE advertisement, reason: "
+
errorCode
);
if
(
mBlePeripheralCallback
!=
null
)
mBlePeripheralCallback
.
onStartAbFailure
(
errorCode
);
}
};
//开启广播
if
(
mBluetoothLeAdvertiser
!=
null
)
mBluetoothLeAdvertiser
.
startAdvertising
(
settings
,
advertiseData
,
scanResponseData
,
mAdCallback
);
}
/**
* 初始化广播服务参数
*/
private
void
initServices
()
{
mBluetoothGattServer
=
getBluetoothGattServer
(
mBluetoothGattServerCallback
);
BluetoothGattService
service
=
new
BluetoothGattService
(
UUID_SERVER
,
BluetoothGattService
.
SERVICE_TYPE_PRIMARY
);
//add a read characteristic.
mCharacteristicRead
=
new
BluetoothGattCharacteristic
(
UUID_CHARREAD
,
BluetoothGattCharacteristic
.
PROPERTY_READ
,
BluetoothGattCharacteristic
.
PERMISSION_READ
);
//add a descriptor
BluetoothGattDescriptor
descriptor
=
new
BluetoothGattDescriptor
(
UUID_DESCRIPTOR
,
BluetoothGattCharacteristic
.
PERMISSION_WRITE
);
mCharacteristicRead
.
addDescriptor
(
descriptor
);
service
.
addCharacteristic
(
mCharacteristicRead
);
//add a write characteristic.
BluetoothGattCharacteristic
characteristicWrite
=
new
BluetoothGattCharacteristic
(
UUID_CHARWRITE
,
BluetoothGattCharacteristic
.
PROPERTY_WRITE
|
BluetoothGattCharacteristic
.
PROPERTY_READ
|
BluetoothGattCharacteristic
.
PROPERTY_NOTIFY
,
BluetoothGattCharacteristic
.
PERMISSION_WRITE
);
service
.
addCharacteristic
(
characteristicWrite
);
mBluetoothGattServer
.
addService
(
service
);
Log
.
e
(
TAG
,
"2. initServices ok"
);
}
/**
* 获取BluetoothLeAdvertiser
*
*/
@SuppressLint
(
"InlinedApi"
)
private
BluetoothLeAdvertiser
getBluetoothLeAdvertiser
()
{
return
mBluetoothAdapter
==
null
?
null
:
mBluetoothAdapter
.
getBluetoothLeAdvertiser
();
}
/**
* 获取BluetoothGattServer
*
*/
private
BluetoothGattServer
getBluetoothGattServer
(
BluetoothGattServerCallback
bluetoothGattServerCallback
){
return
mBluetoothManager
==
null
?
null
:
mBluetoothManager
.
openGattServer
(
mContext
,
bluetoothGattServerCallback
);
}
/**
* 获取BluetoothManager
*
* @param context
* @return
*/
private
BluetoothManager
getBleManager
(
Context
context
)
{
return
context
==
null
?
null
:
(
BluetoothManager
)
context
.
getSystemService
(
Context
.
BLUETOOTH_SERVICE
);
}
/**
* 获取BluetoothAdapter
*
* @return
*/
private
BluetoothAdapter
getBluetoothAdapter
(){
return
mBluetoothManager
==
null
?
null
:
mBluetoothManager
.
getAdapter
();
}
/**
*
* 开启/关闭BLE
*
* @param enabled
* @return
*/
public
boolean
setBleEnabled
(
boolean
enabled
){
if
(
enabled
){
return
mBluetoothAdapter
==
null
?
false
:
mBluetoothAdapter
.
enable
();
}
else
{
return
mBluetoothAdapter
==
null
?
false
:
mBluetoothAdapter
.
disable
();
}
}
/**
* 获取BLE状态
* @return
*/
public
boolean
isBleEnabled
(){
return
mBluetoothAdapter
==
null
?
false
:
mBluetoothAdapter
.
isEnabled
();
}
// ========================================================BluetoothGattServerCallback=========================================
/**
* 服务事件的回调
*/
private
BluetoothGattServerCallback
mBluetoothGattServerCallback
=
new
BluetoothGattServerCallback
()
{
/**
* 1.连接状态发生变化时
* @param device
* @param status
* @param newState
*/
@Override
public
void
onConnectionStateChange
(
BluetoothDevice
device
,
int
status
,
int
newState
)
{
Log
.
w
(
TAG
,
"onConnectionStateChange [ status : "
+
status
+
" | newState : "
+
newState
+
"]"
);
Log
.
e
(
TAG
,
String
.
format
(
"1.onConnectionStateChange:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"1.onConnectionStateChange:status = %s, newState =%s "
,
status
,
newState
));
super
.
onConnectionStateChange
(
device
,
status
,
newState
);
if
(
mBlePeripheralCallback
!=
null
)
mBlePeripheralCallback
.
onConnectionStateChange
(
device
,
status
,
newState
);
}
@Override
public
void
onServiceAdded
(
int
status
,
BluetoothGattService
service
)
{
super
.
onServiceAdded
(
status
,
service
);
Log
.
e
(
TAG
,
String
.
format
(
"onServiceAdded:status = %s"
,
status
));
}
@Override
public
void
onCharacteristicReadRequest
(
BluetoothDevice
device
,
int
requestId
,
int
offset
,
BluetoothGattCharacteristic
characteristic
)
{
Log
.
e
(
TAG
,
String
.
format
(
"onCharacteristicReadRequest:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"onCharacteristicReadRequest:requestId = %s, offset = %s"
,
requestId
,
offset
));
mBluetoothGattServer
.
sendResponse
(
device
,
requestId
,
BluetoothGatt
.
GATT_SUCCESS
,
offset
,
characteristic
.
getValue
());
// super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
}
/**
* 3. onCharacteristicWriteRequest,接收具体的字节
* @param device
* @param requestId
* @param characteristic
* @param preparedWrite
* @param responseNeeded
* @param offset
* @param requestBytes
*/
@Override
public
void
onCharacteristicWriteRequest
(
BluetoothDevice
device
,
int
requestId
,
BluetoothGattCharacteristic
characteristic
,
boolean
preparedWrite
,
boolean
responseNeeded
,
int
offset
,
byte
[]
requestBytes
)
{
Log
.
e
(
TAG
,
String
.
format
(
"3.onCharacteristicWriteRequest:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"3.onCharacteristicWriteRequest:requestId = %s, preparedWrite=%s, responseNeeded=%s, offset=%s, value=%s"
,
requestId
,
preparedWrite
,
responseNeeded
,
offset
,
requestBytes
));
mBluetoothGattServer
.
sendResponse
(
device
,
requestId
,
BluetoothGatt
.
GATT_SUCCESS
,
offset
,
requestBytes
);
// TODO 4.处理响应内容
onResponseToClient
(
device
,
requestId
,
characteristic
,
requestBytes
);
}
/**
* 2.描述被写入时,在这里执行 bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS... 收,触发 onCharacteristicWriteRequest
* @param device
* @param requestId
* @param descriptor
* @param preparedWrite
* @param responseNeeded
* @param offset
* @param value
*/
@Override
public
void
onDescriptorWriteRequest
(
BluetoothDevice
device
,
int
requestId
,
BluetoothGattDescriptor
descriptor
,
boolean
preparedWrite
,
boolean
responseNeeded
,
int
offset
,
byte
[]
value
)
{
Log
.
e
(
TAG
,
String
.
format
(
"2.onDescriptorWriteRequest:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"2.onDescriptorWriteRequest:requestId = %s, preparedWrite = %s, responseNeeded = %s, offset = %s, value = %s,"
,
requestId
,
preparedWrite
,
responseNeeded
,
offset
,
value
));
// now tell the connected device that this was all successfull
mBluetoothGattServer
.
sendResponse
(
device
,
requestId
,
BluetoothGatt
.
GATT_SUCCESS
,
offset
,
value
);
}
/**
* 5.特征被读取。当回复响应成功后,客户端会读取然后触发本方法
* @param device
* @param requestId
* @param offset
* @param descriptor
*/
@Override
public
void
onDescriptorReadRequest
(
BluetoothDevice
device
,
int
requestId
,
int
offset
,
BluetoothGattDescriptor
descriptor
)
{
Log
.
e
(
TAG
,
String
.
format
(
"onDescriptorReadRequest:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"onDescriptorReadRequest:requestId = %s"
,
requestId
));
// super.onDescriptorReadRequest(device, requestId, offset, descriptor);
mBluetoothGattServer
.
sendResponse
(
device
,
requestId
,
BluetoothGatt
.
GATT_SUCCESS
,
offset
,
null
);
}
@Override
public
void
onNotificationSent
(
BluetoothDevice
device
,
int
status
)
{
super
.
onNotificationSent
(
device
,
status
);
Log
.
e
(
TAG
,
String
.
format
(
"5.onNotificationSent:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"5.onNotificationSent:status = %s"
,
status
));
}
@Override
public
void
onMtuChanged
(
BluetoothDevice
device
,
int
mtu
)
{
super
.
onMtuChanged
(
device
,
mtu
);
Log
.
e
(
TAG
,
String
.
format
(
"onMtuChanged:mtu = %s"
,
mtu
));
}
@Override
public
void
onExecuteWrite
(
BluetoothDevice
device
,
int
requestId
,
boolean
execute
)
{
super
.
onExecuteWrite
(
device
,
requestId
,
execute
);
Log
.
e
(
TAG
,
String
.
format
(
"onExecuteWrite:requestId = %s"
,
requestId
));
}
};
/**
* 4.处理响应内容
*
* @param reqeustBytes
* @param device
* @param requestId
* @param characteristic
*/
private
void
onResponseToClient
(
BluetoothDevice
device
,
int
requestId
,
BluetoothGattCharacteristic
characteristic
,
byte
[]
reqeustBytes
)
{
Log
.
e
(
TAG
,
String
.
format
(
"4.onResponseToClient:device name = %s, address = %s"
,
device
.
getName
(),
device
.
getAddress
()));
Log
.
e
(
TAG
,
String
.
format
(
"4.onResponseToClient:requestId = %s"
,
requestId
));
if
(
mBlePeripheralCallback
!=
null
){
// 数据回调
mBlePeripheralCallback
.
onReceiveNewBytes
(
device
,
requestId
,
characteristic
,
reqeustBytes
);
}
}
/**
* 发送数据
* @param device
* @param data
* @return
*/
public
boolean
transfer
(
BluetoothDevice
device
,
byte
[]
data
){
if
(
notify
(
device
,
mCharacteristicRead
,
data
)){
if
(
mBlePeripheralCallback
!=
null
)
mBlePeripheralCallback
.
onWriteBytesAndStatus
(
true
,
data
);
return
true
;
}
else
{
if
(
mBlePeripheralCallback
!=
null
)
mBlePeripheralCallback
.
onWriteBytesAndStatus
(
false
,
data
);
return
false
;
}
}
/**
* 发送通知给主机
*
* @param device :发送的目标设备
* @param characteristic :用来通知的characteristic
* @param data :通知的内容
*/
private
boolean
notify
(
BluetoothDevice
device
,
BluetoothGattCharacteristic
characteristic
,
byte
[]
data
)
{
if
(
device
!=
null
&&
characteristic
!=
null
&&
data
!=
null
)
{
//设置写操作的类型 WRITE_TYPE_DEFAULT的情况选 底层会自动分包 不用人为分包
characteristic
.
setWriteType
(
BluetoothGattCharacteristic
.
WRITE_TYPE_DEFAULT
);
//把要设置的数据装进characteristic
characteristic
.
setValue
(
data
);
//发送出去
return
mBluetoothGattServer
.
notifyCharacteristicChanged
(
device
,
characteristic
,
false
);
}
else
{
return
false
;
}
}
/**
*
* @return
*/
public
IBlePeripheralCallback
getBlePeripheralCallback
()
{
return
mBlePeripheralCallback
;
}
public
void
setBlePeripheralCallback
(
IBlePeripheralCallback
blePeripheralCallback
)
{
this
.
mBlePeripheralCallback
=
blePeripheralCallback
;
}
//========================================================BluetoothReceiver======================================================
private
static
IntentFilter
makeGattUpdateIntentFilter
()
{
final
IntentFilter
intentFilter
=
new
IntentFilter
();
intentFilter
.
addAction
(
BluetoothAdapter
.
ACTION_STATE_CHANGED
);
intentFilter
.
addAction
(
BluetoothA2dp
.
ACTION_CONNECTION_STATE_CHANGED
);
intentFilter
.
addAction
(
BluetoothA2dp
.
ACTION_PLAYING_STATE_CHANGED
);
intentFilter
.
addAction
(
BluetoothDevice
.
ACTION_FOUND
);
intentFilter
.
addAction
(
BluetoothDevice
.
ACTION_BOND_STATE_CHANGED
);
intentFilter
.
addAction
(
BluetoothAdapter
.
ACTION_STATE_CHANGED
);
return
intentFilter
;
}
// 监听BLE状态变化
private
BroadcastReceiver
mBluetoothReceiver
=
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
Log
.
i
(
TAG
,
"=========蓝牙接收处理广播========"
+
intent
.
getAction
());
BluetoothDevice
device
;
switch
(
intent
.
getAction
()){
case
BluetoothAdapter
.
ACTION_STATE_CHANGED
:
int
bleState
=
intent
.
getIntExtra
(
BluetoothAdapter
.
EXTRA_STATE
,
-
1
);
switch
(
bleState
)
{
case
BluetoothAdapter
.
STATE_TURNING_OFF
:
Log
.
i
(
TAG
,
"...正在关闭蓝牙..."
);
break
;
case
BluetoothAdapter
.
STATE_OFF
:
Log
.
i
(
TAG
,
"...蓝牙已关闭!"
);
break
;
case
BluetoothAdapter
.
STATE_TURNING_ON
:
Log
.
i
(
TAG
,
"...正在开启蓝牙..."
);
break
;
case
BluetoothAdapter
.
STATE_ON
:
Log
.
i
(
TAG
,
"...蓝牙已开启..."
);
if
(
mBlePeripheralHelper
!=
null
)
initPeripheral
(
mContext
);
initGATTServer
();
break
;
}
break
;
case
BluetoothA2dp
.
ACTION_CONNECTION_STATE_CHANGED
:
switch
(
intent
.
getIntExtra
(
BluetoothA2dp
.
EXTRA_STATE
,
-
1
))
{
case
BluetoothA2dp
.
STATE_CONNECTING
:
device
=
intent
.
getParcelableExtra
(
BluetoothDevice
.
EXTRA_DEVICE
);
Log
.
i
(
TAG
,
"device: "
+
device
.
getName
()
+
" connecting"
);
break
;
case
BluetoothA2dp
.
STATE_CONNECTED
:
device
=
intent
.
getParcelableExtra
(
BluetoothDevice
.
EXTRA_DEVICE
);
Log
.
i
(
TAG
,
"device: "
+
device
.
getName
()
+
" connected"
);
break
;
case
BluetoothA2dp
.
STATE_DISCONNECTING
:
device
=
intent
.
getParcelableExtra
(
BluetoothDevice
.
EXTRA_DEVICE
);
Log
.
i
(
TAG
,
"device: "
+
device
.
getName
()
+
" disconnecting"
);
break
;
case
BluetoothA2dp
.
STATE_DISCONNECTED
:
device
=
intent
.
getParcelableExtra
(
BluetoothDevice
.
EXTRA_DEVICE
);
Log
.
i
(
TAG
,
"device: "
+
device
.
getName
()
+
" disconnected"
);
break
;
default
:
break
;
}
break
;
case
BluetoothA2dp
.
ACTION_PLAYING_STATE_CHANGED
:
int
state
=
intent
.
getIntExtra
(
BluetoothA2dp
.
EXTRA_STATE
,
-
1
);
switch
(
state
)
{
case
BluetoothA2dp
.
STATE_PLAYING
:
Log
.
i
(
TAG
,
"state: playing."
);
break
;
case
BluetoothA2dp
.
STATE_NOT_PLAYING
:
Log
.
i
(
TAG
,
"state: not playing"
);
break
;
default
:
Log
.
i
(
TAG
,
"state: unkown"
);
break
;
}
break
;
case
BluetoothDevice
.
ACTION_BOND_STATE_CHANGED
:
int
bondState
=
intent
.
getIntExtra
(
BluetoothDevice
.
EXTRA_BOND_STATE
,
BluetoothDevice
.
BOND_NONE
);
device
=
intent
.
getParcelableExtra
(
BluetoothDevice
.
EXTRA_DEVICE
);
switch
(
bondState
){
case
BluetoothDevice
.
BOND_BONDED
:
//配对成功
Log
.
i
(
TAG
,
"Device:"
+
device
.
getName
()+
" bonded."
);
//取消搜索,连接蓝牙设备
break
;
case
BluetoothDevice
.
BOND_BONDING
:
Log
.
i
(
TAG
,
"Device:"
+
device
.
getName
()+
" bonding."
);
break
;
case
BluetoothDevice
.
BOND_NONE
:
Log
.
i
(
TAG
,
"Device:"
+
device
.
getName
()+
" not bonded."
);
break
;
default
:
break
;
}
break
;
default
:
break
;
}
}
};
}
build.gradle
View file @
4e74f69f
// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext
{
targetCompatibility
=
JavaVersion
.
VERSION_1_8
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript
{
buildscript
{
repositories
{
repositories
{
google
()
google
()
...
...
library/build.gradle
View file @
4e74f69f
...
@@ -3,21 +3,29 @@ android {
...
@@ -3,21 +3,29 @@ android {
compileSdkVersion
32
compileSdkVersion
32
defaultConfig
{
defaultConfig
{
minSdkVersion
14
minSdkVersion
14
<<<<<<<
HEAD
targetSdkVersion
32
targetSdkVersion
32
=======
targetSdkVersion
28
versionCode
3
versionName
"1.3"
>>>>>>>
ee610bcfe666c5a8aee5df89f5948dec147a6d1f
}
}
buildTypes
{
buildTypes
{
release
{
release
{
minifyEnabled
false
minifyEnabled
false
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
versionNameSuffix
'1.0'
debuggable
false
jniDebuggable
false
renderscriptDebuggable
false
renderscriptOptimLevel
3
signingConfig
signingConfigs
.
debug
}
debug
{
versionNameSuffix
'1.0'
debuggable
true
jniDebuggable
false
renderscriptDebuggable
true
renderscriptOptimLevel
3
signingConfig
signingConfigs
.
debug
minifyEnabled
false
multiDexEnabled
true
}
}
}
}
lintOptions
{
lintOptions
{
...
@@ -60,5 +68,5 @@ artifacts {
...
@@ -60,5 +68,5 @@ artifacts {
dependencies
{
dependencies
{
implementation
fileTree
(
include:
[
'*.jar'
],
dir:
'libs'
)
implementation
fileTree
(
include:
[
'*.jar'
],
dir:
'libs'
)
compileOnly
'androidx.recyclerview:recyclerview:1.
0
.0'
compileOnly
'androidx.recyclerview:recyclerview:1.
2
.0'
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment