我已经实现了一个应用程序来检查蓝牙是否使用CoreBluetooth.framework进行开/关,但是当我在我的iphone 3GS设备上运行该应用程序时,它显示该设备不支持蓝牙低功耗.请任何人帮我这个或只是暗示支持iphone 3GS的任何其他蓝牙API也很明显.
谢谢
要连接到BLE设备,我使用的代码可以在这里找到:
我在我的macpro上运行代码并激活蓝牙.我可以在设置 - >蓝牙菜单中连接到我的手机但不幸的是,当我运行代码时,我只在控制台上输出"此平台上不支持CoreBluetooth BLE硬件"输出.我还没有开发者帐户,这可能是问题吗?
我写了一些CoreBluetooth代码,我可以发现设备,但我似乎无法发现特征无法发现我发现的外围设备的有人有一个好的示例代码可以用来验证我的代码吗?
\n\n这是我写的:
\n\n#import "ViewController.h"\n@property(nonatomic, strong) CBCentralManager* centralmanager;\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad {\n [super viewDidLoad];\n self.centralmanager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];\n}\n\n- (void)centralManagerDidUpdateState:(CBCentralManager *)central\n{\n CBCentralManagerState currentState = central.state;\n NSLog(@"state %i", currentState);\n\n switch (central.state)\n {\n case CBCentralManagerStatePoweredOn:\n {\n NSDictionary *options = @{\n CBCentralManagerScanOptionAllowDuplicatesKey: @YES\n };\n [self.centralmanager scanForPeripheralsWithServices:nil options:options];\n NSLog(@"I just started scanning for peripherals");\n break;\n }\n }\n}\n- (void)centralManager:(CBCentralManager *)central\n didConnectPeripheral:(CBPeripheral *)peripheral{\n NSLog(@"connected!");\n}\n\n- (void) centralManager:(CBCentralManager *)central\n didDiscoverPeripheral:(CBPeripheral *)peripheral\n advertisementData:(NSDictionary *)advertisementData\n RSSI:(NSNumber *)RSSI\n{\n [self.centralmanager connectPeripheral:peripheral options:nil];\n\n if ([[advertisementData description] containsString:@\xe2\x80\x9ckeyword\xe2\x80\x9d]) {\n\n\n NSLog(@"peripheral count …Run Code Online (Sandbox Code Playgroud) 我正在研究一种在门关闭时保持计数的设备.我想要做的是当我走进门时,我的iPhone会自动同步设备上的数据并将该计数发送到服务器(通过iPhone),而无需打开应用程序或将其置于后台.这有可能,如果是这样的话怎么样?
这是我正在思考的图表:
门关闭 - > BLE通知iPhone - > BLE向iPhone发送计数 - > iPhone将该值发送给服务器
所有没有用户(我)触摸我的设备或打开已安装的iOS应用程序.
我有显示几个警告的 BLE 源代码,我是 BLE 的新手。请看下面的代码。我曾尝试用 readRSSI 替换,但告诉我我无法将 Int 与 Void 进行比较。如何获得 readRSSI 的 Int 值?或者我应该如何更改代码?
- (void)peripheralDidUpdateRSSI:(CBPeripheral * _Nonnull)peripheral error:(NSError * _Nullable)error
{
if (!isConnected)
return;
if (rssi != peripheral.RSSI.intValue)
{
rssi = peripheral.RSSI.intValue;
[[self delegate] bleDidUpdateRSSI:activePeripheral.RSSI];
}
}
Run Code Online (Sandbox Code Playgroud)
*rssi 是一个静态整数。
*isConnected 是一个布尔值。
编辑:问题是自 iOS 8.0 起不推荐使用 RSSI。
iOS 11:当应用程序在后台时 CBCentralManager 委托方法 didDiscoverPeripheral、didConnectPeripheral、didFailToConnectPeripheral、didDisconnectPeripheral 未调用。
我正在尝试访问macOS(2017 iMac)上的蓝牙外围设备,但是CBCentralManager似乎从未进入该.poweredOn状态。
import Cocoa
import CoreBluetooth
class BluetoothManager: NSObject {
var centralManager: CBCentralManager!
override init() {
super.init()
self.centralManager = CBCentralManager(delegate: self, queue:nil)
self.checkState()
}
func checkState() {
print("central state: \(self.centralManager?.state.rawValue ?? -1)")
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2), execute: {
self.checkState()
})
}
}
extension BluetoothManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
print("Power on")
case .unsupported:
print("Unsupported")
default:break
}
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var bluetoothManager: BluetoothManager?
func applicationDidFinishLaunching(_ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用我的应用程序自动打开蓝牙。通常的方法是用户进入设置并打开。但我需要从应用程序中打开。我查阅了很多文档,它们都指的是私有 API,但都非常旧了。我不介意它不会在 App Store 获得批准。
有没有办法以编程方式打开蓝牙?
我想通过使用标识符连接外围设备。我可以做吗?
centralManager?.connect(peripheral, options: nil)
Run Code Online (Sandbox Code Playgroud)
我正在一个中扫描设备ViewController并在另一个中连接它ViewController。
以下是我的要求:
当我扫描设备并保存下次可以出现在主屏幕并直接连接的设备时。因此,为此,我使用单例类来从一个视图访问另一个视图的外围数据。但它总是初始化为空。所以我想通过使用标识符来连接外围设备。我不知道我们在哪里存储[CBPeripheral]并在需要时访问它。
我正在做一些CoreBluetooth教程,并得到了这个obj-c代码:
if (request.offset > myCharacteristic.value.length) {
// stuff
}
Run Code Online (Sandbox Code Playgroud)
value是类型的NSData.由于我的代码在Swift 3中,因此值是类型Data.但是,Data对象没有该属性length.你知道Swift中的等价物吗?
core-bluetooth ×10
ios ×8
swift ×4
bluetooth ×2
objective-c ×2
cbperipheral ×1
ibeacon ×1
ios9 ×1
iphone-3gs ×1
macos ×1