Get uuid in didreceivewriterequests ios - ios

I'm making BLE trnasport between tizen and ios using ble. I can send and receive data but I want to add some logic.
IOS function, didreceivewriterequests() callback is called when ios receive data and I want to store received data to a list with uuid.
Tizen already have this logic. But I don't know how to get uuid to identify what uuid is including data. I want to get the data in other funciton with a list by uuid.
How to get uuid in didreceivewriterequests() callback?
Thank you.
As I see, CBATTRequest.characteristic.UUID is deprecated.

characteristic.uuid is not deprecated. You need to access it from each request in the requests array. From there you can retreive the UUID of the characteristic.
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for request in requests {
let id = request.characteristic.uuid
print("ID is \(id.uuidString)")
}
}

Swift 3
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for writeRequest in requests {
print(characteristic.uuid.uuidString)
}
}

Related

iOS Xcode BLE receiving different data lengths

I am developing an iOS app using BLE, I have the BLE comms working but I do not seem to receive any data. The plan is to use multiple BLE UUID characteristics to send data from our ECU to the iOS mobile app. I know the BLE ECU is working as I have checked it with a BLE scanner.
I have followed this example, but still cannot get the BLE read to work correctly. Oddly if read the bytes back one by one I get the required data, but I cannot seem to find a solution to read the complete string in one go? I could use a loop to read all the data back, but surely there must be a way to read back the BLE receive buffer in one go?
https://www.freecodecamp.org/news/ultimate-how-to-bluetooth-swift-with-hardware-in-20-minutes/
Any help would be highly appreciated.
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
var characteristicASCIIValue = NSString()
guard characteristic == rxCharacteristic,
let characteristicValue = characteristic.value,
let ASCIIstring = NSString(data: characteristicValue, encoding: String.Encoding.utf8.rawValue) else { return }
print("Value Recieved: \((characteristicASCIIValue as String))")
if(characteristic.uuid.isEqual(CBUUIDs.ECU_UUID_A)
{
print ("Found ECU_UUID_A")
Extract_ECU_UUID_A(ASCIIstring)
}
else if(characteristic.uuid.isEqual(CBUUIDs.ECU_UUID_B)
{
print ("Found ECU_UUID_B")
Extract_ECU_UUID_B(ASCIIstring)
}
}

How to stream audio and send data over Bluetooth in Swift?

I'm developing and app for iOS which needs to connect to a Bluetooth Device. I've been able to find and connect to devices near me using CoreBluetooth and GATT profile. The writing and reading of characteristics worked as well. These values will eventually be written when a slider is moved. However, now I also need to be able to stream audio to the Bluetooth Device.
When I looked around for streaming audio over bluetooth I noticed this could be done with AVFoundation by allowing output over bluetooth. However, then the device has to use A2DP profile. But this causes the app to be unable to read/write characteristics?
I got this piece of code to read, notify and write to specific characteristics.
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService,
error: Error?) {
guard let characteristics = service.characteristics else { return }
for characteristic in characteristics {
print(characteristic)
if characteristic.properties.contains(.read) {
print("\(characteristic.uuid): properties contains .read")
if characteristic.uuid == batteryLevelCharacteristicCBUUID { peripheral.readValue(for: characteristic) }
}
if characteristic.properties.contains(.notify) {
print("\(characteristic.uuid): properties contains .notify")
if characteristic.uuid == batteryLevelCharacteristicCBUUID {
peripheral.setNotifyValue(true, for: characteristic)
}
}
if characteristic.properties.contains(.write) {
print("\(characteristic.uuid): properties contains .write")
if characteristic.uuid == writeTestCharacteristicCBUUID {
print("Writing characteristic found! Trying to write now.")
let string = "Hello World!"
let data = string.data(using: .utf8)!
peripheral.writeValue(data, for: characteristic, type: CBCharacteristicWriteType.withResponse)
}
}
}
}
This question seems similar but it is mentioned that it's not possible. Is possible send hexadecimal data to A2DP Bluetooth Device in swift?
However, I was thinking. If you look at a headset for example when you press buttons there has to be some data transfer. Can this somehow be used for what I want to do? But I can't seem to find any information about these signals or how to use those in my code.
So what I want is to be able to stream the audio and also send small values like percentages.
If you know any information that might help me please let me know. Thanks in advance!

sending data to bluetooth module to be read by arduino rom iOS swift 3

If i want to send data to a bluetooth module, which is connected to the Arduino, what code lines are specifically the ones I need to take notice of.
I want to send something like, the number '75' to the bluetooth module and the Arduino will read it
thanks
Bluetooth LE is very long and cumbersome with many delegates in between. The minimum path to write your data is:
Make sure you have bluetooth permission: CBCentralManagerDelegate.centralManagerDidUpdateStateand if so start scanning with scanForPeripherals
CBCentralManagerDelegate.didDiscover If this is the peripheral you want then set yourself as its delegate
CBPeripheralDelegate.peripheral:didDiscoverServices: If this is the service you want then stop scanning and discoverCharacteristics:for: service
CBPeripheralDelegate.peripheral:didDiscoverCharacteristicsFor: service If a characteristic in the array of characteristics is the one you want then:
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
for characteristic in characteristics {
if characteristic.uuid == CBUUID(string: characteristicIdentifier) {
let value: UInt8 = 75
let data = Data(bytes: [value])
peripheral.writeValue(data, for: characteristic, type: .withResponse)
}
}
}

How to get the characteristic(s) of HM-10 Bluetooth LE in iOS

I am trying to send some data from an iOS device to the HM-10 Bluetooth LE Module connected to an arduino. Problem is after connecting to the module discoverServices doesn't return a characteristic for the service.
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
if(error != nil) {
print(error?.description)
}
for service in peripheral.services! {
let thisService = service as CBService
print("Service: \(thisService.description)")
print("Characteristic: \(thisService.characteristics)")
}
}
This outputs:
Service: <CBService: 0x137e84ea0, isPrimary = YES, UUID = FFE0>
Characteristic: nil
I am a beginner with this arduino stuff as well as iOS. So any suggestions would be welcome. Maybe there is a way to write to the bluetooth module without knowing the characteristic... I have no idea.
I finally managed to get the answer. Because of my rudimentary understanding of the CoreBluetooth Framework I forgot to call discoverCharacteristics in didDiscoverServices. Well, I am really learning by doing. (I somehow thought discoverServices would call discoverCharacteristics itself.)

swift - central writing characteristic to Peripheral

I have connected to two iPhones and found the services and characteristics in it. I am facing problems in sending write requests from central and receiving response from peripheral. I have written the code below to write data in central and received data in Peripheral. But the function didReceiveWriteRequests didn't execute. I don't know Why didn't work.
In central view write, the data code:
func writeValue(data:NSData){
if let discoveredPeripheral = discoveredPeripheral{
if let deviceCharacteristics = deviceCharacteristics{
discoveredPeripheral.writeValue(data, forCharacteristic: deviceCharacteristics, type: CBCharacteristicWriteType.WithResponse)
}
}
}
and in peripheral view, the receive code:
func peripheralManager(peripheral: CBPeripheralManager!, didReceiveWriteRequests requests: [AnyObject]!) {
println("didReceiveWriteRequests")
println("\(requests.count)")
}

Resources