我目前正在尝试开发和应用程序,允许用户通过点击一个按钮绑定到一个外设,密码将自动输入。
是否可以使用swift以编程方式焊接和移除焊接?
发布于 2018-12-23 00:03:46
每当您尝试写入或读取BLE设备上的特征时,都会启动配对。但是,如果设备未设置为需要身份验证和/或绑定,您将看不到请求PIN码的iOS弹出窗口。
我用我的HM-10解决了这个问题,因为我可以使用Core Bluetooth (via Swift) function writeValue()将数据写入特征,而不会看到配对弹出窗口。
直到我仔细阅读HM-10 (实现IC cc2451)数据表,发现我需要将AT+TYPE设置为值3,它默认为0,这意味着HM-10不需要配对/绑定,所以您永远看不到iOS弹出窗口。
您可以阅读更多关于我提出问题并最终找到解决方案并将其写出来的详细信息:How do I pair and/or bond to BLE on iOS using Swift code and an HM-10 so data sent is encrypted?
发布于 2020-02-13 21:16:04
按照步骤将Ble设备连接到iOS程序中。
1)导入
import CoreBluetooth
2)将变量声明到类或ViewController中。
let kServiceUART = CBUUID(string: "0x1800")
var peripheralHeartRateMonitor: CBPeripheral?
var cbManger: CBCentralManager!
3)将cbManger初始化为viewController的ViewDidLoad函数或初始化类的函数。
cbManger = CBCentralManager(delegate: self, queue: .main)
4)重写CBCentralManager的委托方法。
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unsupported:
print("BLe Unsupported")
break
case .unauthorized:
print("BLe unauthorized")
break
case .poweredOff:
let alertMessgesInst = AlertMessages.sharedInstance
CommonUtils.showAlert(alertMessgesInst.actofit_Title, message: alertMessgesInst.trun_On_blueTooth)
break
case .poweredOn:
if isNewFirmWareOFImpulse {
let uuidString = StorageServices.readFromDefaults(key: Constants.userDefaultKeys.impulseUUID)
let uuid = UUID(uuidString:uuidString as! String )
let device = cbManger.retrievePeripherals(withIdentifiers: [uuid!])
peripheralHeartRateMonitor = device.first
peripheralHeartRateMonitor!.delegate = self
cbManger?.connect(peripheralHeartRateMonitor!)
}else {
let option:[String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(value: false)]
cbManger.scanForPeripherals(withServices: nil, options: option)
}
break
case .unknown:
print("BLe unknown")
break
default:
break
} // End Swith
} // End 'centralManagerDidUpdateState' function.
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if isNewFirmWareOFImpulse {
peripheralHeartRateMonitor = peripheral
print("UUid of band is :- \(peripheralHeartRateMonitor?.identifier.uuidString)")
if impulseName == peripheral.name {
peripheralHeartRateMonitor!.delegate = self
cbManger.stopScan()
// STEP 6: connect to the discovered peripheral of interest
cbManger?.connect(peripheralHeartRateMonitor!)
} // End impulse condition
}else {
let keysArray = advertisementData.keys
if let tempImpulseName = peripheral.name {
print(impulseName + " and " + tempImpulseName )
if impulseName == tempImpulseName {
for key in keysArray {
if key == "kCBAdvDataManufacturerData"{
let manufactureData = advertisementData[key]
if let stringValue = manufactureData.debugDescription as? String {
var heartValue: String = String()
heartValue = stringValue
heartValue.removeLast()
heartValue.removeLast()
let last = heartValue.removeLast()
let secondLast = heartValue.removeLast()
let hR = String([secondLast, last])
if let value = UInt8(hR, radix: 16){
if Int(value) > 60 {
hrArray.append(Int(value))
}
} // End the value block
} // end of if 'stringValue' condition
} // end 'Key' if condition
} // End for each loop
} // End impulse condition
} // End pheripheral if condition
} // end version condition
} // End function 'didDiscover peripheral'.
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
// STEP 8: look for services of interest on peripheral
peripheralHeartRateMonitor?.discoverServices(nil)
} // END func centralManager(... didConnect peripheral
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if error != nil {
https://stackoverflow.com/questions/41690946
复制相似问题