iOS:只需给计算机提供MAC地址,就可以通过蓝牙向计算机发送字符串吗
我是swift编程新手,需要蓝牙方面的帮助 我正在做一个项目,这个项目涉及通过蓝牙向计算机发送字符串,我能够事先输入接收设备的MAC地址,这样它就知道在哪里发送 在此阶段,我唯一的问题是连接到所述设备,并发送数据。我试着查找教程,但它们要么是针对Android的(我已经开始工作了,我现在需要一个用于iOS的),要么是关于如何通过服务UUID连接(什么?) 以下是我目前掌握的代码:iOS:只需给计算机提供MAC地址,就可以通过蓝牙向计算机发送字符串吗,ios,swift,bluetooth,core-bluetooth,Ios,Swift,Bluetooth,Core Bluetooth,我是swift编程新手,需要蓝牙方面的帮助 我正在做一个项目,这个项目涉及通过蓝牙向计算机发送字符串,我能够事先输入接收设备的MAC地址,这样它就知道在哪里发送 在此阶段,我唯一的问题是连接到所述设备,并发送数据。我试着查找教程,但它们要么是针对Android的(我已经开始工作了,我现在需要一个用于iOS的),要么是关于如何通过服务UUID连接(什么?) 以下是我目前掌握的代码: import UIKit import CoreBluetooth class transmitter: UIVi
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)
}
}
在这个阶段,我想做的是:
帮助?确定,因此似乎不支持SPP。废话:/ 我会研究约翰·多伊和保罗的建议
谢谢 您不能使用核心蓝牙通过MAC地址连接到设备。您需要扫描正在宣传您感兴趣的服务的外围设备,然后连接到它。连接后,您可以发现并写入设备提供的特征。核心蓝牙将MAC地址隐藏在标识符后面。另外,核心蓝牙不支持旧的配置文件,例如您可能正在与Android一起使用的串行端口配置文件。好的,有没有API允许我像Android一样通过MAC地址连接?或者这在iOSNo中是完全禁止的,没有api可以做你想做的事情。您需要在pc上运行一个应用程序来宣传您的iOS应用程序可以连接到的服务。更不用说,您对SPP做了更多的研究,看起来它受到bluetooth 4.0的支持(有点),但不支持BLE。因此,我的方法无效:(Bluetooth 4.0支持SPP配置文件,因为它与早期版本的Bluetooth向后兼容,但Core Data仅向iOS应用程序公开GATT配置文件。如果您的外设通过MFi认证,则它可以通过外部附件框架使用SPP配置文件。