我试图通过蓝牙使用我的iphone看到外围设备
实际上我现在写这段代码
class BlueT: ViewController, CBCentralManagerDelegate, CBPeripheralDelegate{
var centralManager: CBCentralManager!
var peripheral: CBPeripheral!
let BEAN_NAME = "Az200"
let BEAN_SCRATCH_UUID =
CBUUID(string: "A159EA45-A794-27F4-8947-1C0A5E3C70B0")
let BEAN_SERVICE_UUID =
CBUUID(string: "A159EA45-A794-27F4-8947-1C0A5E3C70B0")
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if #available(iOS 10.0, *){
switch (central.state) {
case CBManagerState.poweredOff:
print("CBCentralManagerState.PoweredOff")
case CBManagerState.unauthorized:
print("CBCentralManagerState.Unauthorized")
break
case CBManagerState.unknown:
print("CBCentralManagerState.Unknown")
break
case CBManagerState.poweredOn:
print("CBCentralManagerState.PoweredOn")
centralManager.scanForPeripherals(withServices: nil, options: nil)
case CBManagerState.resetting:
print("CBCentralManagerState.Resetting")
case CBManagerState.unsupported:
print("CBCentralManagerState.Unsupported")
break
}}else{
switch central.state.rawValue{
case 0:
print("CBCentralManagerState.Unknown")
break
case 1:
print("CBCentralManagerState.Resetting")
case 2:
print("CBCentralManagerState.Unsupported")
break
case 3:
print("This app is not authorised to use Bluetooth low energy")
break
case 4:
print("Bluetooth is currently powered off.")
case 5:
print("Bluetooth is currently powered on and available to use.")
centralManager.scanForPeripherals(withServices: nil, options: nil)
default:break
}}}
func centralManager(
central: CBCentralManager,
didDiscoverPeripheral peripheral: CBPeripheral,
advertisementData: [String : AnyObject],
RSSI: NSNumber) {
let device = (advertisementData as NSDictionary)
.object(forKey: CBAdvertisementDataLocalNameKey)
as? NSString
if device?.contains(BEAN_NAME) == true {
self.centralManager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
print("peripheral: \(peripheral)")
} }
}
事实上,输出是
"蓝牙目前已开机并可供使用。"
但是印刷品("外围设备:(外围设备)")不会出现
那么我该怎样才能看到我的外围设备?