2017-03-03 3 views
0

J'essaie de créer une application iOS qui va lire à partir d'un périphérique OBDII Bluetooth 4 (LE). J'ai acheté un VGA icar3 bluetooth (4.0) elm327 OBDII. Je le branche dans ma voiture et trouve le périphérique IOS-VLink qui annonce certains services. Je peux ensuite obtenir les caractéristiques de ces services. Ce sont les services que je trouve:Comment utiliser BLE OBDII Peripheral

<CBService: 0x1780729c0, isPrimary = YES, UUID = Battery> 
<CBService: 0x178072a80, isPrimary = YES, UUID = 1803> 
<CBService: 0x178072ac0, isPrimary = YES, UUID = 1802> 
<CBService: 0x178072b00, isPrimary = YES, UUID = 1811> 
<CBService: 0x178072b40, isPrimary = YES, UUID = 1804> 
<CBService: 0x178072b80, isPrimary = YES, UUID = 18F0> 
<CBService: 0x178072bc0, isPrimary = YES, UUID = Device Information> 
<CBService: 0x178072c00, isPrimary = YES, UUID = E7810A71-73AE-499D-8C15-FAA9AEF0C3F2> 

Mais je ne sais pas quels sont ou comment les utiliser les 1803, 1802, 1811, 1804 et services 18F0. Voici mon programme simple pour savoir ce qui est annoncé.

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

var centralManager = CBCentralManager() 
var peripherals = [CBPeripheral]() 
@IBOutlet weak var outputTextView: UITextView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    centralManager.delegate = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    print("connected to \(peripheral.name ?? "unnamed")") 
    peripheral.delegate = self 
    peripheral.discoverServices(nil) 
} 

func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    if central.state == .poweredOn { 
     central.scanForPeripherals(withServices: nil, options: nil) 
    } 
} 

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
    if peripheral.name == "IOS-Vlink" { 
     peripherals.append(peripheral) 
     print(peripheral.name ?? "peripheral has no name") 
     print(peripheral.description) 
     central.connect(peripheral, options: nil) 
     central.stopScan() 
    } 

} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    guard let services = peripheral.services else { 
     return 
    } 
    for service in services { 
     print(service.description) 
     peripheral.discoverCharacteristics(nil, for: service) 
    } 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
    guard let chars = service.characteristics else { 
     return 
    } 
    for char in chars { 
     print(char.description) 
    } 
} 
} 
+0

Je trouve cela sur le E7810A ... UUID http://www.globals.market/offer/bcm20710%2520dual%2520mode%2520bluetooth%2520module%2520to%2520support%2520the%2520android%2520ios%2520apple% 2520master% 2520slave% 2520software% 2520can% 2520be% 2520customized% 2520at% 2520version-tb44737964123.html et ce https://www.bluetooth.com/specifications/gatt/services sur les autres uuids. Je ne sais toujours pas comment les utiliser. –

Répondre

0

Je l'ai compris. Le service "E7810A71-73AE-499D-8C15-FAA9AEF0C3F2" a une caractéristique avec un uuid de "BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F". Si vous écrivez une commande AT à cette caractéristique, elle appelle peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) et récupère les résultats dans la propriété value. Voici le code qui envoie une commande ATZ simple. À ce stade, il utilise simplement les commandes OBDII ELM327 correctes.

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

var centralManager = CBCentralManager() 
var peripherals = [CBPeripheral]() 
@IBOutlet weak var outputTextView: UITextView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    centralManager.delegate = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    print("connected to \(peripheral.name ?? "unnamed")") 
    peripheral.delegate = self 
    peripheral.discoverServices([CBUUID(string:"E7810A71-73AE-499D-8C15-FAA9AEF0C3F2")]) 
} 

func centralManagerDidUpdateState(_ central: CBCentralManager) { 
    if central.state == .poweredOn { 
     central.scanForPeripherals(withServices: nil, options: nil) 
    } 
} 

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
    if peripheral.name == "IOS-Vlink" { 
     peripherals.append(peripheral) 
     print(peripheral.name ?? "peripheral has no name") 
     print(peripheral.description) 
     central.connect(peripheral, options: nil) 
     central.stopScan() 
    } 

} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    guard let services = peripheral.services else { 
     return 
    } 
    for service in services { 
     peripheral.discoverCharacteristics([CBUUID(string:"BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F")], for: service) 
    } 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
    guard let chars = service.characteristics else { 
     return 
    } 
    guard chars.count > 0 else { 
     return 
    } 
    let char = chars[0] 
    peripheral.setNotifyValue(true, for: char) 
    peripheral.discoverDescriptors(for: char) 

    print (char.properties) 
    peripheral.writeValue("ATZ\r\n".data(using: .utf8)!, for: char, type: CBCharacteristicWriteType.withResponse) 

    peripheral.readValue(for: char) 
    if let value = char.value { 
     print(String(data:value, encoding:.utf8) ?? "bad utf8 data") 
    } 
} 

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
    if let value = characteristic.value { 
     print(String(data:value, encoding:.utf8)!) 
    } 
} 
func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) { 
    print(characteristic.descriptors ?? "bad didDiscoverDescriptorsFor") 
} 
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { 
    if let error = error { 
     print(error) 
    } 
    print("wrote to \(characteristic)") 
    if let value = characteristic.value { 
     print(String(data:value, encoding:.utf8)!) 
    } 
} 
} 
0

Si vous trouvez le service attribué sur https://www.bluetooth.com/specifications/gatt/services ils vous pouvez suivre le lien de service pour obtenir des informations sur la caractéristique (s). De la description du service, le lien caractéristique peut être suivi pour plus d'informations.

Par exemple: Le service 0x1804 est destiné au niveau de puissance d'émission. Il s'agit d'un service en lecture seule fournissant une caractéristique org.bluetooth.characteristic.tx_power_level qui, si elle est liée à, peut fournir un niveau db 8 bits signé entre -100 et 20.

Un autre exemple: Service 0x1811 est pour le service de notification d'alerte. Il fournit 5 caractéristiques distinctes, org.bluetooth.characteristic.supported_new_alert_category, org.bluetooth.characteristic.new_alert, org.bluetooth.characteristic.supported_unread_alert_category, org.bluetooth.characteristic.unread_alert_status et org.bluetooth.characteristic.alert_notification_control_point. La description du service donne une brève explication de chaque caractéristique et des liens vers d'autres détails sur les valeurs de cette caractéristique. Ces caractéristiques n'ont probablement pas de sens, et ne sont probablement pas supportées, si votre appareil n'est pas capable de communiquer par message vocal, SMS, messagerie instantanée ou courrier électronique.