Skip to content

Commit

Permalink
对接llsync标准蓝牙辅助配网协议
Browse files Browse the repository at this point in the history
http://tapd.oa.com/NEW_IOT/prong/stories/view/1020393192863935819#

Change-Id: Id5e0894279cada756afb446076c07cf2bb8fced7
  • Loading branch information
sevenhhe authored and SundoggyNew committed Aug 13, 2021
1 parent 9668e7f commit 99c514a
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -36,6 +37,7 @@ import com.tencent.iot.explorer.link.T
import com.tencent.iot.explorer.link.core.auth.callback.MyCallback
import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
import com.tencent.iot.explorer.link.core.link.entity.TrtcDeviceInfo
import com.tencent.iot.explorer.link.core.link.service.BleConfigService
import com.tencent.iot.explorer.link.customview.recyclerview.CRecyclerView
import com.tencent.iot.explorer.link.customview.verticaltab.*
import com.tencent.iot.explorer.link.kitlink.consts.CommonField
Expand Down Expand Up @@ -78,14 +80,15 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
App.data.tabPosition = 0 // Reset the position of vertical tab
App.data.screenWith = getScreenWidth()
HttpRequest.instance.getParentCategoryList(this)
BleConfigService.get().context = this
beginScanning()
}


private val runnable = Runnable {
iv_loading_cirecle.clearAnimation()
scanning.visibility = View.GONE
not_found_dev.visibility = View.VISIBLE
BleConfigService.get().stopScanBluetoothDevices()
}

override fun onResume() {
Expand Down Expand Up @@ -380,7 +383,7 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
}

private fun isBluetoothValid() : Boolean {
val adapter = BluetoothAdapter.getDefaultAdapter()
val adapter = BluetoothAdapter.getDefaultAdapter()
return adapter?.isEnabled ?: false
}

Expand All @@ -393,7 +396,8 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
not_found_dev.visibility = View.GONE
scann_fail.visibility = View.GONE
iv_loading_cirecle.startAnimation(rotateAnimation)
handler.postDelayed(runnable, 15000)
handler.postDelayed(runnable, 60000)
BleConfigService.get().startScanBluetoothDevices()
} else {
scann_fail.visibility = View.VISIBLE
scanning.visibility = View.GONE
Expand Down
2 changes: 2 additions & 0 deletions sdk/explorer-link-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tenext">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tencent.iot.explorer.link.core.link.entity

import android.bluetooth.BluetoothDevice

class BleDevice {
var url = ""
var productName = ""
var devName = ""
var blueDev : BluetoothDevice? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.tencent.iot.explorer.link.core.link.entity

class BleDeviceInfo {
companion object {

fun byteArr2BleDeviceInfo(byteArray: ByteArray): BleDeviceInfo {
var ret = BleDeviceInfo()
if (byteArray.isEmpty()) return ret
if (byteArray.get(0) != 0x08.toByte()) return ret

ret.llsyncVersion = byteArray[3].toInt().toString()
ret.mtuFlag = if ((byteArray[4].toInt() and 0x80) == 8) 1 else 0
ret.mtuSize = (byteArray[4].toInt() and 0x02 shl 8) or (byteArray[5].toInt() and 0xFF)
ret.devNameLen = byteArray[6].toInt()
val nameByteArr = ByteArray(ret.devNameLen)
System.arraycopy(byteArray, 7, nameByteArr, 0, ret.devNameLen)
ret.devName = String(nameByteArr)

return ret
}
}

var llsyncVersion = "" // 协议版本号
var mtuFlag = 0 // 是否设置 mtu 当 mtu flag为 1 时,进行 MTU 设置;当 mtu flag 为 0 时,不设置 MTU
var mtuSize = 0 // mtu 大小
var devNameLen = 0
var devName = ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.tencent.iot.explorer.link.core.link.entity

class BleDeviceWifiInfo {
var ssid = ""
var pwd = ""

fun formatByteArr(): ByteArray {
if (ssid == null || pwd == null) {
return ByteArray(0)
}

var byteArr = ByteArray( 5 + ssid.toByteArray().size + pwd.toByteArray().size)
byteArr[0] = 0xE2.toByte()
byteArr[1] = ((byteArr.size - 3) / Math.pow(2.0, 8.0).toInt()).toByte()
byteArr[2] = ((byteArr.size - 3) % Math.pow(2.0, 8.0).toInt()).toByte()
byteArr[3] = ssid.toByteArray().size.toByte()
System.arraycopy(ssid.toByteArray(), 0, byteArr, 4, ssid.toByteArray().size)
byteArr[4 + ssid.toByteArray().size] = pwd.toByteArray().size.toByte()
System.arraycopy(pwd.toByteArray(), 0, byteArr, 5 + ssid.toByteArray().size, pwd.toByteArray().size)
return byteArr
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tencent.iot.explorer.link.core.link.entity

enum class BleDeviceWifiMode(var modeValue : Byte) {
NULL(0x00.toByte()),
STA(0x01.toByte());

fun getValue(): Byte {
return modeValue
}

companion object {
fun valueOf(modeValue : Byte) : BleDeviceWifiMode{
if (modeValue == 0x01.toByte()) {
return STA
}
return NULL
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tencent.iot.explorer.link.core.link.entity

class BleWifiConnectInfo {
var wifiMode: BleDeviceWifiMode = BleDeviceWifiMode.NULL
var connected: Boolean = false
var softAp = false
var ssid = ""
var ssidLen = 0

companion object {
fun byteArr2BleWifiConnectInfo(byteArray: ByteArray): BleWifiConnectInfo {
var ret = BleWifiConnectInfo()
if (byteArray.isEmpty()) return ret
if (byteArray.get(0) != 0xE2.toByte()) return ret

ret.wifiMode = BleDeviceWifiMode.valueOf(byteArray[3].toInt().toByte())
ret.connected = byteArray[4].toInt() == 0
ret.ssidLen = byteArray[6].toInt()
val ssidByteArr = ByteArray(ret.ssidLen)
System.arraycopy(byteArray, 7, ssidByteArr, 0, ret.ssidLen)
ret.ssid = String(ssidByteArr)

return ret
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tencent.iot.explorer.link.core.link.listener

import com.tencent.iot.explorer.link.core.link.entity.BleDeviceInfo
import com.tencent.iot.explorer.link.core.link.entity.BleWifiConnectInfo
import com.tencent.iot.explorer.link.core.link.exception.TCLinkException

interface BleDeviceConnectionListener {
fun onBleDeviceConnected()
fun onBleDeviceDisconnected(exception : TCLinkException)
fun onBleDeviceInfo(bleDeviceInfo: BleDeviceInfo)
fun onBleSetWifiModeResult(success: Boolean)
fun onBleSendWifiInfoResult(success: Boolean)
fun onBleWifiConnectedInfo(wifiConnectInfo: BleWifiConnectInfo)
fun onBlePushTokenResult(success: Boolean)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tencent.iot.explorer.link.core.link.listener

import com.tencent.iot.explorer.link.core.link.entity.BleDevice

interface BleDeviceScanResult {
fun onBleDeviceFounded(bleDevice: BleDevice)
}
Loading

0 comments on commit 99c514a

Please sign in to comment.