Search code examples
iosswiftbluetoothcore-bluetooth

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


Im new to programming in swift, & I need help with working with bluetooth.

Im working on a project that involves sending a string to a computer via bluetooth, and Im able to enter the receiving device's MAC address beforehand so it has that to know where to send it.

My only problem at this stage is connecting to said device, & sending the data. I tried looking up tutorials, but they were either for Android (Which I already got working, I need one for iOS now), or they were about how to connect via service UUID (what?).

Heres the code I have so far:

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

}

What I was trying to do at this stage was:

  1. Check to make sure bluetooth is enabled (Works)
  2. List devices available, as I was not able to connect via MAC address (Fails)

What would be nice though is that if I didn't have to do step 2, & just connect via the provided MAC address instead of scanning for devices, which didn't show any.

Help?


Solution

  • Ok, so it seems that SPP is not supported. Crap :/

    Ill look into what both John Doe & Paulw11 suggested

    Thanks!