Saturday, 30 June 2018

iOS CoreBluetooth print CBService and CBCharacteristic

I am using Swift to develop a iOS application to integrate bluetooth printer to print out information. I have used CoreBluetooth framework but can't know which service, characteristic I can writevalue to print out

//find CBService
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
    //println("CBPeripheralDelegate didDiscoverServices")

    for service in peripheral.services {

        println("Service: Discover service \(service)")
        println("Service: UUID \(service.UUID) ")

        peripheral.discoverCharacteristics(nil, forService: service as! CBService)

    }
}

//find CBCharacteristics
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {

    //if service.UUID == CBUUID(string: "18F0"){
        for characteristic in service.characteristics {

            let chara: CBCharacteristic? = characteristic as? CBCharacteristic
            println("Char: service \(service.UUID) Discover char \(chara)")
            println("Char: UUID \(chara!.UUID)")

            peripheral.readValueForCharacteristic(chara)

            /*
            println("0")

            switch chara!.UUID {

            case CBUUID(string: "2AF1"):
                println("1")

                var rawArray:[UInt8] = [0x01];
                let data = NSData(bytes: &rawArray, length: rawArray.count)

                peripheral.writeValue(data, forCharacteristic: chara, type: CBCharacteristicWriteType.WithoutResponse)

            default: println("")
            }
            */
        }
    //}

}

and the system display result like below:

Service: Discover service Service: UUID Battery Service: Discover service Service: UUID 1803 Service: Discover service Service: UUID 1802 Service: Discover service Service: UUID 1811 Service: Discover service Service: UUID 1804 Service: Discover service Service: UUID 18F0 Service: Discover service Service: UUID Device Information Service: Discover service Service: UUID E7810A71-73AE-499D-8C15-FAA9AEF0C3F2

Char: Discover char Char: UUID Battery Level Char: Discover char Char: UUID 2A06 Char: Discover char Char: UUID 2A06 Char: Discover char Char: UUID 2A47 Char: Discover char Char: UUID 2A46 Char: Discover char Char: UUID 2A48 Char: Discover char Char: UUID 2A45 Char: Discover char Char: UUID 2A44 Char: Discover char Char: UUID 2A07 Char: Discover char Char: UUID 2AF1 Char: Discover char Char: UUID 2AF0 Char: Discover char Char: UUID System ID Char: Discover char Char: UUID Manufacturer Name String Char: Discover char Char: UUID BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F

can any bluetooth expert can guide me, thanks



from iOS CoreBluetooth print CBService and CBCharacteristic

No comments:

Post a Comment