Reputation: 91
I just want to get a list of any Bluetooth Devices around me, but CoreBluetooth display only Bluetooth Low Energy (4.0.
I don't want to connect to a device, but just display its name.
Is there a solution to do this?
Upvotes: 5
Views: 10638
Reputation: 177
I am using like this, connect on button click event and use CBCentralManagerDelegate, CBPeripheralDelegate
Delegate
func connectDevice(sender:UIButton){
if peripheral != nil {
manager.cancelPeripheralConnection(peripheral)
manager = CBCentralManager(delegate: self, queue: nil)
}
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
central.scanForPeripheralsWithServices(nil, options: nil)
} else {
self.showAlert(Messages().alert , message: "Bluetooth is not on.")
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
let device = (advertisementData as NSDictionary).objectForKey(CBAdvertisementDataLocalNameKey) as? NSString
print(device)
if device?.containsString(BEAN_NAME) == true {
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connectPeripheral(peripheral, options: nil)
}
}
Upvotes: 1
Reputation: 36
if (!_centralManager) {
dispatch_queue_t queue = dispatch_get_main_queue();
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:@{CBCentralManagerOptionShowPowerAlertKey:@YES}];
[_centralManager setDelegate:self];
}
//found BLE
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//Array add peripheral
}
Upvotes: 1
Reputation: 8289
What you want to do is scan for all the CBPeripheral
in your area. CBPeripheral
is the class that does the broadcasting of any CBService
(s) that the peripheral may advertise.
To scan for these peripherals you will need an instance of CBCentralManager
. CBCentralManager
is the class that does the scanning of your peripherals.
To do this you must instantiate your CBCentralManager
centralManager = CBCentralManager(delegate: self, queue: .main)
A good idea is before you scan for peripherals to tell your CBCentralManager
that you only want to discover unique peripherals only once.
let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:
NSNumber(value: false)]
And when you tell your CBCentralManager
to scan, don't specify any services of the advertising CBPeripheral
(s). Instead pass nil
for this parameter to indicate you want to discover all peripherals.
centralManager?.scanForPeripherals(withServices: nil, options: options)
The above call will begin the actual scanning for the bluetooth devices in the area. You will receive the callback in the CBCentralManagerDelegate methods to the result of your scan.
To get the name of the bluetooth devices, simple look at the name of the CBPeripheral
(s) that are discovered. You do this through the CBCentralManagerDelegate
method didDiscover peripheral: CBPeripheral
.
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered \(peripheral.name ?? "")")
}
Upvotes: 6