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

时间:2018-01-31 04:31:20

标签: ios swift bluetooth core-bluetooth

我是swift编程的新手,&我需要帮助处理蓝牙问题。

我正在开发一个项目,该项目涉及通过蓝牙将字符串发送到计算机,并且我能够事先输入接收设备的MAC地址,以便知道在哪里发送它。

我在这个阶段唯一的问题是连接到所述设备,&amp;发送数据。我尝试查找教程,但它们要么是Android(我已经开始工作了,我现在需要一个用于iOS),或者它们是关于如何通过服务UUID连接(什么?)。< / p>

这是我到目前为止的代码:

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. 检查以确保蓝牙已启用(Works)
  2. 列出可用的设备,因为我无法通过MAC地址连接(失败)
  3. 如果我不必做第2步,那么会很好。只需通过提供的MAC地址进行连接,而不是扫描没有显示任何内容的设备。

    帮助?

1 个答案:

答案 0 :(得分:1)

好的,似乎不支持SPP。废话:/

我会看看John Doe和John Paulw11建议

谢谢!