在工作中,我被赋予了使命 :) 移植一个连接到设备以执行操作的 Android 库(由于 NDA,我不能说更多)。 Android 代码使用 getRemoteDevice(macAddress)
和 createRfcommSocketToServiceRecord
基本上跳过整个配对机制,然后“按原样”发送字节。这在 iOS 上可行吗?有没有这种程度的“肮脏”:-)
最佳答案
如果是蓝牙2.1就很简单了。您还可以创建套接字。例如:
EASession *session = [[EASession alloc] initWithAccessory:accessory
forProtocol:PROTOCOL_STRING];
if (session) {
[[session inputStream] setDelegate:self];
[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session inputStream] open];
[[session outputStream] setDelegate:self];
[[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session outputStream] open];
}
但是,正如@Paulw11 提到的,您需要通过 MFI 程序。
蓝牙 LE 有很大不同。它没有 socket 。您只能以服务和特征进行操作。
顺便说一句,您不能仅通过使用其 MAC 地址连接到 BLE 设备。您首先需要使用其服务 UUID 扫描此设备:
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kScanServiceUUID]] options:options];
只有在那之后连接到它的 CBPeripheral。
更新: 至于蓝牙 2.1 中的“基本上跳过整个配对机制”,我猜你不能在没有用户的情况下进行配对。 有一种方法向用户显示对话框,他可以选择他想要配对的设备,但这就是我能找到的全部:
- (void)showBluetoothAccessoryPickerWithNameFilter:(NSPredicate *)predicate completion:(EABluetoothAccessoryPickerCompletion)completion
Displays an alert that allows the user to pair the device with a Bluetooth accessory.
与蓝牙 2.1 不同,在低功耗蓝牙中您根本不需要配对。
关于ios - iOS 上的 IOBluetooth/使用其 Mac 地址连接到设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325809/