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

标签 ios swift bluetooth core-bluetooth

我是 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 的建议

谢谢!

关于iOS:是否可以通过蓝牙将字符串发送到计算机,只需给它 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48534518/

相关文章:

ios - 0.f 意味着比 0.0f 更高的精度是什么意思

ios - 如何在 ViewController 中将 xib 作为 "pop up"调出?

蓝牙调试工具

android - 如何防止名称缓存并在发现时检测蓝牙名称更改

ios - 无法在 UITextfield 中输入

ios - 是我不了解 UIActivityViewController,还是目前的实现很糟糕?

ios - Swift Progress View 动画使进度条超过 100%

ios - 如何在不滚动的情况下选择 TableView 中的单元格?

ios - 如何使用 Depth Api 去除肖像背景?

bluetooth - Obexd 守护进程未运行