iOS:是不是可以通过蓝牙将字符串发送到计算机,只需为其提供 MAC 地址
Posted
技术标签:
【中文标题】iOS:是不是可以通过蓝牙将字符串发送到计算机,只需为其提供 MAC 地址【英文标题】:iOS: Is it possible to send a string to a computer via bluetooth just by giving it the MAC addressiOS:是否可以通过蓝牙将字符串发送到计算机,只需为其提供 MAC 地址 【发布时间】:2018-01-31 04:31:19 【问题描述】:我是 swift 编程新手,我需要有关使用蓝牙的帮助。
我正在开展一个项目,该项目涉及通过蓝牙向计算机发送字符串,并且我能够事先输入接收设备的 MAC 地址,以便知道将其发送到哪里。
现阶段我唯一的问题是连接到所述设备并发送数据。我尝试查找教程,但它们要么是针对 android 的(我已经开始工作了,我现在需要一个针对 ios 的教程),要么是关于如何通过服务 UUID 进行连接的(什么?)。
这是我到目前为止的代码:
import UIKit
import CoreBluetooth
class transmitter: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate
var manager:CBCentralManager!
var peripheral:CBPeripheral!
let SCRATCH_UUID = UUID.init(uuidString: "00001101-0000-1000-8000-00805F9B34FB")
let SERVICE_UUID = CBUUID(string: "00001101-0000-1000-8000-00805F9B34FB")
override func viewDidLoad()
super.viewDidLoad()
// Define manager
manager = CBCentralManager(delegate: self, queue: nil)
print(globals.data)
// Do any additional setup after loading the view.
@IBOutlet weak var console: UILabel!
// Check if teh bluetooth is enabled
func centralManagerDidUpdateState(_ central: CBCentralManager)
if central.state == CBManagerState.poweredOn
central.scanForPeripherals(withServices:nil, options: nil)
print (central.isScanning)
console.text = String(describing: central.retrievePeripherals(withIdentifiers: [SCRATCH_UUID!]))
else
//print("Bluetooth not available.")
let alert = UIAlertController(title: "Bluetooth unavalible", message: "Bluetooth is unavalibe for this device. Is it even turned on?", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
// Pair with device....
// TODO: Change to be based on MAC Address instead of name...?
private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
let device = (advertisementData as NSDictionary).object(forKey: CBAdvertisementDataLocalNameKey) as? NSString
// console.text = peripheral.name
/*
if device?.contains(globals.macAddress) == true
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connect(peripheral, options: nil)
*/
//
// The rest is copied from a tutorial
//
// Once you are connected to a device, you can get a list of services on that device.
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral)
peripheral.discoverServices(nil)
// Once you get a list of the services offered by the device, you will want to get a list of the characteristics. You can get crazy here, or limit listing of characteristics to just a specific service. If you go crazy watch for threading issues.
private func peripheral(peripheral: CBPeripheral,didDiscoverServices error: NSError?)
for service in peripheral.services!
let thisService = service as CBService
if service.uuid == SERVICE_UUID
peripheral.discoverCharacteristics(nil, for: thisService)
// There are different ways to approach getting data from the BLE device. One approach would be to read changes incrementally. Another approach, the approach I used in my application, would be to have the BLE device notify you whenever a characteristic value has changed.
private func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
for characteristic in service.characteristics!
let thisCharacteristic = characteristic as CBCharacteristic
if thisCharacteristic.uuid == SERVICE_UUID
self.peripheral.setNotifyValue(true, for: thisCharacteristic)
// This is an optional step, but hey, let us be good programmers and clean up after ourselves. Also a good place to start scanning all over again.
private func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)
central.scanForPeripherals(withServices: nil, options: nil)
我在这个阶段试图做的是:
-
检查以确保蓝牙已启用(有效)
列出可用设备,因为我无法通过 MAC 地址连接(失败)
不过,如果我不必执行第 2 步,只需通过提供的 MAC 地址进行连接,而不是扫描未显示任何设备的设备,那就更好了。
帮助?
【问题讨论】:
您不能使用 Core Bluetooth 通过 MAC 地址连接到设备。您需要扫描正在宣传您感兴趣的服务的外围设备,然后连接到它。连接后,您可以发现并写入设备提供的特征。核心蓝牙将 MAC 地址隐藏在标识符后面。此外,Core Bluetooth 不支持旧配置文件,例如您可能与 Android 一起使用的串行端口配置文件。 好的,是否有一个 API 可以 允许我像在 android 上一样通过 MAC 地址进行连接?还是在iOS中完全禁止 不,没有 api 可以做你想做的事。您需要在 PC 上运行一个应用来宣传您的 iOS 应用可以连接到的服务。 没关系,对 SPP 进行了更多研究,并且蓝牙 4.0 似乎支持它(有点),但不支持 BLE。所以可悲的是我的方法无效:( 蓝牙 4.0 支持 SPP 配置文件,因为它向后兼容早期版本的蓝牙,但 Core Data 仅向 iOS 应用程序公开 GATT 配置文件。如果您的外围设备通过了 MFi 认证,那么它可以通过外部附件框架使用 SPP 配置文件。 【参考方案1】:好的,所以似乎不支持 SPP。废话:/
我会研究 John Doe 和 Paulw11 的建议
谢谢!
【讨论】:
以上是关于iOS:是不是可以通过蓝牙将字符串发送到计算机,只需为其提供 MAC 地址的主要内容,如果未能解决你的问题,请参考以下文章