4
我對iOS編程和藍牙協議都很陌生。 我發現了一個快速編寫的示例代碼,並試圖修改它以使用我自己的藍牙模塊。我有的模塊是dorji的DBM01。適用於iOS的基本BLE通信應用程序swift
我需要使用該服務FFF0和特點是FFF1用於發送ASCII值。
當我在我的MacBook上使用LightBlue應用程序並連接到我設計的,其上有DBM01模塊的電路板時,我可以發送「1」的char值並獲得期望的響應(打開LED),當我發送「0」的值時,它關閉LED。
現在用我的代碼,我可以連接到DBM01模塊。我可以打印它的名字。但是,我不能使用以下功能從它斷開連接。我也不確定這是否與設備斷開連接,或者在設備斷開連接時自動呼叫。無論如何,這種方式無法奏效。
func centralManager(central: CBCentralManager,
didDisconnectPeripheral peripheral: CBPeripheral,
error: NSError?)
我的主要問題是,我真的不明白,我怎麼指定服務和我感興趣的特點和連接到具有他們特定的設備。
我也無法發送消息。當我嘗試,我得到了預定義的錯誤,因爲以下條件我以前不抱
if writeCharacteristic != nil
下面是我的全部代碼。
如果您能指出我做錯了什麼,以及如何通過特定的服務和特性信息以及發送數據實現與特定設備的連接,請注意。
//
// ViewController.swift
// bleSwift
//
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
var writeCharacteristic: CBCharacteristic!
var service: CBService!
var characteristic: CBCharacteristic!
var bluetoothAvailable = false
let message = "1"
@IBOutlet weak var deviceName: UILabel!
@IBOutlet weak var ServiceName: UILabel!
@IBOutlet weak var CharacteristicsName: UILabel!
func centralManagerDidUpdateState(central: CBCentralManager)
{
print("Checking state")
switch (central.state)
{
case .PoweredOff:
print("CoreBluetooth BLE hardware is powered off")
case .PoweredOn:
print("CoreBluetooth BLE hardware is powered on and ready")
bluetoothAvailable = true;
case .Resetting:
print("CoreBluetooth BLE hardware is resetting")
case .Unauthorized:
print("CoreBluetooth BLE state is unauthorized")
case .Unknown:
print("CoreBluetooth BLE state is unknown");
case .Unsupported:
print("CoreBluetooth BLE hardware is unsupported on this platform");
}
if bluetoothAvailable == true
{
discoverDevices()
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
// Stop scanning
self.centralManager.stopScan()
print("Stopped Scanning")
// Set as the peripheral to use and establish connection
//self.peripheral = peripheral
//self.peripheral.delegate = self
//self.centralManager.connectPeripheral(peripheral, options: nil)
peripheral.discoverServices([CBUUID(string: "FFF0")])
print("CONNECTED!!")
print(peripheral.name)
deviceName.text = peripheral.name
}
func discoverDevices() {
print("Discovering devices")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
@IBAction func disconnectDevice(sender: AnyObject) {
func centralManager(central: CBCentralManager,
didDisconnectPeripheral peripheral: CBPeripheral,
error: NSError?)
{
print("CONNECTION WAS DISCONNECTED")
deviceName.text = "Disconnected"
}
}
@IBAction func Scan(sender: AnyObject)
{
print("Scan")
centralManager = CBCentralManager(delegate: self, queue: nil)
}
@IBAction func Send(sender: AnyObject)
{
let data = message.dataUsingEncoding(NSUTF8StringEncoding)
if writeCharacteristic != nil
{
print("Sent")
peripheral!.writeValue(data!, forCharacteristic: writeCharacteristic, type: CBCharacteristicWriteType.WithoutResponse)
}
else
{
print("Couldn't Send")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我創建了一個GitHub的演示應用程序,它列出了藍牙LE設備。一旦你有了'CBPeripheral'對象,就可以連接它併發送數據。 (查看[這個答案](https://stackoverflow.com/a/44782698/784318)) – Besi