我是苹果开发的新手,并且一直在解决蓝牙问题。我尝试编写一个基本的OSX应用程序,它将扫描附近的设备并使用已发现的设备列表填充PopupList。
只是设置场景一点.. 我有一个按钮'startStopScanBut',它将启动'startStopScan' 我也有两个日志。 Conn记录连接信息。 stateLog for phone state info。
ViewController.h
// ViewController.h
// F4STestHarness
//
// Created by Nicholas Hayward on 26/06/2015.
// Copyright (c) 2015 Nicholas Hayward. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : NSViewController
<CBCentralManagerDelegate,
CBPeripheralDelegate>
@end
ViewController.m
// ViewController.m
// F4STestHarness
//
// Created by Nicholas Hayward on 26/06/2015.
// Copyright (c) 2015 Nicholas Hayward. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) IBOutlet NSTextView *stateLogTextView;
@property (nonatomic) IBOutlet NSTextView *bluetoothLogTextView;
@property BOOL bluetoothOn;
@property BOOL isScanning;
@property (nonatomic) IBOutlet NSButton *startStopScanBut;
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (nonatomic) IBOutlet CBPeripheral *foundPeripheral;
@property (nonatomic) IBOutlet NSPopUpButton *foundPeripheralsPop;
/*@property BOOL isAdvertising;
@property BOOL isAdvertisingInitialised;
@property (nonatomic) IBOutlet NSButton *startStopAdvertisingButton;
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
@property (strong, nonatomic) CBMutableCharacteristic *myCharacteristic;
@property (strong, nonatomic) NSDictionary *advertisingData;*/
#define SERVICE_UUID @ "1f9f7e9d-7a83-4053-87f9-2c328b8f315a"
#define CHARACTERISTIC_UUID @ "FFF3"
@end
@implementation ViewController
//Write to log function
-(void)conLog:(NSString *)msg
{
self.bluetoothLogTextView.string = [@"\r\n" stringByAppendingString:self.bluetoothLogTextView.string];
self.bluetoothLogTextView.string = [msg stringByAppendingString:self.bluetoothLogTextView.string];
}
-(void)stateLog:(NSString *)msg
{
self.stateLogTextView.string = [@"\r\n" stringByAppendingString:self.stateLogTextView.string];
self.stateLogTextView.string = [msg stringByAppendingString:self.stateLogTextView.string];
}
//form events
//form load
- (void)viewDidLoad {
[super viewDidLoad];
//Initialise flags
self.bluetoothOn = NO;
self.isScanning = NO;
//self.isAdvertising = NO;
//self.isAdvertisingInitialised = NO;
//initialise bluetooth central manager
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:Nil];
//initialise bluetooth perihperl manager
//self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:Nil];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
//Bluetooth Central events
- (IBAction)startStopScan:(id)sender
{
//check bluetooth
if (!self.bluetoothOn){
[self conLog:@"Bluetooth is OFF, Cannot start scan"];
return;
}
//Do we need to start or stop the scan?
if (!self.isScanning)
{
//We are not scanning, but we are starting a new scan
//empty the foundPeripheralsPop
[_foundPeripheralsPop removeAllItems];
//Start the scan. (we scan for the service uuid used when advertising services)
[self.centralManager scanForPeripheralsWithServices: Nil
options: @{ CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];
//update flag
self.isScanning = YES;
//change button text to say stop Scanning
[self.startStopScanBut setTitle:@"Stop Scanning"];
//update log
[self conLog:@"Starting scan for Peripherals"];
}
else
{
//We are scanning, Stop the scan
[self.centralManager stopScan];
//update flag
self.isScanning = NO;
//change button text to say start Scanning
[self.startStopScanBut setTitle:@"Get Advertising Peripherals"];
//update log
[self conLog:@"Stopped scan for peripherals"];
}
}
//the state of the adapter has changedA(su1 enabled or disabled bluetooth
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state != CBCentralManagerStatePoweredOn)
{
[self stateLog:@"Bluetooth OFF"];
self.bluetoothOn = NO;
}
else
{
[self stateLog:@"Bluetooth ON"];
self.bluetoothOn = YES;
}
}
//We have found a peripheral device while scanning.
-(void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSString *deviceName = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
if (deviceName == nil || [deviceName isEqual: @""]) deviceName = @"No Name";
//log what device we discovered
[self conLog:[NSString stringWithFormat:@"Discovered %@, RSSI: %@\n", deviceName, RSSI]];
//Add device to select list
[_foundPeripheralsPop addItemWithTitle:deviceName];
self.foundPeripheral = peripheral;
//[self.centralManager connectPeripheral:peripheral options:nil];
}
我遇到的问题是这个OSX应用程序会不断地找到相同的外设,不断地启动didFindPeripheral功能。我已经基于上面的代码创建了一个单独的IOS项目,它没有找到重复项。
我试图找到的外围设备是...... 1.在插入ios设备上运行的Lynda.com外设示例 2. Fitbit健身追踪器。 (http://www.fitbit.com)
OSX中是否有任何东西可以阻止这些副本通过?任何人都有任何想法为什么它一遍又一遍地发现这些设备?