Reputation: 4090
I'm making a core bluetooth app using my iPhone as a peripheral and my mac as a central. My iPhone is turned on and advertising, but in the bluetooth browser other iPhones and Macs cannot discover it. However, my friend's Android phone can discover it. What's going on? iOS end:
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")
}
}
}
It's printing out that Bluetooth status is turned on and advertising. On the Mac end:
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")
}
}
It's printing out that bluetooth is powered on but it isn't discovering anything.
Upvotes: 3
Views: 1958
Reputation: 4090
FYI that code there worked just fine after a reboot, so obviously this was a glitch with Core Bluetooth. If anyone else has this issue, definitely try this!
Upvotes: 2
Reputation: 8279
My preferred method to scan for peripherals is to not explicitly ask for any service in particular. I have found going that route leads to headache when CBCentralManager
sometimes fails to indicate the service from the advertisements themselves.
Instead, the more reliable way of discovering the peripheral you are interested in is to not specify any services in your scanForPeripheral
parameter and scan for everything, only once. In your options
dictionary include the CBCentralManagerScanOptionAllowDuplicatesKey
key with a value of false
wrapped in an NSNumber
.
And when you receive the callback from didDiscover peripheral:
method look into the advertisingData
. It will have a field called kCBAdvDataServiceUUIDs
. This will be an array of service UUIDs you placed on your peripheral. Check these (usually just one) and see if any match the service UUIDs you are interested in, if so, those are your peripherals.
SCAN
note: Typically I do this on a timed interval basis.
let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(value: false)]
centralManager?.scanForPeripherals(withServices: nil, options: options)
CALLBACK
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)")
}
}
Upvotes: 2