我正在制作核心蓝牙应用程序,使用我的iPhone作为外围设备,我的mac作为核心。我的iPhone开启和广告,但在蓝牙浏览器中,其他iPhone和Mac无法发现它。但是,我朋友的Android手机可以发现它。这是怎么回事? iOS结束:
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBPeripheralManagerDelegate {
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var connectionInfoLabel: UILabel!
var peripheralManager: CBPeripheralManager!
var accelerometerXCharacteristic: CBMutableCharacteristic!
var phoneDataService: CBMutableService!
var accelerometerXData: Data!
override func viewDidLoad() {
super.viewDidLoad()
connectionInfoLabel.isHidden = true
//Ignore this I was testing the data encoding
let accelerometerVal = "45"
let someData = accelerometerVal.data(using: String.Encoding.utf8)
let revertedString = String(data: someData!, encoding: String.Encoding.utf8)! as String
let integer = Int(revertedString)!
print(integer)
setupBT(delegate: self, intialXData: someData!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupBT(delegate: CBPeripheralManagerDelegate, intialXData: Data) {
peripheralManager = CBPeripheralManager(delegate: delegate, queue: nil)
//print(peripheralManager.state)
let accelerometerXCharacteristicUUID = CBUUID(string: "07B24C73-C35B-45C7-A43D-58240E3DB4DF")
let phoneDataServiceUUID = CBUUID(string: "CAB1B028-2243-4F2C-A2F1-3BE5AD51AD61")
accelerometerXCharacteristic = CBMutableCharacteristic(type: accelerometerXCharacteristicUUID, properties: CBCharacteristicProperties.read, value: intialXData, permissions: CBAttributePermissions.readable)
phoneDataService = CBMutableService(type: phoneDataServiceUUID, primary: true)
phoneDataService.characteristics = [accelerometerXCharacteristic]
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
var statusMessage = ""
switch peripheral.state {
case .poweredOn:
statusMessage = "Bluetooth Status: Turned On"
peripheralManager.add(phoneDataService)
peripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [phoneDataService.uuid], CBAdvertisementDataLocalNameKey: "DSiPhone"])
case .poweredOff:
statusMessage = "Bluetooth Status: Turned Off"
case .resetting:
statusMessage = "Bluetooth Status: Resetting"
case .unauthorized:
statusMessage = "Bluetooth Status: Not Authorized"
case .unsupported:
statusMessage = "Bluetooth Status: Not Supported"
default:
statusMessage = "Bluetooth Status: Unknown"
}
print(statusMessage)
}
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
if ((error) != nil) {
print("Error \(error!.localizedDescription)")
}
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
print("Advertising")
if ((error) != nil) {
print("Error advertising \(error!.localizedDescription)")
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
var requestedCharacteristic: CBMutableCharacteristic?
switch request.characteristic.uuid {
case accelerometerXCharacteristic.uuid:
requestedCharacteristic = accelerometerXCharacteristic
default:
requestedCharacteristic = nil
}
if let characteristic = requestedCharacteristic {
if request.offset > characteristic.value!.count {
request.value = characteristic.value?.subdata(in: request.offset..<(characteristic.value!.count - request.offset))
peripheralManager.respond(to: request, withResult: CBATTError.success)
} else {
peripheralManager.respond(to: request, withResult: CBATTError.invalidAttributeValueLength)
}
} else {
peripheralManager.respond(to: request, withResult: CBATTError.attributeNotFound)
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
print("Connection Successful")
statusLabel.text = "Successful!"
let didSendValue = peripheralManager.updateValue(accelerometerXData, for: characteristic as! CBMutableCharacteristic, onSubscribedCentrals: nil)
if !didSendValue {
print("Send failed")
}
}
}
打印出蓝牙状态已打开并进行广告宣传。在Mac端:
import Cocoa
import CoreBluetooth
class ViewController: NSViewController, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func setupBT() {
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var statusMessage = ""
switch central.state {
case .poweredOn:
centralManager.scanForPeripherals(withServices: [CBUUID(string: "CAB1B028-2243-4F2C-A2F1-3BE5AD51AD61")], options: nil)
statusMessage = "Bluetooth Status: Turned On"
case .poweredOff:
statusMessage = "Bluetooth Status: Turned Off"
case .resetting:
statusMessage = "Bluetooth Status: Resetting"
case .unauthorized:
statusMessage = "Bluetooth Status: Not Authorized"
case .unsupported:
statusMessage = "Bluetooth Status: Not Supported"
default:
statusMessage = "Bluetooth Status: Unknown"
}
print(statusMessage)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered")
}
}
打印出蓝牙已开启,但它没有发现任何东西。
答案 0 :(得分:1)
我首选的扫描外围设备的方法是不明确要求任何服务。当CBCentralManager
有时无法从广告本身指示服务时,我发现该路线会导致头痛。
相反,发现您感兴趣的外围设备的更可靠方法是不在scanForPeripheral
参数中指定任何服务,只扫描所有内容。在您的options
词典中,CBCentralManagerScanOptionAllowDuplicatesKey
包含false
,其值NSNumber
包含在didDiscover peripheral:
中。
当您收到advertisingData
方法的回调时,请查看kCBAdvDataServiceUUIDs
。它将有一个名为let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(value: false)]
centralManager?.scanForPeripherals(withServices: nil, options: options)
的字段。这将是您放置在外围设备上的一系列服务UUID。检查这些(通常只有一个)并查看是否与您感兴趣的服务UUID匹配,如果是,那些是您的外围设备。
<强>扫描强>
注意:通常我会按照定时间隔进行此操作。
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if let ids = advertisementData["kCBAdvDataServiceUUIDs"] as? [CBUUID],
let id = ids.first,
id == CBUUID(string: "YourServiceID") {
print("Found Peripheral \(peripheral)")
}
}
<强> CALLBACK 强>
{{1}}
答案 1 :(得分:0)
仅供参考,重启后代码工作得很好,所以很明显这是Core Bluetooth的故障。如果其他人有这个问题,一定要试试这个!