Skip to content

Commit ff9bb4d

Browse files
author
xiaoyunfei
committed
添加DFU 的服务
1 parent 032f1ff commit ff9bb4d

File tree

8 files changed

+117
-12
lines changed

8 files changed

+117
-12
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ android {
3838
}
3939

4040
dependencies {
41+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
42+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
4143
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2'
4244
implementation 'androidx.navigation:navigation-ui-ktx:2.3.2'
4345
implementation 'androidx.multidex:multidex:2.0.1'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.eson.liteble
2+
3+
import android.os.SystemClock
4+
import android.util.Log
5+
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import androidx.test.platform.app.InstrumentationRegistry
7+
import org.eson.log.LogUtils
8+
import org.junit.Assert.assertEquals
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
import java.text.SimpleDateFormat
12+
import java.util.*
13+
14+
/**
15+
* Instrumented test, which will execute on an Android device.
16+
*
17+
* See [testing documentation](http://d.android.com/tools/testing).
18+
*/
19+
@RunWith(AndroidJUnit4::class)
20+
class ExampleInstrumentedTest {
21+
@Test
22+
fun useAppContext() {
23+
LogUtils.d("11111111111111")
24+
// Context of the app under test.
25+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
26+
assertEquals("org.eson.liteble.test", appContext.packageName)
27+
28+
test()
29+
30+
}
31+
32+
33+
private fun test() {
34+
val rxTimestampMillis = System.currentTimeMillis() -
35+
SystemClock.elapsedRealtime() +
36+
233821638290418L / 1000000
37+
println("rxTimestampMillis = $rxTimestampMillis")
38+
val rxDate = Date(rxTimestampMillis)
39+
val sDate = SimpleDateFormat("HH:mm:ss.SSS").format(rxDate)
40+
println("sDate = $sDate")
41+
}
42+
}

app/src/main/java/org/eson/liteble/common/util/BleUUIDUtil.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
public class BleUUIDUtil {
3232

33+
private static final int COMPANY_ID_MICROSOFT = 0x0006;
34+
private static final int COMPANY_ID_APPLE = 0x004C;
35+
private static final int COMPANY_ID_NORDIC_SEMI = 0x0059;
36+
3337
public static final String UUID_FORMAT = "0000%04x-0000-1000-8000-00805f9b34fb";
3438

3539
public static final String UNKNOWN_SERVICE = "Unknown Service";
@@ -39,6 +43,8 @@ public static final class GattService {
3943
public static final int Generic_Access = 0x1800;
4044
public static final int Generic_Attribute = 0x1801;
4145
public static final int Device_Information = 0x180A;
46+
public static final int Nordic_Service = 0x1530;
47+
4248
}
4349

4450

@@ -51,23 +57,30 @@ public static final class GattCharacteristics {
5157
public static final int Firmware_Revision_String = 0x2A26;
5258
public static final int Hardware_Revision_String = 0x2A27;
5359
public static final int Manufacturer_Name_String = 0x2A29;
60+
61+
public static final int DFU_Control_Point = 0x1531;
62+
public static final int DFU_Packet = 0x1532;
63+
public static final int DFU_Status_Report = 0x1533;
64+
public static final int DFU_Revision = 0x1534;
5465
}
5566

5667

5768
public static String getServiceNameByUUID(UUID uuid) {
5869
int serviceID = getValue(uuid);
59-
UUID uuid1 = makeUUID(serviceID);
70+
// UUID uuid1 = makeUUID(serviceID);
6071

61-
if (!TextUtils.equals(uuid.toString(), uuid1.toString())) {
62-
return UNKNOWN_SERVICE;
63-
}
72+
// if (!TextUtils.equals(uuid.toString(), uuid1.toString())) {
73+
// return UNKNOWN_SERVICE;
74+
// }
6475
switch (serviceID) {
6576
case GattService.Generic_Access:
6677
return "Generic Access";
6778
case GattService.Generic_Attribute:
6879
return "Generic Attribute";
6980
case GattService.Device_Information:
7081
return "Device Information";
82+
case GattService.Nordic_Service:
83+
return "Nordic (DFU) Service";
7184
default:
7285
return UNKNOWN_SERVICE;
7386
}
@@ -76,11 +89,11 @@ public static String getServiceNameByUUID(UUID uuid) {
7689

7790
public static String getCharacterNameByUUID(UUID characterUUID) {
7891
int characterID = getValue(characterUUID);
79-
UUID uuid1 = makeUUID(characterID);
80-
81-
if (!TextUtils.equals(characterUUID.toString(), uuid1.toString())) {
82-
return UNKNOWN_CHARACTER;
83-
}
92+
// UUID uuid1 = makeUUID(characterID);
93+
//
94+
// if (!TextUtils.equals(characterUUID.toString(), uuid1.toString())) {
95+
// return UNKNOWN_CHARACTER;
96+
// }
8497

8598
switch (characterID) {
8699
case GattCharacteristics.Device_Name:
@@ -97,6 +110,15 @@ public static String getCharacterNameByUUID(UUID characterUUID) {
97110
return "Hardware Revision String";
98111
case GattCharacteristics.Manufacturer_Name_String:
99112
return "Manufacturer Name String";
113+
114+
case GattCharacteristics.DFU_Control_Point:
115+
return "DFU Control Point";
116+
case GattCharacteristics.DFU_Packet:
117+
return "DFU Packet";
118+
case GattCharacteristics.DFU_Status_Report:
119+
return "DFU Status Report";
120+
case GattCharacteristics.DFU_Revision:
121+
return "DFU Revision";
100122
default:
101123
return UNKNOWN_CHARACTER;
102124
}

app/src/main/java/org/eson/liteble/main/ScannerLiveData.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.eson.log.LogUtils
99
class ScannerLiveData : LiveData<ScannerLiveData>() {
1010

1111
private val scanResultList = mutableListOf<ScanResult>()
12-
1312
fun getScanResultList(): MutableList<ScanResult> {
1413
scanResultList.sortByDescending {
1514
it.rssi

app/src/main/java/org/eson/liteble/main/adapter/ScanBLEItem.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.eson.liteble.R;
1010
import org.eson.liteble.common.util.ByteUtil;
11+
import org.eson.log.LogUtils;
12+
import org.eson.test.band.TimeTest;
1113

1214
import kale.adapter.item.AdapterItem;
1315
import no.nordicsemi.android.support.v18.scanner.ScanRecord;
@@ -70,6 +72,10 @@ public void handleData(ScanResult device, final int position) {
7072
rootView.setOnClickListener(v -> mOnClickListener.onItemClick(device));
7173

7274

75+
// LogUtils.d("device.isLegacy() = "+ device.isLegacy());
76+
// LogUtils.d("device.isConnectable() = "+ device.isConnectable());
77+
TimeTest.test(device.getTimestampNanos());
78+
7379
ScanRecord scanRecord = device.getScanRecord();
7480
if (scanRecord == null) {
7581
return;
@@ -82,17 +88,20 @@ public void handleData(ScanResult device, final int position) {
8288
StringBuilder builder = new StringBuilder();
8389
scanRet.setVisibility(View.VISIBLE);
8490
for (int i = 0; i < array.size(); i++) {
85-
int key = array.keyAt(0);
86-
byte[] b = (byte[]) array.get(key);
91+
int key = array.keyAt(i);
92+
byte[] b = array.get(key);
8793

8894
builder.append(ByteUtil.getFormatHexString(b));
95+
String s = com.shon.bluetooth.util.ByteUtil.byteToCharSequenceUTF(b);
96+
LogUtils.d("s == "+ s);
8997
if (i != array.size() - 1) {
9098
builder.append("\n");
9199
}
92100
}
93101
scanRet.setText(builder.toString());
94102

95103

104+
96105
}
97106

98107
public interface ItemClickListener {

app/src/main/java/org/eson/test/band/Test.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.eson.liteble.common.util.ByteUtil
55

66
fun main(){
77

8+
// TimeTest.test()
89
}
910

1011

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.eson.test.band;
2+
3+
import android.os.SystemClock;
4+
5+
import org.eson.log.LogUtils;
6+
7+
public class TimeTest {
8+
public static void test(long timeNanos){
9+
long rxTimestampMillis = System.currentTimeMillis() -
10+
SystemClock.elapsedRealtime() +
11+
timeNanos / 1000000;
12+
13+
LogUtils.d("rxTimestampMillis = "+ rxTimestampMillis);
14+
// Date rxDate = new Date(rxTimestampMillis);
15+
// String sDate = new SimpleDateFormat("HH:mm:ss.SSS").format(rxDate);
16+
// LogUtils.d("sDate = "+ sDate);
17+
}
18+
}

bluetooth/src/main/java/com/shon/bluetooth/util/ByteUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public static String byteToCharSequence(byte[] buffer) {
4646
}
4747
return sendString;
4848
}
49+
public static String byteToCharSequenceUTF(byte[] buffer) {
50+
if (buffer == null || buffer.length == 0) {
51+
return "";
52+
}
53+
String sendString = "";
54+
try {
55+
sendString = new String(buffer);
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
}
59+
return sendString;
60+
}
4961

5062
/**
5163
* 把16进制字符串转换成字节数组 * @param hex * @return

0 commit comments

Comments
 (0)