IObluetooth mac os doesn't sending commands to device. (swift, vapor, bluetooth, serialport)

63 Views Asked by At

We have been using Golang for a long time to communicate via Bluetooth with one of the devices. we use the go.bug.st/serial library.Now I decided to try something native from the Mac. I found Vapor as a framework and am trying to connect with IoBluetooth. It connect and disconnects but doesn't send any command. The device module is used on device is microchip rn4678 device. It has support for rfcomm and l2capp protocols. but I don't understand why the data is not sending to the device.

import Fluent
import Vapor
import IOBluetooth

struct BluetoothDeviceDTO: Content {
    let nameaddress: String
    let name: String
    let isPaired: Bool
    let isConnected: Bool
    let address :String
}


func routes(_ app: Application) throws {
    app.get { req async throws in
        try await req.view.render("index", ["title": "Hello Vapor!"])
    }
    
    app.get("ports") { req->[BluetoothDeviceDTO] in
        var bluetoothDevices: [BluetoothDeviceDTO] = []
        do {
            guard let devices = try IOBluetoothDevice.pairedDevices() else {
                throw Abort(.notFound, reason: "No Bluetooth devices found")
            }
            
            for item in devices {
                if let device = item as? IOBluetoothDevice {
                    let bluetoothDeviceDTO = BluetoothDeviceDTO(
                        nameaddress: device.nameOrAddress,
                        name: device.name,
                        isPaired: device.isPaired(),
                        isConnected: device.isConnected(),
                        address: device.addressString
                    )
                    bluetoothDevices.append(bluetoothDeviceDTO)
                }
            }
            
        } catch {
            throw Abort(.internalServerError, reason: "Failed to fetch Bluetooth devices: \(error)")
        }
        
        return bluetoothDevices
    }
    
    
    app.get("calibrate", ":address"){ req -> String in
        var address = req.parameters.get("address")!
        //Receive mac address device 04-91-62-91-1c-62
        
        
        //create device
        var device = IOBluetoothDevice(addressString: address)
        
        //data to send
        let str = "[C]\r"
        var data: [UInt8] = []
        
        for char in str.utf8 {
            data.append(char)
        }
        
        
        var ch: IOBluetoothRFCOMMChannel?
        var channelID =  BluetoothRFCOMMChannelID()
        
        let services = device!.services as! [IOBluetoothSDPServiceRecord]
        
        for service in services {
            if service.getRFCOMMChannelID(&channelID) == kIOReturnSuccess{
                break
            }
        }
        
    
        //set parametrs for channel
        ch?.setSerialParameters(115200, dataBits: 8, parity: kBluetoothRFCOMMParityTypeNoParity, stopBits: 1)
        
      
        //open channel
        device?.openRFCOMMChannelAsync(&ch, withChannelID: channelID, delegate: nil)
        
        sleep(2)
        
        print(ch?.isOpen()) // true
        
        
        
        defer{
            ch?.close()
        }
        
        //send data
        if (ch?.writeAsync(&data, length:4, refcon: nil) == kIOReturnSuccess){
            print("OK") // print ok but device doesn't receive anything
        }else{
            print("NO")
        }
        
        sleep(10)//sleep to await

        return "OK"
        
    }


    try app.register(collection: TodoController())
}

enter image description here

I also tried the l2cap protocol, but the result was the same.

0

There are 0 best solutions below