import StarburstSdk
public class DCBleManager{
+ public static let serviceUUID = "AE30"
+ public static let writeUUID = "AE03"
+ public static let notifyUUID = "AE04"
+
+
public static let shared = DCBleManager()
private var connectPeripheral: CBPeripheral?
private var writeCha: CBCharacteristic?
DCLogger.debug("蓝牙已连接 \(peripheral)")
connectPeripheral = peripheral
}
- func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
- DCLogger.debug("peripheral: %@ failed to connect", peripheral)
- }
+
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
DCLogger.debug("蓝牙已断开 \(peripheral)")
StarburstSdk.shared().bleDisConnected()
}
-
- func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
- DCLogger.debug("发现服务成功")
- for service in peripheral.services! {
- DCLogger.debug("peripheral service: %@", service)
- peripheral.discoverCharacteristics(nil, for: service)
- }
-
- }
+//
+// func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
+// DCLogger.debug("发现服务成功")
+// for service in peripheral.services! {
+// DCLogger.debug("peripheral service: %@", service)
+// peripheral.discoverCharacteristics(nil, for: service)
+// }
+//
+// }
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let list = service.characteristics{
DCLogger.debug("发现特征",list)
for char in list{
DCLogger.debug(char)
- peripheral.setNotifyValue(true, for: char)
- if char.uuid.uuidString == "AE03" {
+ if char.uuid.uuidString == DCBleManager.notifyUUID {
+ peripheral.setNotifyValue(true, for: char)
+ }
+ if char.uuid.uuidString == DCBleManager.writeUUID {
writeCha = char
- StarburstSdk.shared().bleConnected()
+
+ DCDevice.shared.handShake { success in
+ StarburstSdk.shared().bleConnected()
+ }
}
}
}
}
}
extension DCDevice{
+ public func handShake(finished:@escaping ((Bool)?)->Void){
+ var head: UInt8 = 0xBA
+ var cmd: UInt8 = 0x11
+ var data:UInt8 = 0x00
+ var total = Data()
+ total.append(&head, count: 1)
+ total.append(&cmd, count: 1)
+ total.append(&data, count: 1)
+ total.addCrc()
+
+ let sender = BleSender()
+ sender.data = total
+ sender.completion = { result in
+ DCLogger.debug("\(result.description)")
+ if result.isSuccess{
+ finished(true)
+ }
+ else{
+ finished(false)
+ }
+ }
+ DCBleManager.shared.send(sender)
+ }
///
///
public func getBatery(finished:@escaping ((Int,Int)?)->Void){
DCLogger.debug("扫描中...")
devices = []
- if let connectedPeripheral = centralManager.retrieveConnectedPeripherals(withServices: [CBUUID(string: "AE30")]).first{
+ if let connectedPeripheral = centralManager.retrieveConnectedPeripherals(withServices: [CBUUID(string: DCBleManager.serviceUUID)]).first{
devices = [connectedPeripheral]
}
discoverDeviceCallback?(devices)
return
}
if let name = peripheral.name,name.hasPrefix("AI") {
+ let mac = BleManager.getMac(advertisementData)
devices.append(peripheral)
DCLogger.debug(peripheral)
discoverDeviceCallback?(devices)
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
stopScanDevice()
- DCLogger.debug("蓝牙已连接 \(peripheral)")
peripheral.delegate = self
peripheral.discoverServices(nil);
App.shared.window?.makeToast("蓝牙已连接")
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
- DCBleManager.shared.centralManager(central, didFailToConnect: peripheral, error: error)
+// DCBleManager.shared.centralManager(central, didFailToConnect: peripheral, error: error)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
disConnectDeviceCallback?(peripheral)
}
}
+
+
+extension BleManager{
+ static func getMac(_ dict:[String:Any])->String?{
+// DCLogger.debug(dict)
+// if let advMsd = dict[CBAdvertisementDataManufacturerDataKey] as? Data {
+// if (advMsd.count >= 13) {
+// let mac = advMsd[7...12];
+// let text = mac.map { String($0, radix: 16).padLeft(toLength: 2, withPad: "0") }.joined(separator: ":").uppercased()
+//
+// DCLogger.debug(advMsd)
+// DCLogger.debug(mac.joined())
+// DCLogger.debug(text)
+// return text
+// } else {
+// return nil
+// }
+// } else {
+// return nil
+// }
+ DCLogger.debug(dict)
+ if let advMsd = dict[CBAdvertisementDataManufacturerDataKey] as? Data {
+ if (advMsd.count >= 6) {
+ let mac = advMsd;
+ let text = mac.map { String($0, radix: 16).padLeft(toLength: 2, withPad: "0") }.joined(separator: ":").uppercased()
+
+ DCLogger.debug(advMsd)
+ DCLogger.debug(mac.joined())
+ DCLogger.debug(text)
+ return text
+ } else {
+ return nil
+ }
+ } else {
+ return nil
+ }
+ }
+
+}
+extension String {
+ var data: Data? { self.data(using: .utf8) }
+
+ func padLeft(toLength: Int, withPad pad: String) -> String {
+ let paddingCount = max(0, toLength - self.count)
+ return String(repeating: pad, count: paddingCount) + self
+ }
+}
self.token = nil
self.info = UserInfo()
UserCache.shared.remove()
+ JoyBleManager.shared.cancel()
NotificationManager.post(type: .logout, objtect: nil)
App.shared.goToLogin()
}
- func tokenIsValid(finished: (() -> Void)? = nil){
+ func tokenIsValid(finished: ((Bool) -> Void)? = nil){
HttpClient.request(CommomApi.token) { [weak self](model:UserInfoRoot,res) in
guard let self = self else{return}
if model.isSuccess{
self.info = model.data
UserCache.shared.save(self.info)
+ finished?(true)
}
else{
if let dict = YLToos.stringToJson(res),let code = dict["code"] as? Int{
}
}
}
+ finished?(false)
}
- finished?()
+
}
}
}
}
}
+extension DCSdkManager{
+ static func getMac(_ dict:[String:Any])->String?{
+
+ if let advMsd = dict[CBAdvertisementDataManufacturerDataKey] as? Data {
+ if (advMsd.count >= 6) {
+ let mac = advMsd;
+ let text = mac.map { String($0, radix: 16).padLeft(toLength: 2, withPad: "0") }.joined(separator: ":").uppercased()
+ Logger.debug(dict)
+ Logger.debug(advMsd)
+ Logger.debug(mac.joined())
+ Logger.debug(text)
+ return text
+ } else {
+ return nil
+ }
+ } else {
+ return nil
+ }
+ }
+}
import UIKit
import CoreBluetooth
import AVFAudio
+import DeepChordsSdk
class DCPeripheral{
var device:CBPeripheral!
if devices.map({ $0.device }).contains(peripheral) {
return
}
-
- // DB209A55-DD0E-5031-29FA-40D734C34971
- // "edr:21140DE4C3D9"
- // "ble:21140D2CC682"
-// if peripheral.identifier.uuidString != "DB209A55-DD0E-5031-29FA-40D734C34971" {
-// return
-// }
- let mac = JoySdkManager.getMac(advertisementData) ?? ""
- devices.append(DCPeripheral(device: peripheral,mac: mac))
Logger.debug(peripheral)
- discoverDeviceCallback?(devices)
+
+ if App.shared.isJoy {
+ let mac = JoySdkManager.getMac(advertisementData) ?? ""
+ devices.append(DCPeripheral(device: peripheral,mac: mac))
+ discoverDeviceCallback?(devices)
+ }
+ else{
+ let mac = DCSdkManager.getMac(advertisementData) ?? ""
+ devices.append(DCPeripheral(device: peripheral,mac: mac))
+ discoverDeviceCallback?(devices)
+ }
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
stopScanDevice()
Logger.debug("蓝牙已连接 \(peripheral)")
-
peripheral.delegate = self
- peripheral.discoverServices([CBUUID(string: JoySdkManager.serviceUUID),CBUUID(string: OtaMananger.otaServiceUUId)]);
+ if App.shared.isJoy {
+ peripheral.discoverServices([CBUUID(string: JoySdkManager.serviceUUID),CBUUID(string: OtaMananger.otaServiceUUId)]);
+ }
+ else{
+ DCBleManager.shared.centralManager(central, didConnect: peripheral)
+ peripheral.discoverServices([CBUUID(string: DCBleManager.serviceUUID),CBUUID(string: OtaMananger.otaServiceUUId)]);
+ }
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
Logger.debug("peripheral: %@ failed to connect", peripheral)
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
Logger.debug("蓝牙已断开 \(peripheral)")
self.isConnetd = false
+ if App.shared.isJoy {
+ DCBleManager.shared.centralManager(central, didDisconnectPeripheral: peripheral, error: nil)
+ }
NotificationManager.post(type: .deviceValidChange, objtect: nil)
OtaMananger.shared.otaManager.centralManager(central, didDisconnectPeripheral: peripheral, error: error)
checkConectState()
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
- if (service.uuid.uuidString == JoySdkManager.serviceUUID) {
- JoySdkManager.shared.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
+ if App.shared.isJoy {
+ if (service.uuid.uuidString == JoySdkManager.serviceUUID) {
+ JoySdkManager.shared.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
+ }
+ else if service.uuid.uuidString == OtaMananger.otaServiceUUId{
+ OtaMananger.shared.otaManager.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
+ }
}
- else if service.uuid.uuidString == OtaMananger.otaServiceUUId{
- OtaMananger.shared.otaManager.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
+ else{
+ DCBleManager.shared.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: (any Error)?) {
- if (characteristic.isNotifying == true && characteristic.service?.uuid.uuidString == JoySdkManager.serviceUUID && characteristic.uuid.uuidString == JoySdkManager.notifyUUID) {
- Logger.debug("特征使能成功")
- JoySdkManager.shared.bleConnected()
- }
- if OtaMananger.isOtadata(characteristic){
- OtaMananger.shared.otaManager.peripheral(peripheral, didUpdateNotificationStateFor: characteristic, error: error)
+ if App.shared.isJoy {
+ if (characteristic.isNotifying == true && characteristic.service?.uuid.uuidString == JoySdkManager.serviceUUID && characteristic.uuid.uuidString == JoySdkManager.notifyUUID) {
+ Logger.debug("特征使能成功")
+ JoySdkManager.shared.bleConnected()
+ }
+ if OtaMananger.isOtadata(characteristic){
+ OtaMananger.shared.otaManager.peripheral(peripheral, didUpdateNotificationStateFor: characteristic, error: error)
+ }
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
// Logger.debug("收到数据 ===uUId ",characteristic.uuid)
- if OtaMananger.isOtadata(characteristic){
- if let _ = characteristic.value,error == nil {
- OtaMananger.shared.otaManager.peripheral(peripheral, didUpdateValueFor: characteristic)
+ if App.shared.isJoy {
+ if OtaMananger.isOtadata(characteristic){
+ if let _ = characteristic.value,error == nil {
+ OtaMananger.shared.otaManager.peripheral(peripheral, didUpdateValueFor: characteristic)
+ }
+ else{
+ Logger.debug("ota 收到数据 ===\(characteristic.uuid)","无数据")
+ }
}
- else{
- Logger.debug("ota 收到数据 ===\(characteristic.uuid)","无数据")
+ else {
+ //MARK: 需要配置传入数据
+ JoySdkManager.shared.receiveValue(characteristic)
}
}
- else {
- //MARK: 需要配置传入数据
- JoySdkManager.shared.receiveValue(characteristic)
+ else{
+ DCBleManager.shared.peripheral(peripheral, didUpdateValueFor: characteristic, error: error)
}
}
// devices.append(DCPeripheral(device: item,mac: mac))
// }
// }
- centralManager.scanForPeripherals(withServices: [CBUUID(string: JoySdkManager.serviceUUID)],options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])
+ if App.shared.isJoy {
+ centralManager.scanForPeripherals(withServices: [CBUUID(string: JoySdkManager.serviceUUID)],options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])
+ }
+ else{
+ centralManager.scanForPeripherals(withServices: [CBUUID(string: DCBleManager.serviceUUID)],options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])
+ }
discoverDeviceCallback?(devices)
}
func cancel() {
guard let peripheral = connectPeripheral else { return }
- self.isConnetd = false
+ if self.isConnetd {
+ self.isConnetd = false
+ centralManager.cancelPeripheralConnection(peripheral.device)
+ }
+
NotificationManager.post(type: .deviceValidChange, objtect: nil)
- centralManager.cancelPeripheralConnection(peripheral.device)
connectPeripheral = nil
}
func unbind(_ peripheral: DCPeripheral){
record = JoyRecord()
callRecord = JoyCallRecord()
chat = JoyChat()
+ chat.loadChatCache()
let options: AVAudioSession.CategoryOptions = [.allowBluetooth]
do {
IASignWithWeChat.shared.register()
- JoySdkManager.shared.setupSdk()
+
setupKeyboard()
loadCache()
LoginVM.fetchCountryList {result in
static let shared = App()
var window:UIWindow?
var isInBackground = false
+ var isJoy = true
func topVC()->UIViewController!{
return getTopViewController(from: vc)
}
+ func willLogin(){
+ CacheManager.shared.user.tokenIsValid {[weak self] success in
+ guard let self = self else{return}
+ if success {
+ didLogin()
+ }
+ }
+ }
func didLogin(){
// 加载目录
Logger.debug(DCFolder.user)
Logger.debug(DCFolder.record)
- window?.rootViewController = BaseTC()
- JoySdkManager.shared.chat.loadChatCache()
-// DCSdkManager.shared.initSdk()
+ self.window?.rootViewController = BaseTC()
+ JoySdkManager.shared.setupSdk()
+ // DCSdkManager.shared.initSdk()
}
func goToLogin(){
window?.rootViewController = BaseNC(rootViewController: LoginVC())
guard let _ = (scene as? UIWindowScene) else { return }
App.shared.window = window
- window?.rootViewController = BaseTC()
+// window?.rootViewController = BaseTC()
// window?.rootViewController = UINavigationController(rootViewController: HistorySelectedVC())
AppLaunch.start()
}
self?.startConectBle(model)
}
}
- CacheManager.shared.user.tokenIsValid(){[weak self] in
+ CacheManager.shared.user.tokenIsValid(){[weak self] sucess in
self?.collectionView.reloadData()
}
}
HttpClient.request(api) { (result:ResponseBaseModel,res) in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
else{
LoginVM.login(param: params) { result in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
else{
LoginVM.phoneCodeLogin(param: params) { result in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
else{
LoginVM.emailCodeLogin(param: params) { result in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
else{
LoginVM.appleLogin(param: params) { result in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
YLprogressHUD.showAutoHiden(result.msg)
LoginVM.wechatLogin(param: params) { result in
if result.isSuccess{
YLprogressHUD.showSuccess {
- App.shared.didLogin()
+ App.shared.willLogin()
}
}
else{
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
- CacheManager.shared.user.tokenIsValid(){[weak self] in
+ CacheManager.shared.user.tokenIsValid(){[weak self] sucess in
self?.header.updateUI()
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let lan = list[indexPath.row]
-// CustomAlert.showWith(title: "提示".l10n, messgage: "重启APP才会生效,是否立即重启?".l10n, cancelCallback: {}) {
-//
-// }
DCConfig.appLanguage = lan
LanuageCache.save(lan: DCConfig.appLanguage)
App.shared.didLogin()
}
}
func updateUI(){
- CacheManager.shared.user.tokenIsValid(){[weak self] in
+ CacheManager.shared.user.tokenIsValid(){[weak self] sucess in
guard let self = self else {return}
titleLabel.text = CacheManager.shared.user.info.userName
icon.kf.setImage(with:CacheManager.shared.user.info.avatar.yc.asUrl )
}
}
func refreshUI(){
- CacheManager.shared.user.tokenIsValid(){[weak self] in
+ CacheManager.shared.user.tokenIsValid(){ [weak self] success in
self?.tableView.reloadData()
}
LoginVM.fetchCountryList {[weak self] result in