iOS:是否可以通过蓝牙将字符串发送到计算机,只需给它 MAC 地址

iOS: Is it possible to send a string to a computer via bluetooth just by giving it the MAC address

我是 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)
    }

}

我在这个阶段尝试做的是:

  1. 检查以确保蓝牙已启用(有效)
  2. 列出可用设备,因为我无法通过 MAC 地址连接(失败)

如果我不必执行第 2 步,只需通过提供的 MAC 地址连接而不是扫描未显示任何设备的设备,那就更好了。

帮忙?

好的,看来不支持SPP。废话:/

我会研究 John Doe 和 Paulw11 的建议

谢谢!