Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Bluetooth/CoreBluetooth/Bluetooth LE Heart Rate Mac
Function: You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Bluetooth/CoreBluetooth/Bluetooth LE Heart Rate Mac
Project "Bluetooth LE Heart Rate Mac.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open() centralManager = new CBCentralManager centralManager.list = list End EventHandler
Property centralManager As CBCentralManager
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class CBCentralManager Inherits CBCentralManagerMBS
EventHandler Sub DidConnectPeripheral(peripheral as CBPeripheralMBS) list.Log CurrentMethodName+": "+peripheral.Name // make new object, so we catch events dim p as new CBPeripheral(peripheral) p.list = list peripherals.append p dim serviceUUIDs() as CBUUIDMBS serviceUUIDs.Append CBUUIDMBS.UUIDWithString("180D") // discoverServices with no array to get all services p.discoverServices serviceUUIDs End EventHandler
EventHandler Sub DidDisconnectPeripheral(peripheral as CBPeripheralMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if connectedPeripheral = nil End EventHandler
EventHandler Sub DidDiscoverPeripheral(peripheral as CBPeripheralMBS, advertisementData as Dictionary, RSSI as String) dim Name as string = peripheral.name list.Log CurrentMethodName+": "+RSSI+" "+Name if Name <> "iPhone" and connectedPeripheral = nil then // make new object dim p as new CBPeripheral(peripheral) p.list = list connectedPeripheral = p self.connectPeripheral p end if End EventHandler
EventHandler Sub DidFailToConnectPeripheral(peripheral as CBPeripheralMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidUpdateState() Select case state case me.kStatePoweredOff list.Log CurrentMethodName+": PoweredOff" case me.kStatePoweredOn list.Log CurrentMethodName+": PoweredOn" case me.kStateResetting list.Log CurrentMethodName+": Resetting" case me.kStateUnauthorized list.Log CurrentMethodName+": Unauthorized" case me.kStateUnknown list.Log CurrentMethodName+": Unknown" case me.kStateUnsupported list.Log CurrentMethodName+": Unsupported" else list.Log CurrentMethodName+": ?" end Select if me.State = me.CBCentralManagerStatePoweredOn then list.Log "State changed to poweredOn" dim serviceUUIDs() as CBUUIDMBS serviceUUIDs.Append CBUUIDMBS.UUIDWithString("180D") // already found it? dim lastPeripherals() as CBPeripheralMBS = me.retrieveConnectedPeripheralsWithServices(serviceUUIDs) if lastPeripherals.ubound >= 0 then dim device as CBPeripheralMBS = lastPeripherals(lastPeripherals.ubound) dim p as new CBPeripheral(device) peripherals.Append p me.connectPeripheral p else // look for it me.scanForPeripheralsWithServices serviceUUIDs end if end if End EventHandler
Property connectedPeripheral As CBPeripheral
Property list As listbox
Property peripherals() As CBPeripheral
End Class
Class CBPeripheral Inherits CBPeripheralMBS
EventHandler Sub DidDiscoverCharacteristicsForService(service as CBServiceMBS, error as NSErrorMBS) list.Log CurrentMethodName dim ServiceUUID as string = service.UUID.UUIDString if ServiceUUID = "180D" then dim characteristics() as CBCharacteristicMBS = service.characteristics for each characteristic as CBCharacteristicMBS in characteristics select case characteristic.UUID.UUIDString case "2A37" // Set notification on heart rate measurement list.Log "Found a Heart Rate Measurement Characteristic" me.setNotifyValue(true, characteristic) case "2A38" // Read body sensor location list.Log "Found a Body Sensor Location Characteristic" me.readValueForCharacteristic(characteristic) case "2A39" // Write heart rate control point list.Log "Found a Heart Rate Control Point Characteristic" dim data as new MemoryBlock(1) data.UInt8Value(0) = 1 me.writeValue(data, characteristic, CBCharacteristicMBS.kPropertyWriteWithoutResponse) end Select next end if if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidDiscoverDescriptorsForCharacteristic(characteristic as CBCharacteristicMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidDiscoverIncludedServicesForService(service as CBServiceMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidDiscoverServices(error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if dim services() as CBServiceMBS = me.services for each service as CBServiceMBS in Services list.Log "Service found: "+service.UUID.UUIDString me.discoverCharacteristics service next End EventHandler
EventHandler Sub DidUpdateName() list.Log CurrentMethodName+": "+me.name End EventHandler
EventHandler Sub DidUpdateNotificationStateForCharacteristic(characteristic as CBCharacteristicMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidUpdateRSSI(error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidUpdateValueForCharacteristic(characteristic as CBCharacteristicMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if select case characteristic.UUID.UUIDString case "2A37" update(characteristic.value) end Select End EventHandler
EventHandler Sub DidUpdateValueForDescriptor(descriptor as CBDescriptorMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidWriteValueForCharacteristic(characteristic as CBCharacteristicMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
EventHandler Sub DidWriteValueForDescriptor(descriptor as CBDescriptorMBS, error as NSErrorMBS) list.Log CurrentMethodName if error <> nil then list.Log "Error: "+error.LocalizedDescription end if End EventHandler
Sub update(data as MemoryBlock) if data.size >= 2 then dim bpm as integer if BitwiseAnd(data.UInt8Value(0), 1) = 0 then bpm = data.uint8Value(1) else bpm = data.uint8Value(1) * 256 + data.uint8Value(2) end if list.Log "BPM: "+str(bpm) end if End Sub
Property list As listbox
End Class
Module Module1
Sub Log(extends l as listbox, row as string) l.AddRow row // scroll at bottom l.ScrollPosition = l.ListCount End Sub
End Module
End Project

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Bluetooth Plugin.





Links
MBS Xojo Chart Plugins