Skip to content

Commit

Permalink
调整项目结构
Browse files Browse the repository at this point in the history
  • Loading branch information
aicareles committed Apr 25, 2020
1 parent 4ac4046 commit eca2c13
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 14 deletions.
48 changes: 34 additions & 14 deletions kotlin-sample/src/main/java/com/i502tech/appkotlin/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import cn.com.heaton.blelibrary.ble.Ble
import cn.com.heaton.blelibrary.ble.BleLog
import cn.com.heaton.blelibrary.ble.callback.*
import cn.com.heaton.blelibrary.ble.model.BleDevice
import cn.com.heaton.blelibrary.ble.model.BleFactory
import cn.com.heaton.blelibrary.ble.model.EntityData
import cn.com.heaton.blelibrary.ble.utils.ByteUtils
import cn.com.heaton.blelibrary.ble.utils.UuidUtils
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException
import java.util.*

class MainActivity : AppCompatActivity() {
companion object{
const val TAG = "MainActivity"
const val REQUESTCODE: Int = 0x01
private const val TAG = "MainActivity"
private const val REQUEST_CODE: Int = 0x01
}

private lateinit var mBle: Ble<BleDevice>
Expand Down Expand Up @@ -112,7 +113,7 @@ class MainActivity : AppCompatActivity() {

private fun requestBLEPermission() {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION), REQUESTCODE)
Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_CODE)
}


Expand All @@ -125,9 +126,25 @@ class MainActivity : AppCompatActivity() {
connectFailedRetryCount = 3
connectTimeout = 10000L
scanPeriod = 12000L
uuidService = UUID.fromString(UuidUtils.uuid16To128("fd00", true))
uuidWriteCha = UUID.fromString(UuidUtils.uuid16To128("fd01", true))
}.create(applicationContext)
uuidService = UUID.fromString(UuidUtils.uuid16To128("fd00"))
uuidWriteCha = UUID.fromString(UuidUtils.uuid16To128("fd01"))
bleWrapperCallback = MyBleWrapperCallback()
factory = object : BleFactory<MyDevice>() {
//实现自定义BleDevice时必须设置
override fun create(address: String, name: String): MyDevice{
return MyDevice(address, name) //自定义BleDevice的子类
}
}
}.create(applicationContext, object :Ble.InitCallback{
override fun failed(failedCode: Int) {
BleLog.i(TAG, "init failed: $failedCode")
}

override fun success() {
BleLog.i(TAG, "init success")
}

})
//3、检查蓝牙是否支持及打开
checkBluetoothStatus()
}
Expand Down Expand Up @@ -186,7 +203,6 @@ class MainActivity : AppCompatActivity() {

override fun onReady(device: BleDevice?) {
super.onReady(device)
// mBle.startNotify(device, bleNotifyCallback())
mBle.enableNotify(device, true, bleNotifyCallback())
}

Expand All @@ -208,7 +224,7 @@ class MainActivity : AppCompatActivity() {

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUESTCODE) {
if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
// 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
Expand All @@ -229,8 +245,8 @@ class MainActivity : AppCompatActivity() {

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUESTCODE) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val i = ContextCompat.checkSelfPermission (this, Manifest.permission.ACCESS_COARSE_LOCATION)
if (i != PackageManager.PERMISSION_GRANTED) {
// 提示用户应该去应用设置界面手动开启权限
Expand All @@ -242,10 +258,14 @@ class MainActivity : AppCompatActivity() {
}
}

@Throws(IOException::class)
private fun sendEntityData() {
val data = ByteUtils.stream2Bytes(assets.open("WhiteChristmas.bin"))
mBle.writeEntity(mBle.connectedDevices[0], data, 20, 50, object : BleWriteEntityCallback<BleDevice>() {
val payload = ByteUtils.stream2Bytes(assets.open("WhiteChristmas.bin"))
val entityData = EntityData.Builder().apply {
address = mBle.connectedDevices[0].bleAddress
data = payload
delay = 50
}.build()
mBle.writeEntity(entityData, object : BleWriteEntityCallback<BleDevice>() {
override fun onWriteSuccess() {
BleLog.e("writeEntity", "onWriteSuccess")
hideProgress()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.i502tech.appkotlin

import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.util.Log
import cn.com.heaton.blelibrary.ble.callback.wrapper.BleWrapperCallback
import cn.com.heaton.blelibrary.ble.model.BleDevice
import cn.com.heaton.blelibrary.ble.utils.ByteUtils

/**
* author: jerry
* date: 20-4-13
* email: superliu0911@gmail.com
* des: 例: OTA升级可以再这里实现,与项目其他功能逻辑完全解耦
*/
class MyBleWrapperCallback : BleWrapperCallback<BleDevice>() {
override fun onChanged(device: BleDevice, characteristic: BluetoothGattCharacteristic) {
super.onChanged(device, characteristic)
Log.d(TAG, "onChanged: " + ByteUtils.toHexString(characteristic.value))
}

override fun onServicesDiscovered(device: BleDevice, gatt: BluetoothGatt) {
super.onServicesDiscovered(device, gatt)
Log.d(TAG, "onServicesDiscovered: ")
}

override fun onWriteSuccess(device: BleDevice, characteristic: BluetoothGattCharacteristic) {
super.onWriteSuccess(device, characteristic)
Log.d(TAG, "onWriteSuccess: ")
}

override fun onConnectionChanged(device: BleDevice) {
super.onConnectionChanged(device)
Log.d(TAG, "onConnectionChanged: $device")
}

override fun onStop() {
super.onStop()
Log.d(TAG, "onStop: ")
}

override fun onLeScan(device: BleDevice, rssi: Int, scanRecord: ByteArray) {
super.onLeScan(device, rssi, scanRecord)
Log.d(TAG, "onLeScan: $device")
}

override fun onNotifySuccess(device: BleDevice) {
super.onNotifySuccess(device)
Log.d(TAG, "onNotifySuccess: ")
}

override fun onNotifyCanceled(device: BleDevice) {
super.onNotifyCanceled(device)
Log.d(TAG, "onNotifyCanceled: ")
}

override fun onReady(device: BleDevice) {
super.onReady(device)
Log.d(TAG, "onReady: ")
}

override fun onDescWriteSuccess(device: BleDevice, descriptor: BluetoothGattDescriptor) {
super.onDescWriteSuccess(device, descriptor)
}

override fun onDescWriteFailed(device: BleDevice, failedCode: Int) {
super.onDescWriteFailed(device, failedCode)
}

override fun onDescReadFailed(device: BleDevice, failedCode: Int) {
super.onDescReadFailed(device, failedCode)
}

override fun onDescReadSuccess(device: BleDevice, descriptor: BluetoothGattDescriptor) {
super.onDescReadSuccess(device, descriptor)
}

override fun onMtuChanged(device: BleDevice, mtu: Int, status: Int) {
super.onMtuChanged(device, mtu, status)
}

override fun onReadSuccess(device: BleDevice, characteristic: BluetoothGattCharacteristic) {
super.onReadSuccess(device, characteristic)
}

companion object {
private const val TAG = "MyBleWrapperCallback"
}
}
15 changes: 15 additions & 0 deletions kotlin-sample/src/main/java/com/i502tech/appkotlin/MyDevice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.i502tech.appkotlin

import cn.com.heaton.blelibrary.ble.model.BleDevice

/**
* author: jerry
* date: 20-4-25
* email: superliu0911@gmail.com
* des: 自定义BleDevice对象
*/
class MyDevice(address: String, name: String): BleDevice(address, name) {
var onLight: Boolean = false //是否开灯状态
var type = 0 //产品类型
var parent = 0 //产品父类型
}

0 comments on commit eca2c13

Please sign in to comment.