|
@@ -1,4 +1,4 @@
|
|
|
-package com.luojigou.xiaodou
|
|
|
+package com.luojigou.hardware.xiaodou
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.app.Service
|
|
@@ -25,15 +25,14 @@ import androidx.core.app.NotificationChannelCompat
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
import androidx.core.content.ContextCompat
|
|
|
-import com.luojigou.xiaodou.ble.XDBLEUtils
|
|
|
-import com.luojigou.xiaodou.ble.XDScanBLEDevice
|
|
|
-import com.luojigou.xiaodou.wifi.XDScanWifiInfo
|
|
|
+import com.luojigou.hardware.xiaodou.ble.XDBLEUtils
|
|
|
+import com.luojigou.hardware.xiaodou.wifi.XDScanWifiInfo
|
|
|
import java.util.UUID
|
|
|
import kotlin.concurrent.thread
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
-class XDScanBLEService : Service() {
|
|
|
+class XDConnectService : Service() {
|
|
|
companion object {
|
|
|
const val ACTION_START_SCAN = "com.luojigou.xiaodou.ble.ACTION_START_SCAN"
|
|
|
const val ACTION_STOP_SCAN = "com.luojigou.xiaodou.ble.ACTION_STOP_SCAN"
|
|
@@ -52,9 +51,15 @@ class XDScanBLEService : Service() {
|
|
|
const val ACTION_CONNECT_BLE_RESULT_SUCCESS = "com.luojigou.xiaodou.ble.ACTION_CONNECT_BLE_RESULT_SUCCESS"
|
|
|
|
|
|
const val ACTION_CONNECT_WIFI = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI"
|
|
|
- const val ACTION_CONNECT_WIFI_RESULT_ERROR = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_ERROR"
|
|
|
- const val ACTION_CONNECT_WIFI_RESULT_SUCCESS = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_SUCCESS"
|
|
|
-
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_TIMEOUT = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_TIMEOUT" // -2
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_ERROR = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_ERROR" // -1
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_SUCCESS = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_SUCCESS" // 0
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_CONNECTING = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_CONNECTING" // 1
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_FAIL = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_FAIL" // 2
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_NOT_FOUND = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_NOT_FOUND" // 3
|
|
|
+ const val ACTION_CONNECT_WIFI_RESULT_PASSWORD_ERROR = "com.luojigou.xiaodou.ble.ACTION_CONNECT_WIFI_RESULT_PASSWORD_ERROR" // 4
|
|
|
+
|
|
|
+ private val deviceServiceUuidMask = UUID.fromString("0000af00-0000-1000-8000-00805f9b34fb")
|
|
|
private val wifiConnectServiceUUID = UUID.fromString("0000ae80-0000-1000-8000-00805f9b34fb")
|
|
|
private val wifiConnectWriteUUID = UUID.fromString("0000ae81-0000-1000-8000-00805f9b34fb")
|
|
|
private val wifiConnectNotifyUUID = UUID.fromString("0000ae82-0000-1000-8000-00805f9b34fb")
|
|
@@ -63,7 +68,7 @@ class XDScanBLEService : Service() {
|
|
|
private val bleChannelName = "BLE-CHANNEL"
|
|
|
}
|
|
|
|
|
|
- private val bluetoothDevices: MutableMap<XDScanBLEDevice, BluetoothDevice> = HashMap()
|
|
|
+ private val bluetoothDevices: MutableMap<com.luojigou.hardware.xiaodou.ble.XDScanBLEDevice, BluetoothDevice> = HashMap()
|
|
|
|
|
|
private lateinit var bluetoothManager: BluetoothManager
|
|
|
|
|
@@ -84,10 +89,13 @@ class XDScanBLEService : Service() {
|
|
|
ACTION_SELECT_BLE_DEVICE -> {
|
|
|
val bleDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
intent.getParcelableExtra(
|
|
|
- ACTION_SELECT_BLE_DEVICE_DATA, XDScanBLEDevice::class.java
|
|
|
+ ACTION_SELECT_BLE_DEVICE_DATA,
|
|
|
+ com.luojigou.hardware.xiaodou.ble.XDScanBLEDevice::class.java
|
|
|
)
|
|
|
} else {
|
|
|
- intent.getParcelableExtra<XDScanBLEDevice>(ACTION_SELECT_BLE_DEVICE)
|
|
|
+ intent.getParcelableExtra<com.luojigou.hardware.xiaodou.ble.XDScanBLEDevice>(
|
|
|
+ ACTION_SELECT_BLE_DEVICE
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
bleDevice?.let { connectBLE(it) }
|
|
@@ -104,6 +112,10 @@ class XDScanBLEService : Service() {
|
|
|
|
|
|
wifiInfo?.let { connectWifi(wifiInfo) }
|
|
|
}
|
|
|
+
|
|
|
+ ACTION_CONNECT_WIFI -> {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -150,7 +162,9 @@ class XDScanBLEService : Service() {
|
|
|
result.apply {
|
|
|
if (device.name == null) return
|
|
|
|
|
|
- val device = XDScanBLEDevice(
|
|
|
+ if (!device.name.startsWith("XiaoDouAI")) return
|
|
|
+
|
|
|
+ val device = com.luojigou.hardware.xiaodou.ble.XDScanBLEDevice(
|
|
|
name = device.name ?: "unknown",
|
|
|
bondState = device.bondState.toString(),
|
|
|
rssi = rssi.toString(),
|
|
@@ -256,7 +270,7 @@ class XDScanBLEService : Service() {
|
|
|
sendBroadcast(intent)
|
|
|
}
|
|
|
|
|
|
- private fun connectBLE(device: XDScanBLEDevice) {
|
|
|
+ private fun connectBLE(device: com.luojigou.hardware.xiaodou.ble.XDScanBLEDevice) {
|
|
|
broadcastUpdate(ACTION_CONNECT_BLE)
|
|
|
val bleDevice = bluetoothDevices[device] ?: return
|
|
|
|
|
@@ -271,11 +285,19 @@ class XDScanBLEService : Service() {
|
|
|
private fun connectWifi(device: XDScanWifiInfo) {
|
|
|
if (bluetoothGatt == null || writeCharacteristic == null) return
|
|
|
|
|
|
- bluetoothGatt!!.writeCharacteristic(
|
|
|
- writeCharacteristic!!,
|
|
|
- XDBLEUtils.convertToConnectRequest(device.ssid, device.password),
|
|
|
- BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE,
|
|
|
+ val bytes = XDBLEUtils.convertToConnectRequest(
|
|
|
+ device.ssid, device.password
|
|
|
)
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
+ bluetoothGatt!!.writeCharacteristic(
|
|
|
+ writeCharacteristic!!,
|
|
|
+ bytes, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE,
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ writeCharacteristic!!.setValue(bytes)
|
|
|
+ bluetoothGatt!!.writeCharacteristic(writeCharacteristic!!)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onDestroy() {
|
|
@@ -341,31 +363,13 @@ class XDScanBLEService : Service() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onCharacteristicChanged(
|
|
|
- gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic
|
|
|
- ) {
|
|
|
- super.onCharacteristicChanged(gatt, characteristic)
|
|
|
- gatt.readCharacteristic(characteristic)
|
|
|
- Log.d("XDScanBLEService", "onCharacteristicChanged1:")
|
|
|
- }
|
|
|
-
|
|
|
override fun onCharacteristicChanged(
|
|
|
gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray
|
|
|
) {
|
|
|
super.onCharacteristicChanged(gatt, characteristic, value)
|
|
|
- val result = XDBLEUtils.parseConnectResponse(value)
|
|
|
- Log.d("XDScanBLEService", "onCharacteristicChanged2: $result")
|
|
|
- }
|
|
|
-
|
|
|
- override fun onCharacteristicRead(
|
|
|
- gatt: BluetoothGatt,
|
|
|
- characteristic: BluetoothGattCharacteristic,
|
|
|
- value: ByteArray,
|
|
|
- status: Int
|
|
|
- ) {
|
|
|
- super.onCharacteristicRead(gatt, characteristic, value, status)
|
|
|
- val result = XDBLEUtils.parseConnectResponse(value)
|
|
|
- Log.d("XDScanBLEService", "onCharacteristicRead: $result")
|
|
|
+ val response = XDBLEUtils.parseConnectResponse(value)
|
|
|
+ response.toAction()
|
|
|
+ Log.d("XDScanBLEService", "onCharacteristicChanged: $response")
|
|
|
}
|
|
|
|
|
|
override fun onCharacteristicWrite(
|
|
@@ -377,13 +381,19 @@ class XDScanBLEService : Service() {
|
|
|
Log.d("XDScanBLEService", "onCharacteristicWrite: success")
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- override fun onDescriptorRead(
|
|
|
- gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int, value: ByteArray
|
|
|
- ) {
|
|
|
- super.onDescriptorRead(gatt, descriptor, status, value)
|
|
|
- val result = XDBLEUtils.parseConnectResponse(value)
|
|
|
- Log.d("XDScanBLEService", "onDescriptorRead: $result")
|
|
|
+ private fun Int.toAction() {
|
|
|
+ when (this) {
|
|
|
+ -99 -> {}
|
|
|
+ -2 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_TIMEOUT)
|
|
|
+ -1 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_ERROR)
|
|
|
+ 0 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_SUCCESS)
|
|
|
+ 1 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_CONNECTING)
|
|
|
+ 2 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_FAIL)
|
|
|
+ 3 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_NOT_FOUND)
|
|
|
+ 4 -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_PASSWORD_ERROR)
|
|
|
+ else -> broadcastUpdate(ACTION_CONNECT_WIFI_RESULT_ERROR)
|
|
|
}
|
|
|
}
|
|
|
}
|